web.xml文件的说明和介绍


web.xml文件的说明

web.xml文件的说明和介绍

1.整体介绍

web.xml文件主要用于配置 项目名,后端配置文件及其位置,listen监听项目的运行和filter过滤信息

2.详细介绍

2.1配置顺序

一般web.xml文件的配置顺序为display-name, context-param,listener,filter,servlet,error-page下面进行详细的讲解。

2.2详细说明

 <display-name>项目的名称</display-name>
 <!--项目运行地址,用于解决同个容器部署多个web项目,默认值为webPath.root-->
  <context-param>
    <param-name>webAppRootKey</param-name>
    <param-value>项目名字.root</param-value>
  </context-param>
  <!-- 整个项目的配置文件,数据库连接配置,springmvc配置,放在main下的resource文件下-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      classpath:spring-mvc.xml
      classpath:spring-jdbc.xml
    </param-value>
  </context-param>
<!--配置日志文件,用于输入相关日志信息,默认配置文件放在main下的resource文件下也可以修改配置文件的位置
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>WEB-INF/log4j.properties</param-value>
</context-param>
  -->
  <!--日志文件log4j的刷新间隔-->
  <context-param>
    <param-name>log4jRefreshInterval</param-name>
    <param-value>600000</param-value>
  </context-param>
  <!--listen用于监听项目-->
  <!--监听项目的启动情况-->
  <listener>
    <listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
  </listener>
  <!--spring容器的初始化-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 设置编码格式-->
 <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>
  </filter>
  <!--设置需要过滤的请求-->
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
<!--配置springmvc解析器-->
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>
      org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <!--
    默认配置文件的位置在WEB-INF文件夹下面的dispatcher-servlet.xml文件
    可以修改
     <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:/config/dispatcher-context.xml</param-value>
    </init-param>
    -->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <!--设置需servlet要处理的文件名-->
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <!--设置错误静态界面-->
    <error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/error/404.jsp</location>
  </error-page>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

2.3filter的作用

过滤器
Filter有如下几个用处。

在HttpServletRequest到达Servlet之前,拦截客户的HttpServletRequest。
根据需要检查HttpServletRequest,也可以修改HttpServletRequest头和数据。
在HttpServletResponse到达客户端之前,拦截HttpServletResponse。
根据需要检查HttpServletResponse,也可以修改HttpServletResponse头和数据。
Filter有如下几个种类。
用户授权的Filter:Filter负责检查用户请求,根据请求过滤用户非法请求。
日志Filter:详细记录某些特殊的用户请求。
负责解码的Filter:包括对非标准编码的请求解码。
能改变XML内容的XSLT Filter等。
Filter可负责拦截多个请求或响应;一个请求或响应也可被多个请求拦截。
创建一个Filter只需两个步骤:
建Filter处理类;
web.xml文件中配置Filter。

public class LogFilter implements Filter 
{
//FilterConfig可用于访问Filter的配置信息
private FilterConfig config;
//实现初始化方法
public void init(FilterConfig config)
{
this.config = config; 
}
//实现销毁方法
public void destroy()
{
this.config = null; 
}
//执行过滤的核心方法
public void doFilter(ServletRequest request,ServletResponse response, FilterChain chain)throws IOException,ServletException
{
//---------下面代码用于对用户请求执行预处理---------
//获取ServletContext对象,用于记录日志
ServletContext context = this.config.getServletContext(); 
long before = System.currentTimeMillis();
System.out.println("开始过滤...");
//将请求转换成HttpServletRequest请求
HttpServletRequest hrequest = (HttpServletRequest)request;
//记录日志
context.log("Filter已经截获到用户的请求地址: " + hrequest.getServletPath());
//Filter只是链式处理,请求依然放行到目的地址
chain.doFilter(request, response); 
//---------下面代码用于对服务器响应执行后处理---------
long after = System.currentTimeMillis();
//记录日志
context.log("过滤结束");
//再次记录日志
context.log("请求被定位到" + hrequest.getRequestURI() + "所花的时间为: " + (after - before)); 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值