读书人

Filter施用+struts2x拦截器区别

发布时间: 2012-10-09 10:21:45 作者: rapoo

Filter应用+struts2x拦截器区别

?

版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://shenzhenchufa.blog.51cto.com/730213/269722 (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;
????}
???}
??}???//对不属于不用过滤的,查询数据表,看用户是否有权访问。若没,则返回提示用户无限访问页面。若有,则直接通过。}?

本文出自 “Changes we need ! ” 博客,请务必保留此出处http://shenzhenchufa.blog.51cto.com/730213/269722

?

读书人网 >软件架构设计

热点推荐