Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using Spring 2.x to Wire Model 1 Servlets

DZone's Guide to

Using Spring 2.x to Wire Model 1 Servlets

· Java Zone
Free Resource

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

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.

 

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}