首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

编码过滤器不起作用,该如何解决

2012-02-16 
编码过滤器不起作用各位,在struts框架里面,我写了个过滤器类,然后在web.xml文件中进行了配置,但是测试时,

编码过滤器不起作用
各位,在struts框架里面,我写了个过滤器类,然后在web.xml文件中进行了配置,但是测试时,由页面跳到action处理时,action中拿到的参数值还是乱码,不能起作用

filter类

Java code
package com.isoftstone.struts.filter;import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class EncodingFilter implements Filter {        String encoding = null ;    public void destroy() {        // TODO Auto-generated method stub    }    public void doFilter(ServletRequest arg0, ServletResponse arg1,            FilterChain arg2) throws IOException, ServletException {        HttpServletRequest request = (HttpServletRequest)arg0;        HttpServletResponse response = (HttpServletResponse)arg1;        request.setCharacterEncoding(encoding);    }    public void init(FilterConfig arg0) throws ServletException {        // TODO Auto-generated method stub        encoding = arg0.getInitParameter("encoding");    }}


web.xml文件
XML code
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">   <filter>      <filter-name>EncodingFilter</filter-name>      <filter-class>com.isoftstone.struts.filter.EncodingFilter</filter-class>     <init-param>          <param-name>encoding</param-name>          <param-value>utf-8</param-value>      </init-param>   </filter>            <filter-mapping>      <filter-name>EncodingFilter</filter-name>      <url-pattern>/GovPro/*</url-pattern>    </filter-mapping>  <servlet>    <servlet-name>action</servlet-name>    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>    <init-param>      <param-name>config</param-name>      <param-value>/WEB-INF/struts-config.xml</param-value>    </init-param>    <init-param>      <param-name>debug</param-name>      <param-value>3</param-value>    </init-param>    <init-param>      <param-name>detail</param-name>      <param-value>3</param-value>    </init-param>    <load-on-startup>0</load-on-startup>  </servlet>  <servlet-mapping>    <servlet-name>action</servlet-name>    <url-pattern>*.do</url-pattern>  </servlet-mapping>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>


[解决办法]
楼主是通过超连直接进来的还是通过form的post请求过来?
如果是超连过来的,可以试下这样看看
tomcat目录下conf/server.xml中
 <Connector port="8080" protocol="HTTP/1.1" 
connectionTimeout="20000" 
redirectPort="8443" useBodyEncodingForURI="true"/>
[解决办法]
问一下你,你发出请求时,有到运行EncodingFilter的代码吗,如果没有,那么将该页面放到入GovPro文件夹下,应该你在web.xml配置时只指定了对GovPro下的文件过滤。

[解决办法]
如果你要对所有的请求都要编码的话,就要更改web.xml文件,如下


XML code
<filter-mapping>      <filter-name>EncodingFilter</filter-name>      <url-pattern>/*</url-pattern>    </filter-mapping>
[解决办法]
HttpServletRequest request = (HttpServletRequest)arg0;
HttpServletResponse response = (HttpServletResponse)arg1;
request.setCharacterEncoding(encoding);



request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
chain.doFilter(request,response);
[解决办法]
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)arg0;
HttpServletResponse response = (HttpServletResponse)arg1;
request.setCharacterEncoding(encoding);
}

你这样能执行到action中的话,应该是没有执行到这个filter,
因为你少写了一行arg2.doFilter(request,response);如楼上.
还有你的这个过滤器,只能过滤post请求情况下的 ,以下这种post和get都没问题

Java code
package cn.com.yd.gamecard.filter;import java.io.IOException;import java.io.UnsupportedEncodingException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequestWrapper;import javax.servlet.http.HttpServletResponse;/** * 字符编码过滤器 * @author  * */public class EncodeFilter implements Filter {private  String encode = "gb2312";        public void destroy() {            }    public void doFilter(ServletRequest arg0, ServletResponse arg1,            FilterChain arg2) throws IOException, ServletException {                HttpServletRequest hreq = (HttpServletRequest)arg0;        HttpServletResponse response = (HttpServletResponse)arg1;        response.setContentType("text/html;charset="+encode);        HttpRequestWapper req = new HttpRequestWapper(hreq);        arg2.doFilter(req, arg1);    }        public void init(FilterConfig arg0) throws ServletException {        String encodeName = arg0.getInitParameter("encode");        if(encodeName != null && !"".equals(encodeName.trim())){            this.encode = encodeName;        }    }    public class HttpRequestWapper extends HttpServletRequestWrapper{        public HttpRequestWapper(HttpServletRequest request) {            super(request);                    }        //重写方法        public String getParameter(String name){            String str = null;            str = encoding(((HttpServletRequest)this.getRequest()).getParameter(name));                        return str;        }        public String[] getParameterValues(String name){            String[] str = null;            str = ((HttpServletRequest)this.getRequest()).getParameterValues(name);            if(str != null){                for(int i = 0; i < str.length; i++){                    str[i] = encoding(str[i]);                }            }            return str;        }            }    //实现转码    public String encoding(String str){        if(str != null){            try {                str = new String(str.getBytes("iso-8859-1"),this.encode);            } catch (UnsupportedEncodingException e) {                e.printStackTrace();            }        }        return str;    }} 

热点排行