Web. XML Configuration Introduction

Source: Internet
Author: User
Tags tld

This is not original, a little early, from where the specific has been remembered. But things are real.

1. When starting a Web project, the Web container will read its config file, XML, read and two nodes.

2, emergency, allow to create a ServletContext (servlet context), all parts of this Web project will share this context.

3, the container will be converted to a key value pair, and handed to ServletContext.

4. Create a listener for the class instance in the container creation.

Two Load-on-startup

The Load-on-startup element specifies the order in which the servlet is loaded when the Web application is launched, and its value must be an integer. If its value is a negative integer or the element does not exist, the container will load the servlet when the servlet is called. If the value is a positive integer or 0, the container loads and initializes the servlet when configured, and the container must ensure that the value is loaded first. If the values are equal, the container can automatically select who to load first.

In the servlet configuration, 5 means:

Flags whether the container loads the servlet when it is started.

A value of 0 or greater than 0 o'clock indicates that the container loads the servlet when the app is started;

When it is a negative number or is not specified, it indicates that the container is loaded only when the servlet is selected.

The lower the value of a positive number, the higher the priority to start the servlet.

Three-load order

The first thing to be sure is that the order of loading is independent of their order in the Web. xml file. That is, the filter is not loaded before the filter is written in front of the listener. The final conclusion is: ServletContext, listener, filter, servlet

    同时还存在着这样一种配置节:context-param,它用于向 ServletContext 提供键值对,即应用程序上下文信息。我们的 listener, filter 等在初始化时会用到这些上下文中的信息,那么 context-param 配置节是不是应该写在 listener 配置节前呢?实际上 context-param 配置节可写在任意位置,因此真正的加载顺序为:context-param -> listener -> filter -> servlet    对于某类配置节而言,与它们出现的顺序是有关的。以 filter 为例,web.xml 中当然可以定义多个 filter,与 filter 相关的一个配置节是 filter-mapping,这里一定要注意,对于拥有相同 filter-name 的 filter 和 filter-mapping 配置节而言,filter-mapping 必须出现在 filter 之后,否则当解析到 filter-mapping 时,它所对应的 filter-name 还未定义。web 容器启动时初始化每个 filter  时,是按照 filter 配置节出现的顺序来初始化的,当请求资源匹配多个 filter-mapping 时,filter 拦截资源是按照 filter-mapping 配置节出现的顺序来依次调用 doFilter() 方法的。    servlet 同 filter 类似,此处不再赘述。   由此,可以看出,web.xml 的加载顺序是:ServletContext -> context-param -> listener -> filter -> servlet ,而同个类型之间的实际程序调用的时候的顺序是根据对应的 mapping 的顺序进行调用的。

Four web. xml file detailed

I've collated the elements of Web. Xml I know:

Web. XML is first and foremost a schema that must contain it.

Copy Code
1 2 xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
4 version= "2.4" >
5
6
Copy Code
All the other elements are put in.

It's a description of the platform. Defines the name of the platform to specify whether the platform can be distributed

Used to set the environment parameter of the Web site, which contains two child elements: used to specify the name of the parameter used to set the parameter value. Like what:

1
2 My_param
3 Hello
4
The parameters set here can be obtained by using Getservletcontext (). Getinitparameter ("My_param") in the servlet.

is a related setting that declares the filter, which contains the following child elements:

This is, of course, the name of the filter.

This is the name of the class used to define the filter

Used to define a parameter, it has two child elements:

Used to specify the name of the parameter

Used to set parameter values.

Like what:

Copy Code
1
2 setcharacterencoding
3 Com.myTest.setCharacterEncodingFilter
4
5 encoding
6 GB2312
7
8
Copy Code
In conjunction with the URL used to define the filter, it has two child elements:

Specify the name of the filter

Specifies the URL to which the filter corresponds.

Like what:

1
2 setcharacterencoding
3/*
4
Used to set the listener interface, its primary child element is

Defines the class name of the listener

Like what:

1
2 Com.myTest.ContextListener
3
The data used to declare a servlet consists of the following child elements:

Specify the name of the servlet

Specifies the class name of the servlet

Specify the full path of a JSP Web page in the Web platform

Used to define the parameters, similar to the previous

Similarly, it is used with a URL that defines a servlet, containing two child elements:

Specify the name of the servlet

Specify the URL for the servlet

Like what:

Copy Code
1
2 Shoppingservlet
3 Com.myTest.ShoppingServlet
4
5
6
7 Shoppingservlet
8/shop/shoppingservlet
9
Copy Code
Used to define the session parameters in the Web platform, including a child element:

Used to define the expiration date for all sessions of this web site, in minutes

Defines an extension and a MIME type to be mapped, containing two child elements:

Name of the extension

MIME format

Like what:

Copy Code
1
2 doc
3 Application/vnd.ms-word
4
5
6 xls
7 Application/vnd.ms-excel
8
Copy Code
A list that defines the first page, containing a child element:

Specify the file name of the home page

Like what:

1
2 index.jsp
3 index.html
4
The page used to handle the error code or exception, with three child elements:

Specifying error codes

Specifying a Java exception type

Specify the associated resource path within the Web platform

Like what:

Copy Code
1
2 404
3/error404.jsp
4
5
6 java.lang.Exception
7/exception.jsp
8
Copy Code
The tag library path used to set the JSP Web page has two sub-elements:

Defines the URI of the TLD file, which can be obtained by using the taglib instruction in the JSP Web page

Specify where the TLD file is stored relative to the Web site

Like what:

1
2 Mytaglib
3/web-inf/tlds/mytaglib.tld
4
Defines the resources available to the platform using Jndi, with five child elements:

Resource Description

Resource Name

Types of resources

Resources are licensed through application or container.

Whether a resource can be shared, with shareable and unshareable two values, default to shareable

For example, the configuration database connection pool can be configured here:

Copy Code
1
2 JNDI JDBC DataSource of Shop
3 jdbc/sample_db
4 Javax.sql.DataSource
5 Container
6
Copy Code
Includes and two child elements.

Where the element already exists at JSP 1.2, it is the new element of JSP 2.0.

Elements have eight sub-elements, respectively:

1.: Description of the setting;

2.: Set the name;

3.: The range affected by the setting value, such as:/ch2 or/*.jsp;

4.: If true, indicates that El syntax is not supported;

5.: If true, indicates that <% scripting%> syntax is not supported;

6.: Set the encoding of JSP Web page;

7.: Set the header of the JSP Web page with the extension. JSPF;

8.: Set the end of the JSP Web page with the extension. JSPF.

A simple element is fully configured:

Copy Code
1
2
3 Taglib
4/web-inf/tlds/mytaglib.tld
5
6
7 Special Property Group for JSP Configuration JSP example.
8 Jspconfiguration
9/jsp/*
Ten True
GB2312
True
13/include/prelude.jspf
14/include/coda.jspf
15
16
Copy Code
Configure Web. XML to restrict requests to certain servlets

Sometimes we only want authenticated users to request certain servlets, which can be configured in Web. XML for this purpose.

This will use the element. For Tomcat, medium Web. XML uses the Security-constraint element to create a user name and password in an XML file located in/conf/tomcat-users.xml. For example, the following Tomcat-users.xml file:

Copy Code
1
2
3
4
5
6
7
8
9
Copy Code
This XML fragment includes a tomcat-users root element that contains one or more role and user elements.

The Security-constraint, Login-config, and security-role elements are then created in Web application XML.

Copy Code
1
2
3 HelloServlet
4/helloservlet
5 GET
6 POST
7
8
9 This applies "Tomcat" security role
Ten admin
11
12
NONE
14
15
16
17
BASIC
19
20
Admin
22
Copy Code
Where the Security-constraint element contains one or more web-resource-collection elements that describe which Web resources in the Web application are protected by the specified security restrictions. The Http-method element specifies the HTTP method that the security limit overrides. In the above example, the configuration security mechanism will be triggered when we make a GET or POST request to/helloservlet. The Auth-constraint element is used to describe the security roles that are allowed to access Web components. Examples of security roles in this example are Tomcat, manager, admin. HelloServlet can only be accessed by users who are part of the admin role.

The Web application authenticates the user with the Login-config element and confirms that the user is the correct role. Longin-config contains the Transport-guarantee child elements used to specify the authentication method, basic is a common way of web authentication, the browser prompts the User a dialog box, asking for a user name and password, Tomcat then compares the user name and password given with the user name and password in Tomcat-users.xml, and then uses the previous Security-constraint configuration to determine whether the user has access to the protected servlet.

(except basic, can also be form, Client-cert, digest, etc.)

In fact, this authentication method actually has two steps: 1, check whether the user name and password provided are correct. 2. Determine if the user is mapped to a specific security role. For example, a user may have provided the correct user name and password, but not mapped to a specific security role and will also be prevented from accessing a particular Web resource.

Five corresponding element configurations
1. Web App icon: Indicates large icons and small icons used by IDE and GUI tools to represent web apps

1
2/images/app_small.gif
3/images/app_large.gif
4
2. Web App Name: Provides a GUI tool that may be used to mark a name for this particular Web app
< Display-name>tomcat Example
3. Description of WEB application: Give descriptive text for this purpose
< Disciption>tomcat Example Servlets and JSP pages.
4. Context parameters: Declare the initialization parameters within the scope of the application.

1
2 Contextparameter
3 Test
4 It is a test parameter.
5
The servlet can be obtained by Getservletcontext () getinitparameter ("Context/param").

5. Filter configuration: Associates a name with a class that implements the Javaxs.servlet.Filter interface.

Copy Code
1
2 setcharacterencoding
3 Com.myTest.setCharacterEncodingFilter
4
5 encoding
6 GB2312
7
8
9
Ten setcharacterencoding
11/*
12
Copy Code
6. Listener Configuration

1
2 listener. Sessionlistener
3
7. servlet Configuration

Basic Configuration

Copy Code
1
2 Snoop
3 Snoopservlet
4
5
6 Snoop
7/snoop
8
Copy Code
Advanced Configuration

Copy Code
1
2 Snoop
3 Snoopservlet
4
5 foo
6 Bar
7
8
9 Security role for anonymous access
Ten Tomcat
11
12
13
Snoop
15/snoop
16
Copy Code
Element description

 <servlet></servlet> 用来声明一个servlet的数据,主要有以下子元素:     <servlet-name></servlet-name> 指定servlet的名称     <servlet-class></servlet-class> 指定servlet的类名称     <jsp-file></jsp-file> 指定web站台中的某个JSP网页的完整路径     <init-param></init-param> 用来定义参数,可有多个init-param。在servlet类中通过getInitParamenter(String name)方法访问初始化参数     <load-on-startup></load-on-startup>指定当Web应用启动时,装载Servlet的次序。                                 当值为正数或零时:Servlet容器先加载数值小的servlet,再依次加载其他数值大的servlet.                                 当值为负或未定义:Servlet容器将在Web客户首次访问这个servlet时加载它     <servlet-mapping></servlet-mapping> 用来定义servlet所对应的URL,包含两个子元素       <servlet-name></servlet-name> 指定servlet的名称       <url-pattern></url-pattern> 指定servlet所对应的URL    

8. Session Timeout configuration (in minutes)

1
2 120
3
9. MIME type configuration

1
2 htm
3 text/html
4
10. Specify the Welcome File page Configuration

1
2 index.jsp
3 index.html
4 index.htm
5
11. Configuration Error Page
First, the error code to configure the Error-page

1
2 404
3/notfound.jsp
4
The above is configured to jump to the error handling page notfound.jsp when the system has a 404 error.
Second, configure Error-page by the type of exception

1
2 java.lang.NullException
3/error.jsp
4
The above configuration jumps to the error handling page when the system is java.lang.NullException (that is, a null pointer exception) error.jsp
12. TLD Configuration

1
2 Http://jakarta.apache.org/tomcat/debug-taglib
3/web-inf/jsp/debug-taglib.tld
4
If MyEclipse has been an error, it should be put in the

Copy Code
1
2
3 Http://jakarta.apache.org/tomcat/debug-taglib
4/web-inf/pager-taglib.tld
5
6
Copy Code
13. Resource Management Object Configuration

1
2 Jms/stockqueue
3
14. Resource Factory Configuration

1
2 mail/session
3 javax.mail.Session
4 Container
5
The configuration database connection pool can be configured here:

Copy Code
1
2 JNDI JDBC DataSource of Shop
3 jdbc/sample_db
4 Javax.sql.DataSource
5 Container
6
Copy Code
15, security restrictions configuration

Copy Code
1
2 Example Security Constraint
3
4 Protected Area
5/jsp/security/protected/*
6 DELETE
7 GET
8 POST
9 PUT
10
11
Tomcat
Role1
14
15
Copy Code
16. Login Verification Configuration

Copy Code
1
2 FORM
3 example-based authentiation Area
4
5/jsp/security/protected/login.jsp
6/jsp/security/protected/error.jsp
7
8
Copy Code
17. Security role: The Security-role element gives a list of security roles that will appear in the Role-name child elements of the security-role-ref element within the servlet element. Declaring roles separately makes it easier for advanced Ides to handle security information.

1
2 Tomcat
3
18. Web Environment parameter: Env-entry element declares environment item for web App

1
2 minexemptions
3 1
4 Java.lang.Integer
5
19. EJB Declaration

Copy Code
1
2 Example EJB Reference
3 Ejb/account
4 Entity
5 Com.mycompany.mypackage.AccountHome
6 Com.mycompany.mypackage.Account
7
Copy Code
20. Local EJB Declaration

Copy Code
1
2 Example loacal EJB Reference
3 Ejb/processorder
4 Session
5 Com.mycompany.mypackage.ProcessOrderHome
6 Com.mycompany.mypackage.ProcessOrder
7
Copy Code
21. Configuring DWR

Copy Code
1
2 Dwr-invoker
3 Uk.ltd.getahead.dwr.DWRServlet
4
5
6 Dwr-invoker
7/dwr/*
8
Copy Code
22. Configuring Struts

Copy Code
1 Struts Blank application
2
3 Action
4
5 org.apache.struts.action.ActionServlet
6
7 Br>8 Detail
9 2

All
Debug
2
+

+ config
17/web-inf/struts-config.xml


Application
applicationresources
(
) 2
+

+ action
*.do
29
index.jsp


+

struts-bean
36/web-inf/tld/struts-bean.tld
PNs
>39 struts-html
40/web-inf/tld/struts-html.tld
(
)
struts-nested
44/web-inf/tld/ Struts-nested.tld

Struts-logic
48/web-inf/tld/struts-logic.tld
51
Struts-tiles
52/web-inf/tld/struts-tiles.tld
-
Copy Code
23, configure spring (basically configured in struts)

Copy Code
1
2
3 contextconfiglocation
4
5
6/web-inf/applicationcontext.xml,/web-inf/action-servlet.xml
7
8
9
10
11
12
Org.springframework.web.context.ContextLoaderListener
14
15
16
17
Org.springframework.web.context.request.RequestContextListener
19
20
Copy Code
Other Notes:

The Web. xml file typically includes the configuration of the servlet, spring, filter, Listenr. So what is the order in which they are loaded? The load order affects the call to the spring bean.
For example, the filter needs to use the bean, but the load order is to load the filter after loading spring, the filter in the initialization operation of the bean is null;
The first thing to be sure is that the order of loading is irrelevant to their order in the Web. xml file.
The load order of listener and Serverlet in Web. XML is first listener after Serverlet
The final result: listener first? Filter? Servlet? Spring
So, if you want to use the bean in the filter, you can change the load of spring to listener
1
2
3 Org.springframework.web.context.ContextLoaderListener
4
5
The order of their internal execution also needs to be noted, as is the problem encountered in the following article

Web. XML filter execution sequence caused by garbled, remember!
The cause of the bug is found in the following lines of Web. xml:
Copy Code
1
2 Securityfilter
3 . Do
4

5
6
7 characterencoding
8
. Do
9
10
11
Characterencoding
JSP
-
Copy Code
According to the servlet2.3 specification, the filter execution is executed in accordance with the filter-mapping sequence of the Web. XML configuration, so the above configuration will result
in a URL request to meet. Do, with the Securityfilter filter processed first, This time did not do code processing, is garbled, to the following filter processing has been garbled, and then do the encoding processing has no use.

Correction method, adjust the filter-mapping order, as follows:
Copy Code
1
2 characterencoding
3 . Do
4

5
6
7 characterencoding
8
. jsp
9
10
11
Securityfilter
*.do
14
Copy Code

Web. XML Configuration Introduction

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.