Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat include another xml file to web.xml

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

like image 728
Wracker Avatar asked Sep 19 '14 07:09

Wracker


People also ask

Can we have multiple Web xml files?

Only one web. xml inside WEB-INF folder.

Can Web xml have multiple servlets?

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.

How many Web xml you can use file for an application?

It can be more than one or it is only one for the whole application.

What is Web xml file in Tomcat?

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.


1 Answers

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>
like image 79
Vipul Paralikar Avatar answered Oct 02 '22 20:10

Vipul Paralikar