Filter应用+struts2x拦截器区别

(1)Filter与struts2x拦截器区别:Filter:当程序请求servlet,jsp时,Filter会进行拦截。程序将先经过filter后,才到达目标的servlet,jsp;常用于系统权限管理(即用户访问某些页面之前,进行Filter权限检查)struts2x拦截器:只是在程序访问Action之前进行拦截。常用于记录系统操作日志,或添加额外功能。 (2)Filter简介:Filter的三个方法:init(),destroy(),doFilter(); 三个实例如下:(1)-------<利用filter进行中文字符处理>------------
1)
在web.xml中的配置:
<filter>
   <filter-name>encoding</filter-name>
   <filter-class>
    org.lxh.myzngt.filter.EncodingFilter
   </filter-class>
   <init-param>
    <param-name>charset</param-name>
    <param-value>gbk</param-value>
   </init-param>

</filter>
<filter-mapping>
   <filter-name>encoding</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>2)
JAVA代码:
public class EncodingFilter implements Filter {
private String charset = null;public void destroy() {}public void doFilter(ServletRequest req, ServletResponse resp,
    FilterChain chain) throws IOException, ServletException {
   req.setCharacterEncoding(this.charset);
   chain.doFilter(req, resp);
}//Filter获取web.xml中的初始参数;public void init(FilterConfig arg0) throws ServletException {
   this.charset = arg0.getInitParameter("charset");
}

} (2)<利用filter进行用户登录过滤>------------
1)
<filter>
   <filter-name>userlogin</filter-name>
   <filter-class>
    org.lxh.myzngt.filter.UserLoginFilter
   </filter-class>
</filter>
<filter-mapping>
   <filter-name>userlogin</filter-name>
   <url-pattern>/jsp/user/*</url-pattern>
</filter-mapping>
<filter-mapping>    -->可以多个匹配!!!!
   <filter-name>userlogin</filter-name>
   <url-pattern>/jsp/ques/*</url-pattern>
</filter-mapping>

2)
public class UserLoginFilter implements Filter {
public void destroy() {
}public void doFilter(ServletRequest arg0, ServletResponse arg1,
    FilterChain arg2) throws IOException, ServletException {
   HttpServletRequest req = (HttpServletRequest) arg0;
   if (req.getSession().getAttribute("userid") != null) {
    arg2.doFilter(arg0, arg1);
   } else {
    req.getRequestDispatcher("forward.htm").forward(arg0, arg1) ;
   }
}public void init(FilterConfig arg0) throws ServletException {
}
} (3)在用户访问目标页面之前,利用Filter进行进行权限查询:1.用户是否已登录(即session中是否有相应的记录)2.用户权限是否符合访问页面要求。如下:(1)在web.xml中添加:
<filter>
   <filter-name>PrivilegeFilter</filter-name>
   <filter-class>com.demo.web.filter.PrivilegeFilter</filter-class>
   <init-param>
    <param-name>doNotFilterURL</param-name>
    <param-value>
     admin/rand, /login, admin/logout, /common/, /images/, /style/, /js/, /fckeditor/, /userFilesAbsolutePath, /editPass
    </param-value>

   </init-param>
</filter><filter-mapping>
   <filter-name>PrivilegeFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping> (2)
com.demo.web.filter.PrivilegeFilter定义:
public class PrivilegeFilter implements Filter {/** 不需要过滤的URL */
private String[] doNotFilterURL;public void init(FilterConfig filterConfig) throws ServletException {
   String params = filterConfig.getInitParameter("doNotFilterURL");
   if (params != null) {
    String urls[] = params.split(",");
    doNotFilterURL = new String[urls.length];
    for (int i = 0, size = urls.length; i < size; i++) {
     doNotFilterURL[i] = urls[i];
    }
   }
}
       
   HttpServletRequest req = (HttpServletRequest) request;
   String requestPath = req.getRequestURI();        //如:demo/login.action
   String contextRoot = req.getContextPath();       //如:demo
   int length = contextRoot.length();
   String path = requestPath.substring(length);    //如:/login.action
   if (path != null && path.length() != 0) {
    path = path.trim();
   }   /** 登录页面不进行拦截 */
   if (Constants.FIRST_LOGIN_URL.getStringValue().equals(path)) {
    return true;
   }
   //获取请求的地址,比对不需要过滤的URL的数组doNotFilterURL。
   boolean doNotFilter = false;
   if (doNotFilterURL != null) {
    for (String url : doNotFilterURL) {     if (url != null && path.contains(url.trim())) {
      doNotFilter = true;
      break;
     }
    }
   }    //对不属于不用过滤的,查询数据表,看用户是否有权访问。若没,则返回提示用户无限访问页面。若有,则直接通过。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值