George has posted 1 posts at DZone. View Full User Profile

Using Spring 2.x to Wire Model 1 Servlets

06.06.2008
| 5287 views |
  • submit to reddit

At work I am currently maintaining a older, Model 1 Servlet application. This application is working great in production, even though I think the overall back end design is about as pretty as the Elephant Man! I have always longed for better ways to add functionality to the code, and long for using Spring in parts of the applications.

Well recently I needed to migrate some Spring code from another application into this old one. My first thought is, oh crap, this is going to be interesting. Well things were interesting indeed, and for the positive. I found that there is a nice, clean way to integrate the Spring Framework with my current HTTP Servlets.

First I identified that I would like to have spring wire up some Database Calls in my LoginServlet. To do this I had to make a change to the web.xml to use the ContextLoaderListener class and load the applicaitonContext.xml file.

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Now that these spring is setup to go, I have identified that the LoginServlet is going to be wired using some spring code. To do this I change my web.xml file to use the HttpRequestHandlerServlet in Spring for the LoginServlet servlet-class definition.
<servlet>
<servlet-name>LoginServlet</servlet-name>
<display-name>LoginServlet</display-name>
<!-- servlet-class>edu.unc.ais.bs.onecard.servlets.LoginServlet</servlet-class -->
<servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
</servlet>
Next I need to reference the LoginServlet class that is in my application and have is wired with Spring. To do this I created a Spring bean in the applicationContext with the same id name as the servlet-name parameter in the web.xml.
   <bean id="LoginServlet" class="edu.unc.ais.bs.onecard.servlets.LoginServlet">
<property name="hrEmpApptDataDAO" ref="hrEmpApptDataDAO"/>
<property name="oneCardAccountDAO" ref="oneCardAccountDAO"/>
<property name="oneCardSvcDAO" ref="oneCardSvcDAO"/>
</bean>
The last piece is pretty easy, just go to LoginServlet class and implement the HttpRequestHandler as so.
public class LoginServlet implements HttpRequestHandler {
This will require you to add the handleRequest method to the servlet, which is what is called when you access the servlet from your applicaiton. The doGet and doPost, and other default methods are overridden and handled for you by default. That was it. Now I have this old servlet, which I can wire up and pass Spring JdbcTemplete objects to.

 

Published at DZone with permission of its author, George Jennings.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Cody A_ replied on Fri, 2008/06/06 - 11:58am

Spring integration has become so much easier these days... Nice.

I want to see more  : )

 -dzoneCody

don fernando replied on Wed, 2008/07/09 - 4:48am

nice article.... =)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.