Document directory
- Detailed description of the web. xml file
I,
1. When starting a WEB project, the WEB Container will read its configuration file web. xml and read the <context-param> and <listener> nodes.
2. Urgent: Create a ServletContext. All parts of the web project will share the context.
3. The Container converts <context-param> to a key-Value Pair and submits it to servletContext.
4. Create a class instance in the <listener> container and a listener.
II,
The load-on-startup element specifies the order in which the servlet is loaded when the web application starts. Its value must be an integer. If its value is a negative integer or the element does not exist, the container loads the servlet when the servlet is called. If the value is a positive integer or zero, the container loads and initializes the servlet during configuration. The container must first load the servlet if the value is small. If the values are equal, the container can automatically select who to load first.
In servlet configuration, <load-on-startup> 5 </load-on-startup> indicates:
Mark whether the servlet is loaded when the container is started.
When the value is 0 or greater than 0, the servlet is loaded when the application is started;
When it is a negative number or when it is not specified, it indicates that the container is loaded only when the servlet is selected.
The smaller the positive value, the higher the priority of starting the servlet.
III,
There will always be some loading priority problems in the project, and similar problems have been encountered recently, so I searched for the information and summarized some of the following, after all, people write well, so they will not duplicate the wheel, but just add some modifications.
First of all, it is certain that the order of loading is irrelevant to their order in the web. xml file. That is, the filter will not be loaded first because the filter is written before the listener. The final result
The conclusion is: listener-> filter-> servlet
There is also a configuration section: context-param, which is used to provide key-value pairs to ServletContext, that is, application context information. Our listener and filter will use the information in these contexts during initialization. Should the context-param configuration section be written before the listener Configuration section? In fact, the context-param configuration section can be written in any location, so the actual loading order is:
Context-param-> listener-> filter-> servlet
For certain configuration sections, the order in which they appear is related. Take filter as an example. of course, multiple filters can be defined in xml. One configuration section related to the filter is filter-mapping. Note that, in the filter and filter-mapping configuration sections with the same filter-name, the filter-mapping must appear after the filter; otherwise, when the filter-mapping is parsed, the filter-name corresponding to it is not defined yet. When a web container is started, each filter is initialized according to the sequence in the filter configuration section. When the requested resource matches multiple Filters-mapping, filter intercepts resources by calling the doFilter () method in sequence in the filter-mapping configuration section.
Servlet is similar to filter. We will not repeat it here.
From this, we can see that the web. the xml loading sequence is: context-param-> listener-> filter-> servlet, the actual Program Calling sequence between the same types is called according to the corresponding mapping Sequence.
Detailed description of the web. xml file
Common Web. xml elements
Copy codeThe Code is as follows: <web-app>
<Display-name> </display-name> defines the WEB application name.
<Description> </description> declares the WEB application description.
<Context-param> </context-param> the context-param element declares the initialization parameters in the application scope.
<Filter> </filter> the Filter element associates a name with a class that implements the javax. servlet. filter interface.
<Filter-mapping> </filter-mapping> once a filter is named, the filter-mapping element is used to associate it with one or more servlet or JSP pages.
<Listener> </listener> servlet API Version 2.3 adds support for event listening programs. The event listening program is notified when a session is created, modified, or deleted. The Listener element specifies the event Listener class.
<Servlet> </servlet> when initializing parameters or custom URLs to the servlet or JSP page, you must first name the servlet or JSP page. The Servlet element is used to complete this task.
<Servlet-mapping> </servlet-mapping> the server generally provides the servlet with a default URL: http: // host/webAppPrefix/servlet/ServletName. However, this URL is often changed so that the servlet can access initialization parameters or process relative URLs more easily. When you change the default URL, use the servlet-mapping element.
<Session-config> </session-config> If a session is not accessed for a certain period of time, the server can discard it to save memory. You can use the setMaxInactiveInterval method of HttpSession to explicitly set the timeout value for a single session object, or use the session-config element to specify the default timeout value.
<Mime-mapping> </mime-mapping> If a Web application wants to assign a specific MIME type to a special file, the mime-mapping element provides this guarantee.
<Welcome-file-list> </welcome-file-list> indicates the file used by the server when the server receives a URL that references a directory name rather than a file name.
<Error-page> </error-page> You can specify the page to be displayed when a specific HTTP status code is returned or an exception of a specific type is thrown.
<Taglib> </taglib> specifies an alias for the Tag library Descriptor file (Tag Libraryu Descriptor file. This feature allows you to change the location of TLD files without editing the JSP pages that use these files.
<Resource-env-ref> </resource-env-ref> declares a resource-related management object.
<Resource-ref> </resource-ref> declares the external resources used by a resource factory.
<Security-constraint> </security-constraint> specifies the URL to be protected. It is used together with the login-config element.
<Login-config> </login-config> specifies how the server grants permissions to users attempting to access protected pages. It is used with the sercurity-constraint element.
<Security-role> </security-role> provides a list of security roles that appear in the role-name sub-element of the security-role-ref element in the servlet element. Declaring roles separately makes it easier for advanced IDE to process security information.
<Env-entry> </env-entry> declares the environment items of the Web application.
<Ejb-ref> </ejb-ref> declares the reference of the main directory of an EJB.
<Ejb-local-ref> </ejb-local-ref> declares the application of the local main directory of an EJB.
</Web-app>
Element configuration
1. Web application icons: The IDE and GUI tools are used to indicate the large and small icons of Web applications.
Copy codeThe Code is as follows: <icon>
<Small-icon>/images/app_small.gif </small-icon>
<Large-icon>/images/app_large.gif </large-icon>
</Icon>
2. Web Application name: a GUI tool provided may be used to mark a name of a specific Web application.Copy codeThe Code is as follows: <display-name> Tomcat Example </display-name>
3. Web application Description: This descriptive text is provided.Copy codeThe Code is as follows: <disciption> Tomcat Example servlets and JSP pages. </disciption>
4. context parameters: declare the initialization parameters within the application scope.Copy codeThe Code is as follows: <context-param>
<Param-name> ContextParameter </para-name>
<Param-value> test </param-value>
<Description> It is a test parameter. </description>
</Context-param>
In the servlet, you can use getServletContext (). getInitParameter ("context/param") to obtain
5. Filter configuration: associate a name with a class that implements the javaxs. servlet. Filter interface.Copy codeThe Code is as follows: <filter>
<Filter-name> setCharacterEncoding </filter-name>
<Filter-class> com. myTest. setCharacterEncodingFilter </filter-class>
<Init-param>
<Param-name> encoding </param-name>
<Param-value> GB2312 </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-name> setCharacterEncoding </filter-name>
<Url-pattern>/* </url-pattern>
</Filter-mapping>
6. Listener ConfigurationCopy codeThe Code is as follows: <listener>
<Listerner-class> listener. SessionListener </listener-class>
</Listener>
7. Servlet Configuration
Basic ConfigurationCopy codeThe Code is as follows: <servlet>
<Servlet-name> snoop </servlet-name>
<Servlet-class> SnoopServlet </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> snoop </servlet-name>
<Url-pattern>/snoop </url-pattern>
</Servlet-mapping>
Advanced ConfigurationCopy codeThe Code is as follows: <servlet>
<Servlet-name> snoop </servlet-name>
<Servlet-class> SnoopServlet </servlet-class>
<Init-param>
<Param-name> foo </param-name>
<Param-value> bar </param-value>
</Init-param>
<Run-as>
<Description> Security role for anonymous access </description>
<Role-name> tomcat </role-name>
</Run-as>
</Servlet>
<Servlet-mapping>
<Servlet-name> snoop </servlet-name>
<Url-pattern>/snoop </url-pattern>
</Servlet-mapping>
Element description
<Servlet> </servlet> is used to declare the data of a servlet, mainly including the following sub-elements:
<Servlet-name> </servlet-name> specifies the servlet name.
<Servlet-class> </servlet-class> specifies the servlet class name.
<Jsp-file> </jsp-file> specifies the complete path of a JSP page on the web platform.
<Init-param> </init-param> is used to define parameters. Multiple init-param parameters are allowed. Access the initialization parameters through the getInitParamenter (String name) method in the servlet class.
<Load-on-startup> </load-on-startup> specifies the Servlet loading sequence when the Web application starts.
When the value is positive or zero: The Servlet container first loads the servlet with a small value, and then loads the servlet with a large value in sequence.
The value is negative or undefined: The Servlet container will load it when the Web Client first accesses this servlet.
<Servlet-mapping> </servlet-mapping> defines the URL of the servlet, which contains two child elements.
<Servlet-name> </servlet-name> specifies the servlet name.
<Url-pattern> </url-pattern> specifies the URL of the servlet.
8. Session Timeout configuration (in minutes)Copy codeThe Code is as follows: <session-config>
<Session-Time Out> 120 </session-timeout>
</Session-config>
9. MIME Type ConfigurationCopy codeThe Code is as follows: <mime-mapping>
<Extension> htm </extension>
<Mime-type> text/html </mime-type>
</Mime-mapping>
10. Specify the welcome file page configurationCopy codeThe Code is as follows: <welcome-file-list>
<Welcome-file> index. jsp </welcome-file>
<Welcome-file> index.html </welcome-file>
<Welcome-file> index.htm </welcome-file>
</Welcome-file-list>
11. configuration error page
1. Configure error-page through error codesCopy codeThe Code is as follows: <error-page>
<Error-code> 404 </error-code>
<Location>/NotFound. jsp </location>
</Error-page>
The above configuration jumps to the error handling page NotFound. jsp when a 404 error occurs in the system.
2. Configure error-page through the exception typeCopy codeThe Code is as follows: <error-page>
<Exception-type> java. lang. NullException </exception-type>
<Location>/error. jsp </location>
</Error-page>
The above configuration jumps to the error handling page error. jsp when java. lang. NullException (NULL pointer exception) occurs in the system.
12. TLD ConfigurationCopy codeThe Code is as follows: <taglib>
<Taglib-uri> http://jakarta.apache.org/tomcat/debug-taglib </taglib-uri>
<Taglib-location>/WEB-INF/jsp/debug-taglib.tld </taglib-location>
</Taglib>
If MyEclipse keeps reporting errors, put <taglib> in <jsp-config>.
View sourceCopy codeThe Code is as follows: <jsp-config>
<Taglib>
<Taglib-uri> http://jakarta.apache.org/tomcat/debug-taglib </taglib-uri>
<Taglib-location>/WEB-INF/pager-taglib.tld </taglib-location>
</Taglib>
</Jsp-config>
13. Resource Management object ConfigurationCopy codeThe Code is as follows: <resource-env-ref>
<Resource-env-ref-name> jms/StockQueue </resource-env-ref-name>
</Resource-env-ref>
14. Resource factory ConfigurationCopy codeThe Code is as follows: <resource-ref>
<Res-ref-name> mail/Session </res-ref-name>
<Res-type> javax. mail. Session </res-type>
<Res-auth> Container </res-auth>
</Resource-ref>
You can configure the database connection pool here:Copy codeThe Code is as follows: <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>
15. Security Restriction ConfigurationCopy codeThe Code is as follows: <security-constraint>
<Display-name> Example Security Constraint </display-name>
<Web-resource-collection>
<Web-resource-name> Protected Area </web-resource-name>
<Url-pattern>/jsp/security/protected/* </url-pattern>
<Http-method> DELETE <Http-method> GET <Http-method> POST <Http-method> PUT </Web-resource-collection>
<Auth-constraint>
<Role-name> tomcat </role-name>
<Role-name> role1 </role-name>
</Auth-constraint>
</Security-constraint>
16. login verification ConfigurationCopy codeThe Code is as follows: <login-config>
<Auth-method> FORM </auth-method>
<Realm-name> Example-Based Authentiation Area </realm-name>
<Form-login-config>
<Form-login-page>/jsp/security/protected/login. jsp </form-login-page>
<Form-error-page>/jsp/security/protected/error. jsp </form-error-page>
</Form-login-config>
</Login-config>
17. security role: the security-role element provides a list of security roles that appear in the role-name sub-element of the security-role-ref element in the servlet element.
Declaring roles separately makes it easier for advanced IDE to process security information.Copy codeThe Code is as follows: <security-role>
<Role-name> tomcat </role-name>
</Security-role>
18. Web environment parameters: the env-entry element declares the environment items of the Web application.Copy codeThe Code is as follows: <env-entry>
<Env-entry-name> minExemptions </env-entry-name>
<Env-entry-value> 1 </env-entry-value>
<Env-entry-type> java. lang. Integer </env-entry-type>
</Env-entry>
19. EJB DeclarationCopy codeThe Code is as follows: <ejb-ref>
<Description> Example EJB reference </decription>
<Ejb-ref-name> ejb/Account </ejb-ref-name>
<Ejb-ref-type> Entity </ejb-ref-type>
<Home> com. mycompany. mypackage. AccountHome <Remote> com. mycompany. mypackage. Account </remote>
</Ejb-ref>
20. Local EJB DeclarationCopy codeThe Code is as follows: <ejb-local-ref>
<Description> Example Loacal EJB reference </decription>
<Ejb-ref-name> ejb/ProcessOrder </ejb-ref-name>
<Ejb-ref-type> Session </ejb-ref-type>
<Local-home> com. mycompany. mypackage. ProcessOrderHome </local-home>
<Local> com. mycompany. mypackage. ProcessOrder </local>
</Ejb-local-ref>
21. Configure DWRCopy codeThe Code is as follows: <servlet>
<Servlet-name> dwr-invoker </servlet-name>
<Servlet-class> uk. ltd. getahead. dwr. DWRServlet </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> dwr-invoker </servlet-name>
<Url-pattern>/dwr/* </url-pattern>
</Servlet-mapping>
22. Configure StrutsCopy codeThe Code is as follows: <display-name> Struts Blank Application </display-name>
<Servlet>
<Servlet-name> action </servlet-name>
<Servlet-class> org. apache. struts. action. ActionServlet </servlet-class>
<Init-param>
<Param-name> detail </param-name>
<Param-value> 2 </param-value>
</Init-param>
<Init-param>
<Param-name> debug </param-name>
<Param-value> 2 </param-value>
</Init-param>
<Init-param>
<Param-name> config </param-name>
<Param-value>/WEB-INF/struts-config.xml </param-value>
</Init-param>
<Init-param>
<Param-name> application </param-name>
<Param-value> ApplicationResources </param-value>
</Init-param>
<Load-on-startup> 2 </load-on-startup>
</Servlet>
<Servlet-mapping>
<Servlet-name> action </servlet-name>
<Url-pattern> *. do </url-pattern>
</Servlet-mapping>
<Welcome-file-list>
<Welcome-file> index. jsp </welcome-file>
</Welcome-file-list>
<! -- Struts Tag Library Descriptors -->
<Taglib>
<Taglib-uri> struts-bean </taglib-uri>
<Taglib-location>/WEB-INF/tld/struts-bean.tld </taglib-location>
</Taglib>
<Taglib>
<Taglib-uri> struts-html </taglib-uri>
<Taglib-location>/WEB-INF/tld/struts-html.tld </taglib-location>
</Taglib>
<Taglib>
<Taglib-uri> struts-nested </taglib-uri>
<Taglib-location>/WEB-INF/tld/struts-nested.tld </taglib-location>
</Taglib>
<Taglib>
<Taglib-uri> struts-logic </taglib-uri>
<Taglib-location>/WEB-INF/tld/struts-logic.tld </taglib-location>
</Taglib>
<Taglib>
<Taglib-uri> struts-tiles </taglib-uri>
<Taglib-location>/WEB-INF/tld/struts-tiles.tld </taglib-location>
</Taglib>
23. Configure Spring (basically all configured in Struts)Copy codeThe Code is as follows: <! -- Specify the location of the spring configuration file -->
<Context-param>
<Param-name> contextConfigLocation </param-name>
<Param-value>
<! -- Load Multiple spring configuration files -->
/WEB-INF/applicationContext. xml,/WEB-INF/action-servlet.xml
</Param-value>
</Context-param>
<! -- Define the SPRING listener and load spring -->
<Listener>
<Listener-class> org. springframework. web. context. ContextLoaderListener </listener-class>
</Listener>
<Listener>
<Listener-class> org. springframework. web. context. request. RequestContextListener </listener-class>
</Listener>