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.xml微笑

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>

含义

error-page元素包含三个子元素error-code,exception-type和location.将错误代码(Error Code)或异常(Exception)的种类对应到web应用资源路径.
<error-code>错误代码</error-code>
HTTP Error code,例如: 404、403
<exception-type>Exception</exception-type>
一个完整名称的Java异常类型
<location>/路径</location>
在web应用内的相关资源路径

范例

<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>

含义

jsp-config元素主要用来设定JSP的相关配置,<jsp:config>包括<taglib>和<jsp-property-group>两个子元素.其中<taglib>元素
在JSP 1.2时就已经存在了;而<jsp-property-group>是JSP 2.0新增的元素.

<taglib>
taglib元素包含两个子元素taglib-uri和taglib-location.用来设定JSP网页用到的Tag Library路径.
<taglib-uri>URI</taglib-uri>
   taglib-uri定义TLD文件的URI,JSP网页的taglib指令可以经由这个URI存取到TLD文件.
<taglib-location>/WEB-INF/lib/xxx.tld</taglib-laction>
   TLD文件对应Web站台的存放位置.
</taglib>

<jsp-property-group>
jsp-property-group元素包含8个元素,分别为:
<description>Description</descrition>
此设定的说明

<display-name>Name</display-name>
此设定的名称

<url-pattern>URL</url-pattern>
设定值所影响的范围,如:/CH2 或者/*.jsp

<el-ignored>true|false</el-ignored>
若为true,表示不支持EL语法.

<scripting-invalid>true|false</scripting-invalid>
若为true表示不支持<%scription%>语法.

<page-encoding>encoding</page-encoding>
设定JSP网页的编码

<include-prelude>.jspf</include-prelude>
设置JSP网页的抬头,扩展名为.jspf

<include-coda>.jspf</include-coda>
设置JSP网页的结尾,扩展名为.jspf
</jsp-property-group>
</jsp-config>

范例

<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>

元素15:<resource-env-ref>

含义

resource-env-ref有两个子元素:
<resource-env-ref-name>资源名</resource-env-ref-name>  
资源的名称 相对于java:comp/env
<resource-env-ref-type>查找资源时返回的资源类名</resource-env-ref-type>
当web应用查找该资源的时候,返回的Java类名的全称

范例

 <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> 

元素16:<resource-ref>

含义

resource-ref元素包括五个子元素description,res-ref-name,res-type,res-auth,res-sharing-scope.利用JNDI取得应用可利用资源.
<description>说明</description>
资源说明
<rec-ref-name>资源名称</rec-ref-name>
资源名称
<res-type>资源种类</res-type>
资源种类
<res-auth>Application|Container</res-auth>
资源由Application或Container来许可
<res-sharing-scope>Shareable|Unshareable</res-sharing-scope>
 资源是否可以共享.默认值为 Shareable

范例

<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>

 

具体的文章可以参看如下:https://blog.csdn.net/guihaijinfen/article/details/8363839

请使用手机"扫一扫"x

Guess you like

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