Web. XML configuration file detailed

Source: Internet
Author: User
Tags root access

The author has been engaged in the development of online banking since graduating from university, which are some web development projects. The next step is to write about web development, and it's something that you often use since you work. This article begins with the Web. xml file, the author touches the project has this file, this file is the most basic and most important configuration files in the Tomcat project, the Tomcat startup project will load and read this file, so it is necessary to understand this file.

1. XML declaration and Root element

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE Web-app Public "-//sun Microsystems, INC.//DTD Web Application 2.3//en" "Http://java.sun.com/dtd/web-app_2_3. DTD "><web-app id=" WebApp ">......</web-app>

As with all XML types of files, the first line is an XML declaration that defines the version of the XML (1.0) and the encoding used (UTF-8).

The "DOCYTPE" declaration in the second line must immediately follow the XML declaration, which tells the server the version of the servlet specification (such as 2.3) that is applicable, and specifies a DTD that manages the syntax of the remainder of this file (document Type Definition is the document type definitions).

The third line of the <web-app></web-app> tag is the root element of this file, the Web. xml file's most important configuration information is included in this tag, the next highlight is what this tag contains. XML files are not only case sensitive, but also sensitive to the order in which they appear in other elements. So the case of the label must be used correctly, and the XML declaration has to be the first item in the file, the DOCTYPE declaration must be the second item, and the Web-app element must be the third item. Also within the Web-app element, the order of the elements is important.

2. Web application Configuration

<icon>       <small-icon>/images/small_icon.gif</small-icon>       <large-icon>/images/ Large_icon.gif</large-icon>  </icon><display-name>web</display-name><desciption >web example</desciption><context-param>     <param-name>name</para-name>     < Param-value>value</param-value>     <description>description</description></ Context-param>

The <icon> tab indicates the location of large icons and small icons used by the IDE and GUI tools to represent web apps.
The <display-name> tag indicates the app name for this web app.
<desciption> is, as the name implies, some descriptive information about this Web application.
<context-param> declares the initialization parameters in this application scope, which can be obtained in the servlet class through the Getservletcontext (). Getinitparameter ("Name") method.


3. Filter Filters

<filter>      <filter-name>MyFilter</filter-name>      <filter-class>com.filter.myfilter </filter-class>  </filter>  <filter-mapping>      <filter-name>myfilter</ filter-name>      <url-pattern>/main.jsp</url-pattern>     </filter-mapping>   < filter-mapping>      <filter-name>MyFilter</filter-name>      <url-pattern>/ Addproduct.jsp</url-pattern>     </filter-mapping>

<filter> Tag Internal Configuration Filter,<filter-name> label Specifies the name of this filter,<filter-class> label specifies the class to which this filter points (myfilter), This class must implement the Javax.servlet.Filter interface. The <filter-mapping> tag is used to correlate one or more servlet or JSP pages (/main.jsp and/addproduct.jsp). Note that no matter how many <filter-mapping> they have, their <filter-name> must be the same as the previous name.

4. Listener Listener

<listener>  <listener-class>com.channel.http.SessionListener</listener-class> </ Listener>

The <listener> tag specifies the listener, which listens to some objects in the Web application, creates, destroys, adds, modifies, deletes, and then responds accordingly. When the state of the scope object changes, the server automatically invokes the method in the Listener object. Often used to count the number of people online and online users, the system load to initialize information, statistics site visits and so on.

5. servlet (Very important)

<servlet>        <servlet-name>yqzl</servlet-name>        <servlet-class>            Com.channel.http.servlet.MainServlet        </servlet-class>        <init-param>            <param-name> Contextconfiglocation</param-name>            <param-value>/web-inf/config/springmvc-config.xml</ param-value>        </init-param>        <load-on-startup>1</load-on-startup>        </servlet >    <servlet-mapping>        <servlet-name>yqzl</servlet-name>        <url-pattern>* .action</url-pattern>    </servlet-mapping>

The <servlet> tag is used to specify the servlet-related configuration for this web app, and this configuration is very important. <servlet-name> tag Specifies the name of this servlet,<servlet-class> the class of the servlet, which can be written by the class developer, and generally inherits the HttpServlet class, Used to initialize the entire Web project and accept HTTP requests and process them. Some parameters can be configured in the <init-param> tag.
The <load-on-startup> tag specifies the order in which the servlet is loaded when the current web app starts, and its contents must be integers, and when this number >=0, the container is loaded from small to large by value. If the value is <0 or unspecified, the container loads the Servlet class the first time the user accesses it. The <servlet-mapping> tag defines the servlet map, and the <servlet-name> must be consistent with the previous name,<url-pattern> specifies the path to the servlet map.
When launching an entire Web project with Tomcat, when the <load-on-startup> tag is configured and the number >=0 inside, the servlet class is loaded, an instance of the class is created, and the Init () method is called to initialize the <init-param The configuration information in the > tag, which will only occur once throughout the servlet life cycle. If the <load-on-startup> label or digital <0 is not configured, Tomcat does not load this servlet class when it is started, and of course does not invoke the Init () method to initialize, and when the user first accesses it, the class is loaded and initialized. Therefore, the first visit may be slow to load at this time. Here is a brief description of the role of the tag, and I'll describe in detail the servlet class and several more important classes and interfaces associated with it.

6. Session Configuration

<session-config>  <session-timeout>10</session-timeout> </session-config>

The <session-config> tab is used to set the interval, in minutes, between the length of the session (interaction) between the browser and the server. When a user accesses a Web project, a session is established with the server, and when the user has not interacted with the server information for a long time, the server destroys the session, frees up the memory space, and expires when the user accesses the original session again. A new session needs to be re-established. In addition to setting this label, you can set the time-out value for a single Session object through the HttpSession Setmaxinactiveinterval () method.

7. Welcome Page Configuration

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

When a user accesses a web app, if only the root access URL of the Web app is given, and no specific file name is specified, the container invokes the file specified in the < weblcome-file-list > tag. <welcome-file-list> allows multiple <welcome-file> elements, each representing a single file. The container is searched in order, the first file is found, and if it does, the file is displayed and no more files are searched. If it does not exist, look for the second file, and so on. If none of the files are present, a 404 error is thrown.

8. Other Configurations

The following configuration, the author in the development of the present has not encountered, not particularly understand, here do not do too much explanation, so as not to cause errors. These are all references to Https://www.cnblogs.com/hafiz/p/5715523.html's content, and are interested to go to this blog to see.

<mime-mapping></mime-mapping>
  The mime-mapping element provides this assurance if the Web app has a particular file to think about and wants to ensure that they are assigned a specific MIME type.
<error-page></error-page>
  When a specific HTTP status code is returned, or when a particular type of exception is thrown, the page that will be displayed is made.
<taglib></taglib>
  Specifies an alias for the tag Library descriptor file (tag Libraryu descriptor files). This feature enables you to change the location of TLD files without editing the JSP pages that use those files.
<resource-env-ref></resource-env-ref>
  Declares a management object that is associated with a resource.
<resource-ref></resource-ref>
  Declares an external resource used by a resource factory.
<security-constraint></security-constraint>
  Develop URLs that should be protected. It is used in conjunction with the Login-config element.
<login-config></login-config>
  Specifies how the server should authorize users attempting to access a protected page. It is used in conjunction with the Sercurity-constraint element.
<security-role></security-role>
  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 the role separately to make it easier for the advanced IDE to handle the security information.
<env-entry></env-entry>
  Declares the environment item for the web App.
<ejb-ref></ejb-ref>
  Declares a reference to the home directory of an EJB.
<ejb-local-ref></ejb-local-ref>
  Declares an application for the local home directory of an EJB.

The above basically covers the elements required to configure the Web. xml file, as well as a simple introduction to these elements, which is the basic order of the different element configurations. including <display-name>, <desciption>, <context-param>,<filter>, <listener>, < Servlet>, <servlet-mapping>, <session-config>, <welcome-file-list> These are the author in the development process encountered the most, In particular, the configuration associated with Servlets is considered a priority. So in the next few sections I'll detail the servlet classes and several more important classes and interfaces associated with them. These content is also the author according to the actual work and reference some of the information summarized, of course, there are some mistakes or omissions, welcome to correct me, I also constantly improve learning.

The following is the full Web. xml file configuration.

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE Web-app Public "-//sun Microsystems, INC.//DTD Web Application 2.3//en" "Http://java.sun.com/dtd/web-app_2_3.         DTD "><web-app id=" WebApp "> <icon> <small-icon>/images/small_icon.gif</small-icon> <large-icon>/images/large_icon.gif</large-icon> </icon> <display-name>web</display- name> <desciption>web example</desciption> <context-param> <param-name>name</para -name> <param-value>value</param-value> <description>description</description> &L t;/context-param> <filter> <filter-name>MyFilter</filter-name> <filter-class>c om.filter.myfilter</filter-class> </filter> <filter-mapping> <filter-name>myfilter</ filter-name> <url-pattern>/main.jsp</url-pattern> </filter-mapping>  <filter-mapping> <filter-name>MyFilter</filter-name> <url-pattern>/addproduct.jsp& lt;/url-pattern> </filter-mapping> <listener> <listener-class>com.channel.http.sessionli       stener</listener-class> </listener> <servlet> <servlet-name>yqzl</servlet-name> <servlet-class> Com.channel.http.servlet.MainServlet </servlet-class> <init-para M> <param-name>contextConfigLocation</param-name> <param-value>/web-inf/config/        Springmvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>yqzl</servlet-name> <url-pat tern>*.action</url-pattern> </servlet-mapping> <session-config> <session-tim Eout>10</session-timeout> </session-config> <welcome-file-list> <welcome-file>index.html</welcome-f Ile> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file&gt        ; <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> & Lt;welcome-file>default.jsp</welcome-file> </welcome-file-list> <mime-mapping></mime-map ping> <error-page></error-page> <taglib></taglib> &LT;RESOURCE-ENV-REF&GT;&LT;/RESOURC E-env-ref> <resource-ref></resource-ref> <security-constraint></security-constraint> & Lt;login-config></login-config> <security-role></security-role> <env-entry></ Env-entry> <ejb-ref></ejb-ref> <ejb-local-ref></ejb-local-ref></web-app>
Zhuanzi http://www.cnblogs.com/Y-oung/p/8401549.html

Web. XML configuration file detailed

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.