Java基础——web.xml配置简述

web.xml文件用来初始化Java工程配置信息,以下就该文件做简要描述以作参考:

启动web项目后,web容器首先会去找web.xml文件,读取这个文件,web.xml加载顺序:

1)、容器会创建一个 ServletContext ( servlet 上下文),整个 web 项目的所有部分都将共享这个上下文;

2)、容器将其转换为键值对,并交给 servletContext;

3)、容器创建 中的类实例,创建监听器;

4)、容器加载filter,创建过滤器, 要注意对应的filter-mapping一定要放在filter的后面;

5)、容器加载servlet,加载顺序按照 Load-on-startup 来执行。

<!--定义了WEB应用的名字-->
<display-name></display-name>

<!--声明WEB应用的描述信息-->
<description></description>

<!--servlet API的版本2.3增加了对事件监听程序的支持,事件监听程序在建立、修改和删除会话或servlet环境时得到通知。Listener元素指出事件监听程序类-->
<listener></listener>

<!-- 加载Spring容器配置-->
<listener>  
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
</listener>

<!-- 防止Spring内存溢出监听器-->
<listener>  
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
</listener>

<!--如果在web.xml中不写任何参数配置信息,默认的路径是"/WEB-INF/applicationContext.xml,  
    在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml。  
    如果是要自定义文件名可以在web.xml里加入contextConfigLocation这个context参数:  
    在<param-value> </param-value>里指定相应的xml文件名,如果有多个xml文件,可以写在一起并以“,”号分隔。  
    也可以这样applicationContext-*.xml采用通配符,比如这那个目录下有applicationContext-ibatis-base.xml,  
    applicationContext-action.xml,applicationContext-ibatis-dao.xml等文件,都会一同被载入。  
    在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。-->  
<context-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>classpath*:spring/spring-common.xml</param-value>  
</context-param>

<!-- 配置SpringMVC核心控制器-->
<servlet>  
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <!-- 配置初始配置化文件,前边contextConfigLocation看情况二选一-->
    <init-param>  
        <param-name>contextConfigLocation</param-name>  
        <!--其中<param-value>**.xml</param-value> 这里可以使用多种写法-->  
        <!--1、不写,使用默认值:/WEB-INF/<servlet-name>-servlet.xml-->  
        <!--2、<param-value>/WEB-INF/classes/spring-mvc.xml</param-value>-->  
        <!--3、<param-value>classpath*:spring-mvc.xml</param-value>-->  
        <!--4、多个值用逗号分隔-->  
        <param-value>classpath:spring/spring-mvc.xml</param-value>  
    </init-param>  
    <load-on-startup>1</load-on-startup><!--启动加载一次-->  
</servlet>  
<servlet-mapping>  
    <!--这个Servlet的名字是springMVC,可以有多个DispatcherServlet,是通过名字来区分的。每一个DispatcherServlet有自己的WebApplicationContext上下文对象。同时保存的ServletContext中和Request对象中.-->  
    <!--ApplicationContext是Spring的核心,Context我们通常解释为上下文环境,我想用“容器”来表述它更容易理解一些,ApplicationContext则是“应用的容器”了:P,Spring把Bean放在这个容器中,在需要的时候,用getBean方法取出-->  
    <servlet-name>springMVC</servlet-name>  
    <!--Servlet拦截匹配规则可以自已定义,当映射为@RequestMapping("/user/add")时,为例,拦截哪种URL合适?-->  
    <!--1、拦截*.do、*.htm, 例如:/user/add.do,这是最传统的方式,最简单也最实用。不会导致静态文件(jpg,js,css)被拦截。-->  
    <!--2、拦截/,例如:/user/add,可以实现现在很流行的REST风格。很多互联网类型的应用很喜欢这种风格的URL。弊端:会导致静态文件(jpg,js,css)被拦截后不能正常显示。 -->  
    <url-pattern>/</url-pattern> <!--会拦截URL中带“/”的请求。-->  
</servlet-mapping>

<!--指定欢迎页面-->  

<welcome-file-list>
    <welcome-file>index.html</welcome-file>  
</welcome-file-list>

<!--当系统出现404错误,跳转到页面nopage.html-->  

<error-page>
    <error-code>404</error-code>  
    <location>/nopage.html</location>  
</error-page>  

<!--当系统出现java.lang.NullPointerException,跳转到页面error.html-->  
<error-page>
    <exception-type>java.lang.NullPointerException</exception-type>  
    <location>/error.html</location>  
</error-page>  

<!--会话超时配置,单位分钟-->  
<session-config>
    <session-timeout>360</session-timeout>  
</session-config>

<!-- 解决工程编码过滤器-->
这个过滤器就是针对于每次浏览器请求进行过滤的,然后再其之上添加了父类没有的功能即处理字符编码。  
其中encoding用来设置编码格式,forceEncoding用来设置是否理会 request.getCharacterEncoding()方法,设置为true则强制覆盖之前的编码格式。-->  
<filter>  
    <filter-name>encodingFilter</filter-name>  
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
    <init-param>  
        <param-name>encoding</param-name>  
        <param-value>UTF-8</param-value>  
    </init-param>  
    <init-param>  
        <param-name>forceEncoding</param-name>  
        <param-value>true</param-value>  
    </init-param>  
</filter>  
<filter-mapping>  
    <filter-name>encodingFilter</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping> 

<!-- 自定义过滤器-->
<filter>  
    <filter-name>gainAuthorityFilter</filter-name>  
    <filter-class>com.test.ddm.GainAuthorityFilter</filter-class>  <!-- 自定义过滤器-->
    <init-param>  
        <param-name>redirectURL</param-name>  
        <param-value>/test/login.jsp</param-value>
        <description>如果用户未登录,则重定向到web应用登录页面</description>
    </init-param>
    <init-param>  
        <param-name>sessionKey</param-name>  
        <param-value>cas_account</param-value>
        <description>用户信息在session的key</description>
    </init-param>    
    <init-param>  
        <param-name>exceptionUrl</param-name>  
        <param-value>cas_account</param-value>  
        <description>拦截例外</description>
    </init-param>  
</filter>  
<filter-mapping>
 
    <filter-name>gainAuthorityFilter</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping>

<!--如果Web应用具有想到特殊的文件,希望能保证给他们分配特定的MIME类型,则mime-mapping元素提供这种保证-->
<mime-mapping>
    <extension>htm</extension>
    <mime-type>text/html</mime-type>
</mime-mapping>

<!-- 安全限制配置-->
<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>

待补充...

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ddm01

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值