web.xml配置介绍

来源:互联网 发布:vb中inputbox什么意思 编辑:程序博客网 时间:2024/05/16 19:20

web.xml的作用

web.xml文件是用来初始化配置信息:比如Welcome页面、servlet、servlet-mapping、filter、listener、启动加载级别等。

当你的web工程没用到这些时,你可以不用web.xml文件来配置你的Application。

web.xml的关键元素分析

1、welcome-file-list 和 welcome-file元素

在访问一个网站时,用户看到的第一个页面就是欢迎页面。通常,欢迎页面都在web.xml中指定

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

2、filter和filter-mapping元素

filter元素用于声明一个过滤器,使用该元素可以同时拦截多个请求的URL。filter-mapping元素用来指定与过滤器关联的URL

<!--配置struts2的核心过滤器 --><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping>

3、error-page元素

配置error-page元素来捕获java异常,此时需要设置exception来指定java中的异常

<error-page><error-code>404</error-code><location>/error.jsp</location><!-- 如果发生http404错误,则返回location子元素指定的文件 --></error-page><!-- 用于捕获java异常 --><error-page><exception-type>java.lang.Exception</exception-type><location>/error.jsp</location></error-page>

4、listener元素

该元素用来注册监听器,并使子元素listener-class指定监听程序的完整限定类名

<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param>

5、session-config元素

该元素用来指定会话过期时间

<session-config><session-timeout>30</session-timeout><!-- 单位是分 --></session-config>

6、init-param元素

该元素用来定义参数

0 0
原创粉丝点击