本文介绍了在单个web.xml中添加多个servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一个web.xml中运行两个Servlet类,但是它不起作用,每个servlet类都可以独立工作。

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 meCan I use Spring MVC and Spring WS in one single application?

推荐答案

这不会起作用。映射在 / * 上的那个超过所有请求。您需要在 / 上映射它,以便它只拦截与所有其他现有servlet不匹配的请求(包括隐式映射到<$的JSP servlet) c $ c> *。jsp 以及所有普通静态资源,如CSS / JS /图像文件!)。另请参阅。

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.

如果还需要提供静态资源,那么最好将其映射到更具体的URL模式,如 / ws / * 并创建一个过滤器,它会检查请求URI,然后相应地转发。该过滤器可以安全地映射到 / * 。有关更具体的代码示例,请参阅此答案:上映射全局前端控制器servlet时如何访问静态资源。

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 /*.

这篇关于在单个web.xml中添加多个servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 09:06