Web. XML load order and Web. XML Common Node parsing

Source: Internet
Author: User
Tags define session tld

Web. XML load Order

When the application server starts the Web. XML load process, as to the sequence of these nodes in the file, the order is not related, but some application servers, I have encountered the WebSphere strict requirements of Web. XML node order, otherwise the deployment is unsuccessful, So I'm still in favor of writing in the Web. XML standard format
Content-param--listener---a servlet

1. When you start a Web project, the application server reads its configuration file, XML. Read two nodes:<listener></listener> and <context-param></ context-param>   

2, immediately thereafter, the container creates a ServletContext (context) in which all parts of the Web project will share the context.

3, the container converts <context-param></context-param> into a key-value pair and gives it to ServletContext.

4, the container creates a class instance in <listener></listener>, that is, the listener is created.

5, in the listener will have contextinitialized (Servletcontextevent args) initialization method, obtained in this method:  
 servletcontext = Servletcontextevent.getservletcontext (); The value of
 context-param = Servletcontext.getinitparameter ("Key of Context-param");     

6 , after you get the value of this context-param, you can do something about it. Note that this time your Web project is not fully started yet. This action will be earlier than all the servlets. In other words, this time, you to <context-param The key values in > are executed before your Web project is fully booted. If you want to open the database before the project starts, you can set up the database connection in <context-param> and initialize the database connection in the Listener class. This listener is a class of its own, in addition to the initialization method, it also has the method of destroying. Frees the resource before closing the app. For example, the database connection is closed.


For a class of configuration sections, it is related to the order in which they appear.
In the case of filter, it is possible to define multiple filter in Web. XML, a configuration section related to filter is filter-mapping, and it is important to note that for filter and filter-mappin with the same filter-name In the case of the G configuration section, the filter-mapping must appear after the filter, or the corresponding filter-name is undefined when parsing to filter-mapping.
When each filter is initialized at the start of the Web container, it is initialized in the order in which the Filter configuration section appears, and when the request resource matches multiple filter-mapping, the filter interception resource is invoked in the order in which the Filter-mapping configuration section appears in sequence D The Ofilter () method.
The servlet is similar to filter and is not mentioned here.

For example, the filter needs to use the bean, but the loading order is: Load the filter after loading spring, the filter in the initialization operation of the bean is null, so if you want to use the filter in the bean, you can change the load of spring to The way of listener
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

Web. XML node parsing

Root node
1. <web-app></web-app>

Common Node Introduction
2.<context-param/>Used to set the environment parameters of the Web platform
It consists of two child elements:
<param-name></param-name> the name used to specify the parameter
<param-value></param-value> to set parameter values
The parameters set here can be obtained by using Getservletcontext (). Getinitparameter ("My_param") in the servlet.
Example:
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath*:/log4j.properties</param-value>
</context-param>

3.<listener/>Used to set the listener interface
Its primary child element is the
<listener-class></listener-class> defining class names for listener
Example:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

4.<filter/>is used to declare the relevant settings of the filter
<filter-name></filter-name> of course it's the name of the filter.
<filter-class></filter-class> This is the name of the class used to define the filter
<init-param></init-param> is used to define a parameter, which has two child elements:
<param-name></param-name> the name used to specify the parameter
<param-value></param-value> to set parameter values
Example:
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>

5.<servlet/>The data used to declare a servlet consists of the following child elements:
<servlet-name></servlet-name> Specify the name of the servlet
<servlet-class></servlet-class> specifying the class name of the servlet
<jsp-file></jsp-file> Specify the full path of a JSP Web page in the Web platform
<init-param></init-param> is used to define parameters, similar to the previous <init-param>
Similarly, it is used in conjunction with <servlet></servlet>
<servlet-mapping></servlet-mapping> is used to define a URL for a servlet that contains two child elements:
<servlet-name></servlet-name> Specify the name of the servlet
<url-pattern></url-pattern> Specify the URL for the servlet
<servlet>
<servlet-name>DemoServlet</servlet-name>
<servlet-class>com.test.DemoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DemoServlet</servlet-name>
<url-pattern>/demoServlet</url-pattern>
</servlet-mapping>

Base node:

6.<description/>It's a description of the platform.
Example:<description> preaching, tuition, doubts </description>

7.<display-name/>Define the name of the platform
Example:<display-name> My Site </display-name>

8.<icon>
The icon element contains the Small-icon and Large-icon two child elements. Used to specify the path to small icons and large icons on the Web platform.
<small-icon>/Path/smallicon.gif</small-icon>
The Small-icon element should point to the path of a small icon in the Web site, with a size of X pixel, but the image file must be in GIF or JPEG format, with the extension must be:. gif or. jpg.
<large-icon>/Path/largeicon-jpg</large-icon>
The Large-icon element should point to a large chart path in the Web site, with a size of X-pixel, but the image file must be in GIF or JPEG format and the extension must be; GIF or JPG.
Example:
<icon>
<small-icon>/images/small.gif</small-icon>
<large-icon>/images/large.gir</large-icon>
</icon>

9.<distributable/>is to specify whether the platform can be distributed for processing

10.<session-config/>Used to define session parameters in the Web platform
Contains a child element:
<session-timeout></session-timeout> is used to define the expiration date for all sessions of this web site, in minutes

11, <mime-mapping/> defines an extension and a certain MIME type to do the right should
Consists of two child elements:
<extension></extension> Name of extension
<mime-type></mime-type> MIME Format
Example:
<mime-mapping>
<extension>doc</extension>
<mime-type>application/vnd.ms-word</mime-type>
</mime-mapping>
<mime-mapping>
<extension>xls</extension>
<mime-type>application/vnd.ms-excel</mime-type>
</mime-mapping>

12.<error-page>
<error-page>
<error-code>500</error-code>
<location>/message.jsp</location>
</error-page>
<error-page>
<error-code>400</error-code>
<location>/message.jsp</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/message.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/message.jsp</location>
</error-page>
<error-page>
<error-code>502</error-code>
<location>/index.jsp</location>
</error-page>

13.<jsp-config/>
<jsp-config>
<taglib>
<taglib-uri>/struts-tags</taglib-uri>
<taglib-location>/WEB-INF/struts-tags.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/struts-dojo-tags</taglib-uri>
<taglib-location>/WEB-INF/struts-dojo-tags.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/s</taglib-uri>
<taglib-location>/WEB-INF/struts-tags.tld</taglib-location>
</taglib>
</jsp-config>

14.<welcome-file-list/>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

15, <resource-ref></resource-ref>Define the resources available to the platform using Jndi access
There are five child elements:
<description></description> Resource Description
<rec-ref-name></rec-ref-name> Resource Name
Types of <res-type></res-type> Resources
<res-auth></res-auth> resources licensed through application or container
<res-sharing-scope></res-sharing-scope> whether resources can be shared, with shareable and unshareable two values, default to shareable

For example, the configuration database connection pool can be configured here
<resource-ref>
<description>jndi JDBC DataSource of shop</description>
<res-ref-name>jdbc/sample_db</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

Web. XML load order and Web. XML Common Node parsing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.