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?
Asked
Active
Viewed 734 times
0
![](../../users/profiles/1941151.webp)
incomplete-co.de
- 2,059
- 15
- 22
![](../../users/profiles/269061.webp)
Trevor
- 11,966
- 11
- 74
- 94
-
1see: http://stackoverflow.com/questions/2006022/spring-mvc-servlet-initialization and http://stackoverflow.com/questions/5419695/init-method-in-spring-controller-annotation-version – DannyMo Jul 18 '13 at 23:16
-
1Can you specify what you're trying to do? Maybe it can be achieved in a simpler way – Carlos Gavidia-Calderon Jul 19 '13 at 01:47
-
I would like to run a single background process which any request can pass long-term work to. See the 'Background Processing' section at [http://oreilly.com/catalog/jservlet/chapter/ch03.html]. – Trevor Jul 19 '13 at 04:17
1 Answers
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>
![](../../users/profiles/1828832.webp)
HunkD
- 66
- 2
-
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)? – Trevor Jul 19 '13 at 04:21
-
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". – Yugang Zhou Jul 19 '13 at 04:32