struts1.2乱码纠错

Filter:

 1 /**
 2  * 权限验证过滤器,副业是乱码纠错
 3  * @author izumi
 4  */
 5 public class VerificationFilter implements Filter{
 6     
 7     /**
 8      * HttpServletRequest对象
 9      */
10     private HttpServletRequest thisRequest;
11     /**
12      * HttpServletResponse对象
13      */
14     private HttpServletResponse thisResponse;
15     /**
16      * The default character encoding to set for requests that pass through
17      * this filter.
18      */
19     protected String encoding = null;
20     /**
21      * The filter configuration object we are associated with.  If this value
22      * is null, this filter instance is not currently configured.
23      */
24     protected FilterConfig filterConfig = null;
25     /**
26      * Should a character encoding specified by the client be ignored?
27      */
28     protected boolean ignore = true;
29     /**
30      * init
31      */
32     public void init(FilterConfig filterConfig) throws ServletException {
33         this.filterConfig = filterConfig;
34         this.encoding = filterConfig.getInitParameter("encoding");
35         String value = filterConfig.getInitParameter("ignore");
36         if (value == null){
37             this.ignore = true;
38         }else if (value.equalsIgnoreCase("true")){
39             this.ignore = true;
40         }else if (value.equalsIgnoreCase("yes")){
41             this.ignore = true;
42         }else{
43             this.ignore = false;
44         }
45     }
46     /**
47      * destroy
48      */
49     public void destroy() {
50         this.encoding = null;
51         this.filterConfig = null;
52     }
53     /**
54      * doFilter
55      */
56     public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
57         // Conditionally select and set the character encoding to be used
58         if (ignore || (request.getCharacterEncoding() == null)) {
59             String encoding = selectEncoding(request);
60             if (encoding != null){
61                 request.setCharacterEncoding(encoding);
62             }
63         }
64         thisRequest = (HttpServletRequest)request;
65         thisResponse = (HttpServletResponse)response;
66         String path = thisRequest.getRequestURI()+"?"+thisRequest.getQueryString();
67         if(!path.contains("major.do?method=login") && !path.contains("major.do?method=verification")){ //如果是登录操作,就不过滤权限
68             UserBean userBean = (UserBean)thisRequest.getSession().getAttribute("com.izumi.UserBean");
69             if(null == userBean){ // 如果用户没有登录或session过期,就跳转到登录页面
70                 thisResponse.sendRedirect("/parallel/major.do?method=login");
71                 return;
72             }
73         }
74         chain.doFilter(request, response);
75     }
76     /**
77      * return this.encoding
78      * @param request
79      * @return
80      */
81     protected String selectEncoding(ServletRequest request) {
82         return (this.encoding);
83     }
84 }

web.xml:

 1 <filter>
 2         <filter-name>VerificationFilter</filter-name>
 3         <filter-class>
 4             com.izumi.filter.VerificationFilter
 5         </filter-class>
 6         <init-param>
 7             <param-name>encoding</param-name>
 8             <param-value>utf-8</param-value>
 9         </init-param>
10         <init-param>
11             <param-name>ignore</param-name>
12             <param-value>true</param-value>
13         </init-param>
14     </filter>
15 
16     <filter-mapping>
17         <filter-name>VerificationFilter</filter-name>
18         <url-pattern>/*</url-pattern>
19     </filter-mapping>

 

posted on 2011-11-16 15:23  izumi  阅读(477)  评论(0编辑  收藏  举报

导航