Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 600
David Buckley Avatar asked Dec 08 '09 06:12

David Buckley


People also ask

Can we have multiple servlets in Web xml?

You can declare multiple servlets using the same class with different initialization parameters. The name for each servlet must be unique across the deployment descriptor. The <servlet-mapping> element specifies a URL pattern and the name of a declared servlet to use for requests whose URL matches the pattern.

Can we have multiple servlets in a project?

So the short answer is, you will create many servlets per webapp since each webapp will expose several use cases.

Which tags are used to register a servlet in Web xml?

Your servlet name Registration should be same on both tags <servlet>... </servlet> and <servlet-mapping>... </servlet-mapping> and also package name should be same where your servlet class is located.


2 Answers

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

<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> 
like image 70
Pascal Thivent Avatar answered Oct 22 '22 15:10

Pascal Thivent


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 *-servlet.xml files are used automatically and each creates an application context for that servlet.

From the Spring documentation, 13.2. The DispatcherServlet:

The framework will, on initialization of a DispatcherServlet, look for a file named [servlet-name]-servlet.xml in the WEB-INF 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).

like image 20
cletus Avatar answered Oct 22 '22 16:10

cletus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!