7

I am trying to run two Servlet-class in a single web.xml but its not working, each servlet-class works fine independently.

web.xml:

<servlet>
    <servlet-name>spring-ws</servlet-name>
    <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
    <init-param>
        <param-name>transformWsdlLocations</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>spring-ws</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<session-config>
    <session-timeout>240</session-timeout>
</session-config>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-ws-servlet.xml
     /WEB-INF/health-page-servlet.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>health-page</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>health-page</servlet-name>
    <url-pattern>/health.htm</url-pattern>
</servlet-mapping>

Do let me know if you can figure something wrong that i am doing.

I tried the below link but it doesnt work for me Can I use Spring MVC and Spring WS in one single application?

Community
  • 1
  • 1
Rajeev
  • 91
  • 1
  • 2
  • 6

3 Answers3

6

This isn't going to work. The one which is mapped on /* overtakes all requests. You need to map it on / instead so that it will only intercept on requests which are not matched by all other existing servlets (including the JSP servlet which is implicitly mapped on *.jsp and all "normal" static resources like CSS/JS/image files!). See also Difference between / and /* in servlet mapping url pattern.

If being able to serve static resources is also required, then better map it on a more specific URL pattern like /ws/* and create a Filter which checks the request URI and then forwards accordingly. That filter can in turn safely be mapped on /*. See also this answer for a more concrete code example: How to access static resources when mapping a global front controller servlet on /*.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I thought mappings would be used from most- to least-specific? – Dave Newton Nov 05 '11 at 02:13
  • @Dave: Not if there's a `/*`. Such a mapping should only be used on filters. – BalusC Nov 05 '11 at 02:16
  • So that's considered different from a directory match, like /foo/*? – Dave Newton Nov 05 '11 at 02:20
  • 1
    @Dave: yes. The `/*` (and an empty string) matches **all** requests. Try it yourself and check servlet spec. – BalusC Nov 05 '11 at 02:22
  • I tried using spring-ws / This spring-ws servlet stops working, what should ws folder in /ws/* should contain ?? only the spring-ws-servlet.xml or someother files. Also doing the URL-matching for servlet spring-ws as / , the other servlet(health-page) starts working fine. – Rajeev Nov 07 '11 at 19:05
  • Okay. Hope that helped. I would have chosen a more specific URL pattern though. – BalusC Nov 07 '11 at 19:08
  • @BalusC , the / is not working for spring-ws , what should ws in /ws/* contain, please let me know. I am not very sure on that. – Rajeev Nov 07 '11 at 19:14
  • Your concrete problem is that you're trying to integrate Spring WS in an existing Spring(MVC) based application instead of integrating it in its own standalone webapp. You should map it on a more specific URL pattern instead, such as for example `/ws/*`. You may choose whatever you want; it's just that extra path in the URL which would then invoke the Spring WS servlet. So instead of for example http://localhost:8080/contextname/some-spring-ws-resource.wsdl, you need to invoke http://localhost:8080/contextname/ws/some-spring-ws-resource.wsdl to get it to run. – BalusC Nov 07 '11 at 19:22
  • Or, if they have all a common extension, such as `.wsdl`, then you can also use URL pattern of `*.wsdl` instead. The key point is that it should not conflict with the existing webapp. – BalusC Nov 07 '11 at 19:23
  • @BalusC .. got that working , atleast the basic problem got resolved. thanks – Rajeev Nov 07 '11 at 19:46
2

I am using Java configuration in my project and following code works fine for the same purpose:

public class Initializer implements WebApplicationInitializer {

    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(ApplicationConfiguration.class);
        ctx.setServletContext(servletContext);

        MessageDispatcherServlet messageDispatcherServlet = new MessageDispatcherServlet();
        messageDispatcherServlet.setApplicationContext(ctx);
        messageDispatcherServlet.setTransformWsdlLocations(true);
        Dynamic dynamic = servletContext.addServlet("messageDispatcherServlet", messageDispatcherServlet);
        dynamic.addMapping("/ws/*");
        dynamic.setLoadOnStartup(1);

        dynamic = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
        dynamic.addMapping("/");
        dynamic.setLoadOnStartup(1);
    }
}
Olga
  • 91
  • 4
0

you have a mapping for /* in the spring-ws section which is getting the request. you need to come up with a different strategy... Try putting the /health.htm before the /* mapping.

speajus
  • 46
  • 1