Struts2 Servlet过滤笔记

过滤器的使用步骤:

a.      在web.xml中配置过滤器

<filter>

<filter-name>checkSession</filter-name>  <filter-class>com.zenisoft.usertagmng.support.CheckSessionFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>checkSession</filter-name>

<url-pattern>*.do</url-pattern>

</filter-mapping>

 

b.      新建一个CheckSessionFilter类,实现Filter接口(Filter有三个方法:init方法,doFilter方法,destory方法。三个方法依次调用,其中init方法在服务器启动时最先调用)

c.      在web.xml中配置需要加载的参数

<context-param>

<param-name>paramsPath</param-name>

      <param-value>/conf/params.properties</param-value>    </context-param>

 

d.      通过getInitparameter方法来调用web.xml中配置的参数,在web.xml中有两种参数的书写形式,在init方法中初始化这些参数,该方法使用如下:

1)      <init-param>

String name=getInitParameter(“name”);   或

String name=getServletConfig().getInitParameter(“name”);

 

2)      <context-param>

String name=getServletContext().getInitParameter(“name”);  或

String name=getServletConfig().etServletContext().getInitParameter(“name”);

 

Filter接口参数详解:

a.      ServletRequest接口和ServletResponse接口:
当客户端有请求到来时,Servlet容器会创建一个ServletRequest对象来封装请求数据,同时创建一个ServletResponse对象来封装响应数据。

b.      FilterChain接口:
过滤器的作用之一就是在用户的请求到达servlet之前,拦截下来做预处理,处理之后再执行” filterChain.doFilter(request,response) ” 这个方法,如果还有其他的过滤器,那么将处理好的请求传给下个过滤器,当把所有的请求处理好之后,再将处理完的请求发给servlet。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小胖墩有点瘦

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

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

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

打赏作者

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

抵扣说明:

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

余额充值