0

I'm working on a Java web app that uses the Spring Framework (MVC). All my code is in controller files that are instantiated by the servlet. I would like to extend the servlet so that I can run some code in the init of the servlet; however, I'm very new to the Spring Framework and Java web development in general. I'm not sure how to extend the servlet, where I would put my derived servlet, etc. Can someone point me in the right direction on this?

3

1 Answer 1

2

Did you mean dispatcherServlet? you can just extends this servlet, re-config it in web.xml.

<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>Your DispatcherServlet</servlet-class>
<init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:/META-INF/springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>

Sign up to request clarification or add additional context in comments.

2 Comments

I'm not familiar with defining code in an XML file. How would I add code to the derived servlet's init function? Is there no way to extend the dispatcherServlet using a traditional class file (.java)?
Of course you can extend the DispatcherServlet. The answer is telling how to register your custom DispatcherServlet in the web container. Note the "Your DispatcherServlet".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.