Thursday, July 10, 2008

Using Spring 2.x to Wire Model 1 Servlets

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.

1 comment:

Praveen said...

hi,

u r blog was pretty cool , i am trying to do exactly what you were doing here...
so in the old login servlet . i assume you had services or doPost, Do get methods.. which you had remove and implement a single doHandle ?? is that so ?