DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Frameworks
  4. Using Spring 2.x to Wire Model 1 Servlets

Using Spring 2.x to Wire Model 1 Servlets

George Jennings user avatar by
George Jennings
·
Jun. 06, 08 · Interview
Like (0)
Save
Tweet
Share
6.83K Views

Join the DZone community and get the full member experience.

Join For Free

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.

 

Spring Framework

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Are the Benefits of Java Module With Example
  • How Elasticsearch Works
  • Introduction to Container Orchestration
  • Getting a Private SSL Certificate Free of Cost

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: