How to register multiple servlets in web.xml in one Spring application

2022-09-01 13:19:53

I want to define two servlets in my Spring web.xml - one for the application html/jsp pages, and one for a web service that will be called by an external application. Here is the web.xml:

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

<servlet-mapping>
  <servlet-name>myservlet</servlet-name>
  <url-pattern>*.htm</url-pattern>
</servlet-mapping>

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>WEB-INF/user-service-servlet.xml</param-value>
</context-param>

<servlet>
  <servlet-name>user-webservice</servlet-name>
  <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
  <servlet-name>user-webservice</servlet-name>
  <url-pattern>/UserService/*</url-pattern>
</servlet-mapping>

If I have myservlet use the DispatcherServlet in the file by itself, it works fine. If I have the user-webservice with the context-param for it's config file (user-service-servlet.xml), it works fine. However, if I have both in the file, then the myservlet doesn't work as the myservlet-servlet.xml file isn't loaded automatically. If I remove the context-param, then the myservlet works, but the user-webservice doesn't work as it's configuration file (user-service-servlet.xml) isn't loaded.

How can I have both servlets defined and both of their configuration files loaded?


答案 1

As explained in this thread on the cxf-user mailing list, rather than having the CXFServlet load its own spring context from , you can just load the whole lot into the root context. Rename your existing to some other name (e.g. ) then change your parameter to something like: user-webservice-servlet.xmluser-webservice-servlet.xmluser-webservice-beans.xmlcontextConfigLocation

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

<servlet-mapping>
  <servlet-name>myservlet</servlet-name>
  <url-pattern>*.htm</url-pattern>
</servlet-mapping>

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
    /WEB-INF/applicationContext.xml
    /WEB-INF/user-webservice-beans.xml
  </param-value>
</context-param>

<servlet>
  <servlet-name>user-webservice</servlet-name>
  <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  <load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
  <servlet-name>user-webservice</servlet-name>
  <url-pattern>/UserService/*</url-pattern>
</servlet-mapping>

答案 2

Use config something like this:

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

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

<servlet>
  <servlet-name>user-webservice</servlet-name>
  <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  <load-on-startup>2</load-on-startup>
</servlet>

and then you'll need three files:

  • applicationContext.xml;
  • myservlet-servlet.xml; and
  • user-webservice-servlet.xml.

The files are used automatically and each creates an application context for that servlet.*-servlet.xml

From the Spring documentation, 13.2. The DispatcherServlet:

The framework will, on initialization of a , look for a file named [servlet-name]-servlet.xml in the directory of your web application and create the beans defined there (overriding the definitions of any beans defined with the same name in the global scope).DispatcherServletWEB-INF


推荐