Detailed explanation of configuration in web.xml

Citation:

For a programmer in the J2EE field, basically every day will deal with web applications.

What is a web application? What is the simplest web application? Given you a web application where do you start?

1. What is a web application?

      A web application is an application that can be accessed through the web. In the J2EE field, web applications are applications that comply with a series of standards based on JAVA technology.

2. What is the simplest web application?

      2 folders and 1 xml file can become a web application

      The first folder: application name, such as test

      Second folder: create a folder named WEB-INF in the test folder

      XML file: create a web.xml file in the WEB-INF folder, the content of the file only needs to be <web-app></web-app>

3. Given you a web application, where should you start?

      This is a matter of benevolent people seeing benevolent and wise seeing wisdom. I usually see that a web application starts from web.xmlSmile

text:

1. The scientific name of web.xml is the deployment descriptor file, which is defined in the Servlet specification and is the configuration file of the web application.

2. The deployment descriptor file, like all XML files, must start with an XML header. This header declares the XML version that can be used and gives the character encoding of the file. The DOCYTPE statement must appear immediately after this header. This declaration tells the server which version of the servlet specification is applicable (eg 2.2 or 2.3) and specifies the DTD (Document Type Definition) of the syntax that governs the content of the rest of the file. The top-level (root) element of all deployment descriptor files is web-app. Note that XML elements, unlike HTML, are case-sensitive. Therefore, both web-App and WEB-APP are illegal, and web-app must be lowercase.

      Sample web.xml:

[html]  view plain copy  
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">  
  3. <web-app>  
  4. </web-app>  


 3. XML elements in web.xml are not only case-sensitive, but they are also sensitive to the order in which they appear in other elements. For example, the XML header must be the first item in the file, the DOCTYPE declaration must be the second, and the web-app element must be the third. Within a web-app element, the order of elements is also important. Servers don't necessarily enforce this order, but they allow (and in fact some servers do) to completely refuse to execute Web applications that contain elements that are out of order. This means that web.xml files that use non-standard element ordering are not portable.

       Element order list:

      Element label details:

Element 1: <icon>

meaning

The icon element contains two child elements, small-icon and large-icon. It is used to specify the path of the small icon and large icon in the web site.
<small-icon>/path/smallicon.gif</small-icon> The
small-icon element should point to The path of a small icon in the web site, the size is 16 X 16 pixel, but the image file must be in GIF or JPEG format, and the extension must be: .gif or .jpg.

<large-icon>/path/largeicon-jpg</large-icon>
The large-icon element should point to a path to a large icon in the web site, with a size of 32 X 32 pixel, but the image file must be in GIF or JPEG format , the extension must be; gif or jpg.

example

<icon>
   <small-icon>/images/small.gif</small-icon>
   <large-icon>/images/large.gif</large-icon>
</icon>

元素2、3:<display-name>,<description>

meaning

<display-name>App Name</display-name>
defines the name of the app.

<description>Application description</discription>
Describes the application.

example

<display-name>test</display-name>

<description>Test Application V1.0</discription>

Element 4: <context-param>

meaning

The context-param element is used to set the environment parameters (context) of the web application. It contains two sub-elements:
param-name and param-value.
<param-name>parameter name</param-name>
set the context name
<param -value>value</param-value>
sets the value of the Context name

Example
<context-param>
   <param-name>param_name</param-name>
   <param-value>param_value</param-value>
</context-param>
The parameters set here can use the following in JSP pages Method to obtain:
${initParam.param_name}
If in Servlet, the following method can be used to obtain:
String param_name=getServletContext().getInitParamter("param_name");

元素5,6:<filter>,<filter-mapping>

meaning

The filter element is used to set the filter of the web application, and its two main sub-elements filter-name and filter-class are used to define the class corresponding to the Filter

<filter-name>Filter's name</filter-name>
Defines the Filter's name
<filter-class>Filter's class name</filter-class>
Defines the Filter's class name

The two main sub-elements of the filter-mapping element, filter-name and url-pattern, are used to define the URL corresponding to the Filter.
<filter-name>Filter name</filter-name>
Defines the name of the Filter.
<url-pattern >URL</url-pattern>
RUL corresponding to Filter. For example: <url-pattern>/Filter/*</url-pattern>

example

<filter>
  <filter-name>Encoding</filter-name>
  <filter-class>ghjf.test.filter.SetCharacterEncodingFilter</filter-class>
  <init-param>
     <param-name>encoding</param-name>
     <param-value>GBK</param-value>
  </init-param>
</filter>

<filter-mapping>
   <filter-name>Encoding</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>

Element 7: <listener>

meaning

The listener element is used to define the Listener interface, and its main sub-elements are <listener-class>
<listen-class>Listener's class name</listener-class>
Defines the Listener's class name
<listener>

example

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

元素8、9:<servlet>、<servlet-mapping>

meaning

The two main sub-elements of the servlet element, servlet-name and servlet-class, are used to define the class corresponding to the servlet.

<servlet-name>servlet name</servlet-name>
defines servlet name
<servlet-class>servlet class name</servlet-class>
defines servlet class name

The servlet-mapping element contains two sub-elements servlet-name and url-pattern. It is used to define the URL corresponding to the servlet.
<servlet-name>Servlet name</servlet-name>
defines the servlet name.
<url-pattern>Servlet URL </url-pattern>
defines the RUL corresponding to the Servlet. For example: <url-pattern>/Servlet/*</url-pattern>
</servlet-mapping>

example

<servlet>
 <servlet-name>dwr-invoker</servlet-name>
 <display-name>DWR Servlet</display-name>
 <description>Direct Web Remoter Servlet</description>
 <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
 <init-param>
     <param-name>debug</param-name>
     <param-value>true</param-value>
 </init-param>
</servlet>

<servlet-mapping>
 <servlet-name>dwr-invoker</servlet-name>
 <url-pattern>/dwr/*</url-pattern>
</servlet-mapping>

元素10:<session-cofing>

meaning

session-config contains a sub-element session-timeout. It defines the session parameters in the web application.
<session-timeout>minutes</session-timeout>
defines the validity period of all sessions of this web site. The unit is minutes.

example

<session-config>
   <session-timeout>30</session-timeout>
</session-config>

Element 11: <mime-mapping>

meaning

mime-mapping contains two sub-elements extension and mime-type. It defines a certain extension and a certain MIME Type for mapping.
<extension>extension name</extension>
extension name
<mime-type>MIME format</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>
<mime-mapping>
   <extension>ppt</extesnion>
   <mime-type>application/vnd.ms-powerpoint</mime-type>
</mime-mapping>

元素12:<welcome-file-list>

meaning

welcome-file-list contains a child element welcome-file. It is used to define the home page list.
<welcome-file> is used to specify the name of the home page file</welcome-flie>
welcome-file is used to specify the name of the home page file. We can use <welcome-file> specifies several home pages, and the server will find the home pages according to the set order.

example

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

元素13:<error-page>

meaning

The error-page element contains three sub-elements error-code, exception-type and location. The type of error code (Error Code) or exception (Exception) corresponds to the web application resource path.
<error-code>Error code</error- code>
HTTP Error code, for example: 404, 403
<exception-type>Exception</exception-type>
a Java exception type with a full name
<location>/path</location>
the relevant resource path in the web application

example

<error-page>
   <error-code>404</error-code>
   <location>/error404.jsp</location>
</error-page>
<error-page>
   <exception-type>java.lang.Exception</exception-type>
   <location>/exception.jsp</location>
</error-page>

元素14:<jsp-config>

meaning

The jsp-config element is mainly used to set the related configuration of JSP. <jsp:config> includes two sub-elements, <taglib> and <jsp-property-group>. The <taglib> element
already exists in JSP 1.2; <jsp-property-group> is a new element in JSP 2.0.

The <taglib>
taglib element contains two sub-elements taglib-uri and taglib-location. It is used to set the Tag Library path used by JSP web pages.
<taglib-uri>URI</taglib-uri>
   taglib-uri defines the URI of the TLD file , The taglib instruction of the JSP web page can access the TLD file through this URI.
<taglib-location>/WEB-INF/lib/xxx.tld</taglib-laction>
   The TLD file corresponds to the storage location of the Web site.
</taglib>

<jsp-property-group>
The jsp-property-group element contains 8 elements, which are:
<description>Description</descrition>
The description of this setting

<display-name>Name</display-name>
The name of this setting

<url-pattern>URL</url-pattern>
The scope affected by the setting value, such as: /CH2 or /*.jsp

<el-ignored>true|false</el-ignored>
If true, it means that EL syntax is not supported.

<scripting-invalid>true|false</scripting-invalid>
If true, the <%scription%> syntax is not supported.

<page-encoding>encoding</page-encoding>
Set the encoding of JSP pages

<include-prelude>.jspf</include-prelude>
set the header of JSP web page, the extension is .jspf

<include-coda>.jspf</include-coda>
Sets the end of the JSP page, with the extension .jspf
</jsp-property-group>
</jsp-config>

example

<jsp-config>
<taglib>
   <taglib-uri>Taglib</taglib-uri>
   <taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
</taglib>
<jsp-property-group>
   <description>
      Special property group for JSP Configuration JSP example.
   </description>
   <display-name>JSPConfiguration</display-name>
   <uri-pattern>/*</uri-pattern>
   <el-ignored>true</el-ignored>
   <page-encoding>GB2312</page-encoding>
   <scripting-inivalid>true</scripting-inivalid>
</jsp-property-group>
</jsp-config>

Element 15: <resource-env-ref>

meaning

resource-env-ref has two sub-elements:
<resource-env-ref-name>resource name</resource-env-ref-name>  
The name of the resource relative to java:comp/env
<resource-env-ref-type> The resource class name returned when searching for a resource</resource-env-ref-type>
The full name of the Java class name returned when the web application searches for the resource

example

 <resource-env-ref>  
  <resource-env-ref-name>jdbc/mssql</resource-env-ref-name>  
  <resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>  
 </resource-env-ref> 

Element 16: <resource-ref>

meaning

The resource-ref element includes five sub-elements description, res-ref-name, res-type, res-auth, and res-sharing-scope. Use JNDI to obtain the resources available to the application.
<description>Description</description>
Resource Description
<rec -ref-name>resource name</rec-ref-name>
resource name
<res-type>resource type</res-type>
resource type
<res-auth>Application|Container</res-auth>
resource is controlled by Application or Container allows
<res-sharing-scope>Shareable|Unshareable</res-sharing-scope>
 whether resources can be shared. The default value is Shareable

example

<resource-ref>
   <description>JNDI JDBC DataSource</description>
   <res-ref-name>jdbc/data</res-ref-name>
   <res-type>javax.sql.DataSoruce</res-type>
   <res-auth>Container</res-auth>
</resource-ref>

 

For specific articles, please refer to the following: https://blog.csdn.net/guihaijinfen/article/details/8363839

Please use your mobile phone to "scan" x

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325025165&siteId=291194637