1

First of all I have have generic http servlet which I want to run in specific contexts depending on URL mapping (so I switched to HttpRequestHandler implementation). Please consider simplified example. In web.xml I have following configuration:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>testServlet1</servlet-name>
    <servlet-class>local.TestServlet</servlet-class>
</servlet>
<servlet>
    <servlet-name>testServlet2</servlet-name>
    <servlet-class>local.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>testServlet1</servlet-name>
    <url-pattern>/test1/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>testServlet2</servlet-name>
    <url-pattern>/test2/*</url-pattern>
</servlet-mapping>

My servlet is implementation of HttpRequestHandler and looks like here:

public class TestRequestHandler implements HttpRequestHandler {

private Counter counter;

public void setCounter(Counter counter) {
    this.counter = counter;
}

@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter writer = response.getWriter();
    writer.write("<h1>Hello World</h1>");
    writer.write("<h1>" + counter.getName() + ": " + counter.getValue() + "</h1>");
}
}

So I want to init both servlet beans (testServlet1 and testServlet2) with different Counters beans and have one root context(applicationContext.xml) for common beans and two separate servlet contexts that could extend and override root context (testServlet1-servlet.xml and testServlet2-servlet.xml). So configuration should be something like this:

<bean id="counter" class="local.Counter">
    <property name="name" value="CounterA"/>
</bean>

<bean id="testServlet1" class="local.TestRequestHandler">
    <property name="counter" ref="counter"/>
</bean>

and

<bean id="counter" class="local.Counter">
    <property name="name" value="CounterB"/>
</bean>

<bean id="testServlet2" class="local.TestRequestHandler">
    <property name="counter" ref="counter"/>
</bean>

Is it possible and how could such configuration be implemented? I thought about DispatcherServlet but don't understand how it could be configured with implementation of HttpRequestHandler.

0

1 Answer 1

1

I have found a solution to my task. In few words - We create two DispatcherServlets and each servlet configuration has its own context which extends the root context. Here is the configuration:

web.xml - setup for two DispatcherServlets

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>dispatcher1</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet>
    <servlet-name>dispatcher2</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher1</servlet-name>
    <url-pattern>/test1</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>dispatcher2</servlet-name>
    <url-pattern>/test2</url-pattern>
</servlet-mapping>

applicationContext.xml - root context and common beans goes here

<bean id="counter" class="local.Counter">
    <property name="name" value="CounterC"/>
</bean>

dispatcher1-servlet.xml - overrides implementation of common beans and specific configuration in own context

<bean id="counter" class="local.Counter">
    <property name="name" value="CounterA"/>
</bean>

<bean name="testServlet" class="local.TestRequestHandler">
    <property name="counter" ref="counter"/>
</bean>

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="/test1">testServlet</prop>
        </props>
    </property>
</bean>

Not the answer you're looking for? Browse other questions tagged or ask your own question.