Hello everyone,
Since I have lots of servlet mappings in my web.xml I was wondering if I could actually store all the mappings to separate file and then include it to web.xml.
servervlet-mapping.xml
<servlet>
<servlet-name>red</servlet-name>
<servlet-class>Test.Red</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>red</servlet-name>
<url-pattern>/blue</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>blue</servlet-name>
<servlet-class>Test.Blue</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>blue</servlet-name>
<url-pattern>/blue</url-pattern>
</servlet-mapping>
web.xml
<include file="servlet_mapping.xml"/>
This way it's possible to load xml files in struts.xml. (not sure if the same is possible in web.xml)
Is something like this possible? Or is there any other way to make it work?
(Apologies for my bad english)
Thanks in advance, Alex
Only one web. xml inside WEB-INF folder.
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.
It can be more than one or it is only one for the whole application.
The web. xml file is derived from the Servlet specification, and contains information used to deploy and configure the components of your web applications. When configuring Tomcat for the first time, this is where you can define servlet mappings for central components such as JSP.
Servlet 3.0 introduced the concept of web fragments, which addresses your question about splitting the web.xml
into multiple files.These fragments can contain a portion (or all) of the web deployment descriptor by including a META-INF/web-fragment.xml
<web-fragment metadata-complete="true" version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd">
<name>fragment1</name>
<servlet>
<servlet-name>red</servlet-name>
<servlet-class>Test.Red</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>red</servlet-name>
<url-pattern>/blue</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>blue</servlet-name>
<servlet-class>Test.Blue</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>blue</servlet-name>
<url-pattern>/blue</url-pattern>
</servlet-mapping>
</web-fragment>
in web.xml
...
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>MyWelcomePage</servlet-name>
<servlet-class>MyWelcomePage</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyWelcomePage</servlet-name>
<url-pattern>/MyWelcomePage/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<absolute-ordering>
<name>fragment1</name>
</absolute-ordering>
</web-app>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With