Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...