编码过滤器不起作用
各位,在struts框架里面,我写了个过滤器类,然后在web.xml文件中进行了配置,但是测试时,由页面跳到action处理时,action中拿到的参数值还是乱码,不能起作用
filter类
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"); }}
<?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>
<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都没问题
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; }}