日期:2014-05-20  浏览次数:20785 次

编码过滤器不起作用
各位,在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文件,如下