本来页面没有乱码 为什么添加了 过滤器之后就乱码了、
过滤器 有 首先是汉字乱码过滤 然后是 登录过滤  最后是权限过滤  怎么回事  
不适用过滤器  就不会出现乱码 ………………

解决方案 »

  1.   

    乱码过滤器protected String encoding = null;
    protected FilterConfig filterConfig = null;
    protected boolean ignore = true; public void init(FilterConfig filterConfig) throws ServletException {
    this.filterConfig = filterConfig;
    this.encoding = filterConfig.getInitParameter("encoding");
    String value = filterConfig.getInitParameter("ignore");
    if (value == null)
    this.ignore = true;
    else if (value.equalsIgnoreCase("true"))
    this.ignore = true;
    else
    this.ignore = false;
    } public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain) throws IOException, ServletException {
    // TODO 自动生成方法存根
    if (ignore || (request.getCharacterEncoding() == null)) {
    String encoding = selectEncoding(request);
    if (encoding != null)
    request.setCharacterEncoding(encoding);
    }
    chain.doFilter(request, response);
    }xml配置:
    <filter>
    <filter-name>Set Character Encoding</filter-name>
    <filter-class>team4.motor.action.SetCharacterEncodingFilter</filter-class>
    <init-param>
    <param-name>encoding</param-name>

    <param-value>UTF-8</param-value>
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>Set Character Encoding</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
      

  2.   

    登录过滤器public void doFilter(ServletRequest req, ServletResponse res,
    FilterChain chain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;
    HttpSession session = request.getSession();
    PrintWriter out=response.getWriter();
    if (session.getAttribute("manager") == null||session.getAttribute("carowner") == null||session.getAttribute("operator") == null) {
    out.print("<script language=javascript>alert('请登录');javascript:location='../index.jsp'</script>");
    }
    chain.doFilter(req, res);
    }xml配置:
    <filter>
    <filter-name>loginFilter</filter-name>
    <filter-class>team4.motor.action.LoginFilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>loginFilter</filter-name>
    <url-pattern>/operator/*</url-pattern>
    <url-pattern>/manager/*</url-pattern>
    </filter-mapping>
      

  3.   

    权限过滤
    public void doFilter(ServletRequest req, ServletResponse res,
    FilterChain chain) throws IOException, ServletException {
    // 获取uri地址
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;
    HttpSession session = request.getSession();
    String uri = request.getRequestURI();
    String ctx = request.getContextPath();
    uri = uri.substring(ctx.length());
    PrintWriter out = response.getWriter();
    if (uri.startsWith("/manager")) {
    if (session.getAttribute("manager") == null) {
    out.print("<script language=javascript>alert('can not goto');javascript:history.go(-2)</script>");
    }
    }
    if(uri.startsWith("/operator")){
    if(session.getAttribute("operator")==null){
    out.print("<script language=javascript>alert('can not goto');javascript:history.go(-2)</script>");
    }
    }
    chain.doFilter(req, res);
    }xml配置:
    <filter>
    <filter-name>priorityFilter</filter-name>
    <filter-class>team4.motor.action.PriorityFilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>priorityFilter</filter-name>
    <url-pattern>/manager/*</url-pattern>
    <url-pattern>/operator/*</url-pattern>
    <url-pattern>/carowner/*</url-pattern>
    </filter-mapping>