Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9134849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:44:10+00:00 2026-06-17T08:44:10+00:00

I have a project with a few old HTTPServlets, I would like a way

  • 0

I have a project with a few old HTTPServlets, I would like a way to manage the creation/life-cycle of the Servlet in Spring so I can do a few things like DI and pass in database objects through Spring/Hibernate integration.

First I have setup the Spring application context in web.xml as follows;

<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>

Next I have my servlet definition in web.xml;

<servlet>
    <servlet-name>oldHttpServlet</servlet-name> 
    <display-name>oldHttpServlet</display-name>
    <servlet-class>com.package.MyServlet</servlet-class>
</servlet>

In my Spring application-context.xml I would like to make some bean def as follows;

<bean id="oldHttpServlet" class="com.package.MyServlet"></bean>

I think the above I need to implement some interface within my servlet, keep the bean definition as above in the Spring app-context.xml and then make a change to the servlet definition in the web.xml… I’m not sure what is the simplest change to make as there is some inheritance to worry about on the MyServelet.java side, which looks like this;

class MyServlet extends MyAbstractServlet{
  void doStuff(){
     //YOu know...
  }
}

And MyAbstractServlet;

class MyAbstractServlet extends HttpServlet{
  doPost(){
     //Some post implementation here...
  }
}

What is the best way to wrap MyServlet in Spring and have it loaded from there rather than being instantiated via web.xml?

I’m assuming the best way would be to use Springs HttpRequestHandler and then use the HttpRequestHandlerServlet in the web.xml in place of the current servlet. However I’m not sure how I go about implementing the HttpRequestHandler interface to work with the existing doPost() stuff in the myAbstractServlet…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:44:11+00:00Added an answer on June 17, 2026 at 8:44 am

    If you need to inject dependencies into a servlet than I would go with Spring’s HttpRequestHandlerServlet. You create an implementation of HttpRequestHandler (it has a method:

    public void handleRequest(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException;
    

    that you need to implement – it is the equivalent of what you’d have in a doGet / doPost.
    The instance of all your HttpRequestHandler implementatins should be handled by spring (set up an annotation driven config and annotate it with, say, @Component) or use an XML config to set up those beans.

    In your web.xml create the mapping for HttpRequestHandlerServlet. If you name the servlet the same as you did your HttpRequestHandler then Spring will make HttpRequestHandlerServlet automatically handle the request with the matching HttpRequestHandler.

    Since HttpRequestHandler is a Spring bean, you can make Spring inject all the services you need into it.

    If you still do not want to use HttpRequestHandler in lieu of Servlets than all that is left is some “programming judo”, like retrieving WebApplicationContext with Spring utils and getting beans from there by a “per name” basis. Servlets are not instantiated by Spring so it cannot manage them. You would have to create a base class for your servlets and manage the DI as an initialization step yourself.

    The code would look like this (inside a servlet):

    WebApplicationContext webContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    CertainBeanClass bean = (CertainBeanClass) webContext.getBean("certainBean");
    

    [EDIT]
    Indeed, as one of the commenters suggested, there IS one more option. It still requires you to do a manual setup in your servlets init method though. Here is how this would look like:

    public void init(ServletConfig config) {
        super.init(config);
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    }
    

    You may also try your luck with @Configurable annotation on Servlet with weaving, but this might or might not work (I think Servlet might get initialized before Spring context is set up, so it still won’t have the possibility to do its magic).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project with few dlls. I would like to change one of
I have a few usercontrols from an old Windows CE project that I'd like
I have a VisualStudio project with a few sub folders like Accounting, Store etc.
How can I restore Xcode project settings? I can't see few settings (like Targeted
How can I restore Xcode project settings? I can't see few settings (like Targeted
I have this project that it's due in a few hours and I still
I created an Android project a few months ago and now have to automate
I have a project in PHPStorm, which contains a few libraries (Zend, Doctrine, ...)
I have C# project in which I am using Microsoft.SqlServer.Management.Smo, Microsoft.SqlServer.batchparser.dll and few other
I have recently picked up a project from a few months ago. Went to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.