web.xml配置文件的常用标签及其作用

一、标签简介:
web工程经常会用到web.xml文件,web.xml文件中常用的配置标签有<context-param>、<listener>、<filter>、<servlet>、<session-config>、<error-page>、<welcome-file-list>
1、上下文初始化参数

<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:springxml/*.xml</param-value>
</context-param>

param-name作为参数名在web应用中是唯一的,在web应用的整个生命周期中上下文初始化参数都存在,任意的Servlet和jsp都可以随时随地访问它在JSP网页中可以使用下列方法来取得:${initParam.param_name}
在Servlet可以使用下列方法来获得:
String param_name=getServletContext().getInitParamter(“param_name”);

param-value参数值可以为 Spring添加配置文件 , 配置spring核心监听器,默认会以 /WEB-INF/applicationContext.xml作为配置文件,contextConfigLocation 参数用来指定Spring的配置文件 如:classpath:springxml/*.xml,其中classpath为src目录

2、listener 监听器
它可以监听一些客户端的请求,服务端的操作。常用的监听接口有:
ServletContextAttributeListener监听对ServletContext属性的操作,ServletContextListener监听ServletContext,HttpSessionListener监听HttpSession的操作,HttpSessionAttributeListener监听HttpSession中的属性的操作。
例如:


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

ContextLoaderListener的作用就是启动Web容器时,自动装配
ApplicationContext.xml的配置信息。因为它实现了ServletContextListener这个接口。srvletContextListener 监听了servletContext,当容器创建时加载
springxml/*.xml的配置,当容器关闭时,销毁创建的对象。
3 过滤器
过滤器是servlet中定义的小型web组件,用来拦截servlet容器的请求和响应,控制客户段和服务端的数据交换。 此时需要注意Filter的配置需要放在Servlet配置之前,且过滤器配置的路径应该和需要过滤的Servlet路径相同
例如:

<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>*.do</url-pattern>
	</filter-mapping> 
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>*.jsp</url-pattern>
	</filter-mapping>

访问路径为*.jsp或者 *.action 时 web.xml会将获取到的请求转到StrutsPrepareAndExecuteFilter类,StrutsPrepareAndExecuteFilter接收到请求后会在struts.xml中进行查找和匹配并进行处理

4、session-config
session超时时长,当浏览器与服务器长时间没有交互时,session就会失效,该时长一般默认是30分中,可以通过web.xml进行设置,是针对整个容器的。

5、jsp-config

pageTag /WEB-INF/page-tag.tld

taglib-uri 申明jsp自定义标签库
taglib-location 标签库实际的地址

6、mime-mapping
是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会自动使用指定应用程序来打开
例如:

<mime-mapping>
	<extension>tar</extension>
	<mime-type>application/x-tar</mime-type>
</mime-mapping>

也可以配置静态页面的打开编码
例如:

```	<mime-mapping>
  <extension>htm</extension> 
  <mime-type>text/html;charset=gb2312</mime-type> 
  </mime-mapping>
 <mime-mapping>

7、error-page
error-page在web.xml中有两种配置方式,一是通过错误码来配置,而是通过异常的类型来配置
例如:
错误码配置

<error-page>
	<error-code>500</error-code>
	<location>/common/500.jsp</location>
</error-page>

异常类型配置

<error-page>
	<exception-type>java.lang.RuntimeException</exception-type>
	<location>/echn-console/common/error.jsp</location>
</error-page>

8、welcome-file-list
welcome-file-list是一个配置在web.xml中的一个欢迎页,
用于当用户在url中输入项目名称或者输入web容器url时直接跳转的页面.

例如:

<welcome-file-list>
	<welcome-file>index.jsp</welcome-file>
	<welcome-file>index.do</welcome-file>
</welcome-file-list>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值