The role and basic configuration of the web.xml file

The original text comes from: https://blog.csdn.net/cslbupt/article/details/60580782

What is the role of the web.xml file in the Java web project? Is it a must for every web project?

There can be no web.xml file in a web, that is to say, the web.xml file is not necessary for a web project.


When do you need it and when do you need it?

To answer the above question, you must first understand what the web.xml file is used for . The web.xml file is used to configure: welcome page, servlet, filter, etc. When your web project does not use these, you can configure your web project without the web.xml file.

 

So what are all the things web.xml can do?

In fact, how many kinds of label elements are defined in the schema file of web.xml, the label elements defined by its schema file can appear in web.xml, and it can have those functions defined. The schema file of web.xml is defined by Sun. The root element <web-app> of each web.xml file must indicate which schema file the web.xml uses. For example:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
 xmlns=" http://java.sun.com/xml/ns/javaee "
 xmlns:xsi =" http://www.w3.org/2001/XMLSchema-instance "
 xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ ns/javaee/web-app_2_5.xsd ">
</web-app>

    Moreover, the tags defined in the schema file of web.xml are not fixed, and the schema file can also be changed. Generally speaking, with the version upgrade of the web.mxl schema file, the functions defined in it will become more and more complicated, and the That is, there will be more and more types of label elements, but some are not commonly used, we only need to remember some commonly used ones.


    The following lists the commonly used tag elements in web.xml and the functions of these tag elements:

1. Specify the welcome page, for example:
<welcome-file-list>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index1.jsp</welcome-file>
  </welcome-file-list>
The above example specifies two welcome pages, and they are displayed in order from the first one. If the first one exists, the first one will be displayed, and the latter ones will not work. If the first one doesn't exist, find the second one, and so on.

 

About the welcome page:

    访问一个网站时,默认看到的第一个页面就叫欢迎页,一般情况下是由首页来充当欢迎页的。一般情况下,我们会在web.xml中指定欢迎页。但web.xml并不是一个Web的必要文件,没有web.xml,网站仍然是可以正常工作的。只不过网站的功能复杂起来后,web.xml的确有非常大用处,所以,默认创建的动态web工程在WEB-INF文件夹下面都有一个web.xml文件。
       当你只指定一个web的根名,没有指定具体页面,去访问时一个web时, 如果web.xml文件中配置了欢迎页,那么就返回指定的那个页面作为欢迎页,而在文中没有web.xml文件,或虽然有web.xml,但web.xml也没指定欢迎页的情况下,那么不同的应用服务器可能会有不同的行为,对于tomcat来说,它默认先查找index.html文件,如果找到了,就把index.html作为欢迎页还回给浏览器。如果没找到index.html,tomcat就去找index.jsp。找到index.jsp就把它作为欢迎页面返回。而如果index.html和index.jsp都没找到,又没有用web.xml文件指定欢迎页面,那此时tomcat就不知道该返回哪个文件了,它就显示The requested resource (/XXX) is not available的页面。其中XXX表示web的根名。但如果你指定了具体页面,是可以正常访问的。(如果web根名下存在index.html和index.jsp,而某些应用服务器在web.xml中没指定欢迎页的情况下默认先查找index.jsp的话,其行为跟tomcat就不一样了,因此可能造成没配置web.xml欢迎页的项目,部署到不同的应用服务器看到不一样的首页的现象)。

 

 

2、命名与定制URL。我们可以为Servlet和JSP文件命名并定制URL,其中定制URL是依赖一命名的,命名必须在定制URL前。下面拿serlet来举例:
(1)、为Servlet命名:
<servlet>
    <servlet-name>servlet1</servlet-name>
    <servlet-class>net.test.TestServlet</servlet-class>
</servlet>

(2)、为Servlet定制URL、
<servlet-mapping>
    <servlet-name>servlet1</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>


3、定制初始化参数:可以定制servlet、JSP、Context的初始化参数,然后可以再servlet、JSP、Context中获取这些参数值。下面拿servlet来举例:
<servlet>
    <servlet-name>servlet1</servlet-name>
    <servlet-class>net.test.TestServlet</servlet-class>
    <init-param>
          <param-name>userName</param-name>
          <param-value>Tommy</param-value>
    </init-param>
    <init-param>
          <param-name>E-mail</param-name>
          <param-value>[email protected]</param-value>
    </init-param>
</servlet>
经过上面的配置,在servlet中能够调用getServletConfig().getInitParameter("param1")获得参数名对应的值。

 

4、指定错误处理页面,可以通过“异常类型”或“错误码”来指定错误处理页面。
<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>

 

5、设置过滤器:比如设置一个编码过滤器,过滤所有资源
<filter>
    <filter-name>XXXCharaSetFilter</filter-name>
    <filter-class>net.test.CharSetFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>XXXCharaSetFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

 

6、设置监听器:
<listener>
<listener-class>net.test.XXXListener</listener-class>
</listener>

 

7、设置会话(Session)过期时间,其中时间以分钟为单位,假如设置60分钟超时:
<session-config>
<session-timeout>60</session-timeout>
</session-config>

除了这些标签元素之外,还可以往web.xml中添加那些标签元素呢,那些标签元素都能起什么作用呢?我们只要去查看web.xml的模式文件就能知道。直接看模式文件看不懂,可以找一些中文教程来看看。


下面再列出一些网友总结的web.xml常用配置元素:


1、Web应用图标:指出IDE和GUI工具用来表示Web应用的大图标和小图标    
<icon>    
<small-icon>/images/app_small.gif</small-icon>    
<large-icon>/images/app_large.gif</large-icon>    
</icon>
    
2、Web 应用名称:提供GUI工具可能会用来标记这个特定的Web应用的一个名称    
<display-name>Tomcat Example</display-name>  
  
3、Web 应用描述: 给出于此相关的说明性文本    
<disciption>Tomcat Example servlets and JSP pages.</disciption>  
  
4、上下文参数:声明应用范围内的初始化参数。    
<context-param>    
    <param-name>ContextParameter</para-name>    
    <param-value>test</param-value>    
    <description>It is a test parameter.</description>    
</context-param>    
在servlet里面可以通过getServletContext().getInitParameter("context/param")得到    


5、过滤器配置:将一个名字与一个实现javaxs.servlet.Filter接口的类相关联。    
<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>    
      <listerner-class>listener.SessionListener</listener-class>    
</listener>    


7、Servlet配置    
   基本配置    
   <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>    
   高级配置    
   <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>    
   元素说明    
     <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-config>    
      <session-timeout>120</session-timeout>    
   </session-config>  
   
9、MIME类型配置    
   <mime-mapping>    
      <extension>htm</extension>    
      <mime-type>text/html</mime-type>    
   </mime-mapping>    
   
10、指定欢迎文件页配置    
   <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、配置错误页面    
一、 通过错误码来配置error-page    
   <error-page>    
      <error-code>404</error-code>    
      <location>/NotFound.jsp</location>    
   </error-page>    
上面配置了当系统发生404错误时,跳转到错误处理页面NotFound.jsp。    
二、通过异常的类型配置error-page    
   <error-page>    
       <exception-type>java.lang.NullException</exception-type>    
       <location>/error.jsp</location>    
   </error-page>    
上面配置了当系统发生java.lang.NullException(即空指针异常)时,跳转到错误处理页面error.jsp  
  
12、TLD配置    
   <taglib>    
       <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>    
       <taglib-location>/WEB-INF/jsp/debug-taglib.tld</taglib-location>    
   </taglib>    
   如果MyEclipse一直在报错,应该把<taglib> 放到 <jsp-config>中    
   <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-env-ref>    
       <resource-env-ref-name>jms/StockQueue</resource-env-ref-name>    
   </resource-env-ref>  
   
14、资源工厂配置    
   <resource-ref>    
       <res-ref-name>mail/Session</res-ref-name>    
       <res-type>javax.mail.Session</res-type>    
       <res-auth>Container</res-auth>    
   </resource-ref>    
   配置数据库连接池就可在此配置:    
   <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-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>    
         <http-method>GET</http-method>    
         <http-method>POST</http-method>    
         <http-method>PUT</http-method>    
      </web-resource-collection>    
      <auth-constraint>    
        <role-name>tomcat</role-name>    
        <role-name>role1</role-name>    
      </auth-constraint>    
   </security-constraint> 
   
16、登陆验证配置    
   <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元素给出安全角色的一个列表,这些角色将出现在servlet元素内的security-role-ref元素的role-name子元素中。    
    分别地声明角色可使高级IDE处理安全信息更为容易。    
<security-role>    
     <role-name>tomcat</role-name>    
</security-role>    


18、Web环境参数:env-entry元素声明Web应用的环境项    
<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 声明    
<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</home>    
     <remote>com.mycompany.mypackage.Account</remote>    
</ejb-ref>    


20、本地EJB声明    
<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、配置DWR    
<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、配置Struts    
    <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、配置spring(基本上都是在Struts中配置的)    
   <!-- 指定spring配置文件位置 -->    
   <context-param>    
      <param-name>contextConfigLocation</param-name>    
      <param-value>    
       <!--加载多个spring配置文件 -->    
        /WEB-INF/applicationContext.xml, /WEB-INF/action-servlet.xml    
      </param-value>    
   </context-param>    


   <!-- 定义SPRING监听器,加载spring -->    


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


<listener>    
     <listener-class>    
       org.springframework.web.context.request.RequestContextListener    
     </listener-class>    

</listener>  

Myeclipse edits web.xml, and in design mode, you can see which elements can be edited in web.xml, which is very convenient, as shown below


Guess you like

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