工作生活Spring-webmvc

web.xml

2019-07-06  本文已影响0人  xzz4632
1. <web-app>

web-app元素是web应用程序部署描述符的根元素。这个元素的子元素可以是任意顺序的。

2. <display-name>

定义web应用的名称

3. <distributable>

告诉servlet/JSP容器,Web容器中部署的应用程序适合在分布式环境下运行。

4. <context-param>

上下文初始化参数.含有一对参数名和参数值,用作应用的Servlet上下文初始化参数,参数名在整个Web应用中必须是惟一的,在web应用的整个生命周期中上下文初始化参数都存在,任意的Servlet和jsp都可以随时随地访问它。

子元素:

spring配置文件
配置Spring,必须需要<listener>,而<context-param>可有可无,如果在web.xml中不写<context-param>配置信息,默认的路径是/WEB-INF/applicationontext.xml,在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml。如果是要自定义文件名可以在web.xml里加入contextConfigLocation这个context参数; 在<param-value>
</param-value>里指定相应的xml文件名,如果有多个xml文件,可以写在一起并以“,”号分隔:

<context-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>/WEB-INF/spring-configuration/*.xml</param-value>  
</context-param>  
<listener>  
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
</listener>

在不同环境下如何获取

${initParam.param_name}
String param_name=request.getServletContext().getInitParamter("param_name");

Servlet的ServletConfig对象拥有该Servlet的ServletContext的一个引用,所以可这样取得上下文初始化参数, getServletConfig().getServletContext().getInitParameter(), 也可以在Servlet中直接调用getServletContext().getInitParameter(),两者是等价的。

5. <session-config>

设置容器的session参数.

子元素

6. <listener>

为web应用程序定义监听器,用来监听各种事件,比如:application和session事件,所有的监听器按照相同的方式定义,功能取决去它们各自实现的接口,常用的Web事件接口有如下几个:

配置
配置Listener只要向Web应用注册Listener实现类即可,无序配置参数之类的东西,因为Listener获取的是Web应用ServletContext(application)的配置参数。为Web应用配置Listener的两种方式:

子元素

7. <filter>

过滤器, 其功能是对用户请求request进行预处理,对Response进行后处理.使用Filter的完整流程是:Filter对用户请求进行预处理,接着将请求HttpServletRequest交给Servlet进行处理并生成响应,最后Filter再对服务器响应HttpServletResponse进行后处理。Filter与Servlet具有完全相同的生命周期,且Filter也可以通过<init-param>来配置初始化参数,获取Filter的初始化参数则使用FilterConfig的getInitParameter()。

配置
Filter必须实现javax.servlet.Filter接口.在该接口中定义了三个方法:

子元素

<filter-mapping>
指定Filter所拦截的URL。必须与其对应的<filter>具有相同的<filter-name>.

常用过滤器

8. <servlet>

Servlet是个特殊的java类,继承于javax.servlet.http.HttpServlet.

子元素

<servlet-mapping>
含有<servlet-name>和<url-pattern>, 指定servlet所处理的请求路径.

9. <welcome-file-list>

包含一个子元素<welcome-file>,<welcome-file>用来指定首页文件名称。可以包含一个或多个<welcome-file>子元素。如果在第一个<welcome-file>元素中没有找到指定的文件,Web容器就会尝试显示第二个,以此类推。

9. <env-entry>

定义JNDI环境变量
子元素

附: web.xml示例

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" 
    version="2.4"> 

    <!-- ========================================================== --> 
    <!-- General --> 
    <!-- ========================================================== --> 

    <!-- 定义应用名称 --> 
    <display-name>Example App</display-name> 
    <description>一个示例应用程序,适用于处理Tomcat的一些特性</description> 

    <!-- 是否支持分布式 --> 
    <distributable /> 

    <!-- session超时时间 --> 
    <session-config> 
        <session-timeout>120</session-timeout> 
    </session-config> 


    <!-- ========================================================== --> 
    <!-- 自定义标签库 --> 
    <!-- ========================================================== --> 

    <!-- 自JSP 2.0以来不再需要Taglib声明,请参见从web.xml中删除Taglib --> 
    <!-- 注意,每个web.xml只能有一个<jsp-config>元素 --> 

    <!-- ========================================================== --> 
    <!-- JSP 配置 --> 
    <!-- ========================================================== --> 

    <jsp-config> 
        <jsp-property-group> 
            <url-pattern>*.jsp</url-pattern> 
            <include-prelude>/WEB-INF/jspf/prelude1.jspf</include-prelude> 
            <include-coda>/WEB-INF/jspf/coda1.jspf</include-coda> 
        </jsp-property-group> 
    </jsp-config> 


    <!-- ========================================================== --> 
    <!-- Context 参数 --> 
    <!-- ========================================================== --> 

    <context-param> 
        <description>Enable debugging for the application</description> 
        <param-name>debug</param-name> 
        <param-value>true</param-value> 
    </context-param> 
    <context-param> 
        <description>The email address of the administrator, used to send error reports.</description> 
        <param-name>webmaster</param-name> 
        <param-value>address@somedomain.com</param-value> 
    </context-param> 


    <!-- ========================================================== --> 
    <!-- JNDI 环境变量 --> 
    <!-- ========================================================== --> 

    <env-entry> 
        <env-entry-name>webmasterName</env-entry-name> 
        <env-entry-value>Ms. W. Master</env-entry-value> 
        <env-entry-type>java.lang.String</env-entry-type> 
    </env-entry> 
    <env-entry> 
        <env-entry-name>cms/defaultUserSettings/recordsPerPage</env-entry-name> 
        <env-entry-value>30</env-entry-value> 
        <env-entry-type>java.lang.Integer</env-entry-type> 
    </env-entry> 
    <env-entry> 
        <env-entry-name>cms/enableXMLExport</env-entry-name> 
        <env-entry-value>false</env-entry-value> 
        <env-entry-type>java.lang.Boolean</env-entry-type> 
    </env-entry> 
    <env-entry> 
        <env-entry-name>cms/enableEmailNotifications</env-entry-name> 
        <env-entry-value>true</env-entry-value> 
        <env-entry-type>java.lang.Boolean</env-entry-type> 
    </env-entry> 


    <!-- ========================================================== --> 
    <!-- Servlets --> 
    <!-- ========================================================== --> 

    <!-- 简单 Servlet, provide a name, class, description and map to URL /servlet/SimpleServlet --> 
    <servlet> 
        <servlet-name>Simple</servlet-name> 
        <servlet-class>SimpleServlet</servlet-class> 
        <description>This is a simple Hello World servlet</description> 
    </servlet> 
    <servlet-mapping> 
        <servlet-name>Simple</servlet-name> 
        <url-pattern>/servlet/SimpleServlet</url-pattern> 
    </servlet-mapping> 

    <!-- CMS Servlet, responds to *.cms URL's --> 
    <servlet> 
        <!-- Identification --> 
        <servlet-name>cms</servlet-name> 
        <servlet-class>com.metawerx.servlets.ContentManagementSystem</servlet-class> 
        <description>This servlet handles requests for the CMS (it is a controller in an MVC architecture)</description> 

        <!-- This servlet has two parameters --> 
        <init-param> 
            <param-name>debug</param-name> 
            <param-value>true</param-value> 
        </init-param> 
        <init-param> 
            <param-name>detail</param-name> 
            <param-value>2</param-value> 
        </init-param> 

        <!-- 容器启动时加载servlet (调用这个servlet的init()) --> 
        <load-on-startup>5</load-on-startup> 
        <!-- <run-at>0:00, 6:00, 12:00, 18:00</run-at> 这个标签仅在使用Resin服务器时有效 --> 
    </servlet> 

    <!-- 映射一些url到cms Servlet (demonstrates *.extension mapping) -->
    <servlet-mapping> 
        <!-- cms servlet将会处理任何以.cms结尾的url --> 
        <servlet-name>cms</servlet-name> 
        <url-pattern>*.cms</url-pattern> 
    </servlet-mapping> 

    <!-- Rewriter Servlet, responds to /content/* and /admin/RewriterStatistics URL's --> 
    <!-- Define a servlet to respond to /content/* URL's --> 
    <servlet> 
        <servlet-name>rewriter</servlet-name> 
        <servlet-class>com.metawerx.servlets.URLRewriter</servlet-class> 
    </servlet> 

    <!-- Map some URL's to the rewriter servlet (demonstrates /path/* and specific URL mapping) --> 
    <servlet-mapping> 
        <!-- For any URL starting with /content/, the rewriter servlet will be called --> 
        <servlet-name>rewriter</servlet-name> 
        <url-pattern>/content/*</url-pattern> 
    </servlet-mapping> 
    <servlet-mapping> 
        <!-- The rewriter servlet can also be called directly as /admin/RewriterStatistics, to return stats --> 
        <servlet-name>rewriter</servlet-name> 
        <url-pattern>/admin/RewriterStatistics</url-pattern> 
    </servlet-mapping> 

    <!-- PathJSP Servlet, maps /shop/item/* URL's to a JSP file --> 
    <!-- Define a JSP file to respond to /shop/item/* URL's --> 
    <servlet> 
        <servlet-name>pathjsp</servlet-name> 
        <jsp-file>pathfinder.jsp</jsp-file> 
    </servlet> 

    <!-- Map some URL's to the pathjsp servlet (demonstrates /long/path/* URL mapping) --> 
    <servlet-mapping> 
        <!-- For any URL starting with /shop/item/, the pathjsp servlet will be called --> 
        <servlet-name>pathjsp</servlet-name> 
        <url-pattern>/shop/item/*</url-pattern> 
    </servlet-mapping> 


    <!-- ========================================================== --> 
    <!-- 过滤器 --> 
    <!-- ========================================================== --> 

    <!-- Example filter to set character encoding on each request (from Tomcat servlets-examples context) --> 
    <filter> 
        <filter-name>Set Character Encoding</filter-name> 
        <filter-class>filters.SetCharacterEncodingFilter</filter-class> 
        <init-param> 
            <param-name>encoding</param-name> 
            <param-value>EUC_JP</param-value> 
        </init-param> 
    </filter> 
    <filter-mapping> 
        <filter-name>Set Character Encoding</filter-name> 
        <url-pattern>/*</url-pattern> 
    </filter-mapping> 

    <!-- Example filter to dump the HTTP request at the top of each page (from Tomcat servlets-examples context) --> 
    <filter> 
        <filter-name>Request Dumper Filter</filter-name> 
        <filter-class>filters.RequestDumperFilter</filter-class> 
    </filter> 
    <filter-mapping> 
        <filter-name>Request Dumper Filter</filter-name> 
        <url-pattern>/*</url-pattern> 
    </filter-mapping> 


    <!-- ========================================================== --> 
    <!-- 监听器 --> 
    <!-- ========================================================== --> 

    <!-- Define example application events listeners --> 
    <listener> 
        <listener-class>com.metawerx.listener.ContextListener</listener-class> 
    </listener> 
    <listener> 
        <listener-class>com.metawerx.listener.SessionListener</listener-class> 
    </listener> 


    <!-- ========================================================== --> 
    <!-- Security --> 
    <!-- ========================================================== --> 

    <!-- 定义角色 --> 
    <security-role> 
        <role-name>admin</role-name> 
    </security-role> 
    <security-role> 
        <role-name>cms_editors</role-name> 
    </security-role> 
     
    <!-- Define a constraint to restrict access to /private/* --> 
    <security-constraint> 

        <display-name>Security constraint for the /private folder</display-name> 

        <web-resource-collection> 
                 
            <web-resource-name>Protected Area</web-resource-name> 
            <url-pattern>/private/*</url-pattern> 
             
            <!-- If you list http methods, only those methods are protected. --> 
            <!-- Leave this commented out to protect all access --> 
            <!-- 
            <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> 
            <!-- Only only administrator and CMS editors to access this area --> 
            <role-name>admin</role-name> 
            <role-name>cms_editors</role-name> 
        </auth-constraint> 

    </security-constraint> 

    <!-- FORM based authentication --> 
    <!-- Leave this commented out, we will use BASIC (HTTP) authentication instead --> 
    <!-- 
    <login-config> 
        <auth-method>FORM</auth-method> 
        <form-login-config> 
            <form-login-page>/login.jsp</form-login-page> 
            <form-error-page>/error.jsp</form-error-page> 
        </form-login-config> 
    </login-config> 
    --> 
    <!-- This application uses BASIC authentication --> 
    <login-config> 
        <auth-method>BASIC</auth-method> 
        <realm-name>Editor Login</realm-name> 
    </login-config> 

    <!-- Define a constraint to force SSL on all pages in the application --> 
    <security-constraint> 

        <web-resource-collection> 
            <web-resource-name>Entire Application</web-resource-name> 
            <url-pattern>/*</url-pattern> 
        </web-resource-collection> 

        <user-data-constraint> 
            <transport-guarantee>CONFIDENTIAL</transport-guarantee> 
        </user-data-constraint> 

    </security-constraint> 


    <!-- ========================================================== --> 
    <!-- 错误页面 --> 
    <!-- ========================================================== --> 

    <!-- Define an error handler for 404 pages --> 
    <error-page> 
        <error-code>404</error-code> 
        <location>/error404.jsp</location> 
    </error-page> 

    <!-- Define an error handler for java.lang.Throwable --> 
    <error-page> 
        <exception-type>java.lang.Throwable</exception-type> 
        <location>/errorThrowable.jsp</location> 
    </error-page> 


    <!-- ========================================================== --> 
    <!-- Extra MIME types --> 
    <!-- ========================================================== --> 

    <!-- Set XML mime-mapping so spreadsheets open properly instead of being sent as an octet/stream --> 
    <mime-mapping> 
        <extension>xls</extension> 
        <mime-type>application/vnd.ms-excel</mime-type> 
    </mime-mapping> 


    <!-- ========================================================== --> 
    <!-- Locale --> 
    <!-- ========================================================== --> 

    <!-- Set Locale Encoding --> 
    <locale-encoding-mapping-list> 
        <locale-encoding-mapping> 
            <locale>ja</locale> 
            <encoding>Shift_JIS</encoding> 
        </locale-encoding-mapping> 
    </locale-encoding-mapping-list> 


    <!-- ========================================================== --> 
    <!-- Welcome Files --> 
    <!-- ========================================================== --> 

    <!-- Define, in order of preference, which file to show when no filename is defined in the path --> 
    <!-- eg: when user goes to http://yoursite.com/ or http://yoursite.com/somefolder --> 
    <!-- Defaults are provided in the server-wide web.xml file, such as index.jsp, index.htm --> 
    <!-- Note: using this tag overrides the defaults, so don't forget to add them here --> 
    <welcome-file-list> 
        <!-- Use index.swf if present, or splash.jsp, otherwise just look for the normal defaults --> 
        <welcome-file>index.swf</welcome-file> 
        <welcome-file>splash.jsp</welcome-file> 
        <welcome-file>index.html</welcome-file> 
        <welcome-file>index.htm</welcome-file> 
        <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 

</web-app>
上一篇 下一篇

猜你喜欢

热点阅读