解决 STRUTS2 GET 参数中文乱码问题

STRUTS2中使用GET中文参数乱码问题,网上讨论这个问题的人很多却没有一个好的办法,于是唯有自己动脑了.

思路很简单,写一个filter放到filterchain最顶,将get参数值转为utf-8编码.

Filter:
Java代码 复制代码
  1. package filter;   
  2.   
  3. import java.io.IOException;   
  4. import java.util.Map;   
  5. import java.util.Set;   
  6.   
  7. import javax.servlet.Filter;   
  8. import javax.servlet.FilterChain;   
  9. import javax.servlet.FilterConfig;   
  10. import javax.servlet.ServletException;   
  11. import javax.servlet.ServletRequest;   
  12. import javax.servlet.ServletResponse;   
  13. import javax.servlet.http.HttpServletRequest;   
  14.   
  15.   
  16.   
  17.   
  18. public class SetCharacterEncodingFilter implements Filter {   
  19.   
  20.   
  21.     // ----------------------------------------------------- Instance Variables   
  22.   
  23.   
  24.       
  25.     protected String encoding = null;   
  26.   
  27.   
  28.       
  29.     protected FilterConfig filterConfig = null;   
  30.   
  31.   
  32.       
  33.     protected boolean ignore = true;   
  34.   
  35.   
  36.     // --------------------------------------------------------- Public Methods   
  37.   
  38.   
  39.       
  40.     public void destroy() {   
  41.   
  42.         this.encoding = null;   
  43.         this.filterConfig = null;   
  44.   
  45.     }   
  46.   
  47.   
  48.       
  49.     public void doFilter(ServletRequest request, ServletResponse response,   
  50.                          FilterChain chain)   
  51.  throws IOException, ServletException {   
  52.   
  53.            
  54.         //debug   
  55.         //System.out.println("OH YEYEYE");   
  56.            
  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.         //process get parameters   
  65.         HttpServletRequest httpRequest = (HttpServletRequest)request;   
  66.         Map paramMap = request.getParameterMap();   
  67.         String[] queryStringArray = {""};   
  68.         if (httpRequest.getQueryString()!=null) {   
  69.             queryStringArray = httpRequest.getQueryString().split("&");   
  70.         }   
  71.         for (int i = 0; i < queryStringArray.length; i++) {   
  72.             queryStringArray[i] = queryStringArray[i].replaceAll("(.*)=(.*)""$1");   
  73.         }   
  74.         Set<String> keySet = paramMap.keySet();   
  75.         for(String key : keySet){   
  76.             //check where param from   
  77.             boolean isFromGet = false;   
  78.             for(String paramFromGet:queryStringArray){   
  79.                 if(key.equals(paramFromGet))   
  80.                 {   
  81.                     isFromGet = true;   
  82.                 }   
  83.             }   
  84.             if (!isFromGet) {   
  85.                 continue;   
  86.             }   
  87.             String[] paramArray = (String[])paramMap.get(key);   
  88.             for(int i=0;i<paramArray.length;i++){   
  89.                     paramArray[i] = new String(paramArray[i].getBytes("iso-8859-1"),encoding);   
  90.             }   
  91.         }   
  92.            
  93.            
  94.  // Pass control on to the next filter   
  95.         chain.doFilter(request, response);   
  96.   
  97.     }   
  98.   
  99.   
  100.       
  101.     public void init(FilterConfig filterConfig) throws ServletException {   
  102.   
  103.  this.filterConfig = filterConfig;   
  104.         this.encoding = filterConfig.getInitParameter("encoding");   
  105.         String value = filterConfig.getInitParameter("ignore");   
  106.         if (value == null)   
  107.             this.ignore = true;   
  108.         else if (value.equalsIgnoreCase("true"))   
  109.             this.ignore = true;   
  110.         else if (value.equalsIgnoreCase("yes"))   
  111.             this.ignore = true;   
  112.         else  
  113.             this.ignore = false;   
  114.   
  115.     }   
  116.   
  117.   
  118.     // ------------------------------------------------------ Protected Methods   
  119.   
  120.   
  121.       
  122.     protected String selectEncoding(ServletRequest request) {   
  123.   
  124.         return (this.encoding);   
  125.   
  126.     }   
  127.   
  128. }  
package filter;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

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;




public class SetCharacterEncodingFilter implements Filter {


    // ----------------------------------------------------- Instance Variables


   
    protected String encoding = null;


   
    protected FilterConfig filterConfig = null;


   
    protected boolean ignore = true;


    // --------------------------------------------------------- Public Methods


   
    public void destroy() {

        this.encoding = null;
        this.filterConfig = null;

    }


   
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain)
 throws IOException, ServletException {

    	
    	//debug
    	//System.out.println("OH YEYEYE");
    	
        // Conditionally select and set the character encoding to be used
        if (ignore || (request.getCharacterEncoding() == null)) {
            String encoding = selectEncoding(request);
            if (encoding != null)
                request.setCharacterEncoding(encoding);
        }

        //process get parameters
        HttpServletRequest httpRequest = (HttpServletRequest)request;
        Map paramMap = request.getParameterMap();
        String[] queryStringArray = {""};
        if (httpRequest.getQueryString()!=null) {
        	queryStringArray = httpRequest.getQueryString().split("&");
		}
        for (int i = 0; i < queryStringArray.length; i++) {
        	queryStringArray[i] = queryStringArray[i].replaceAll("(.*)=(.*)", "$1");
		}
        Set<String> keySet = paramMap.keySet();
        for(String key : keySet){
        	//check where param from
        	boolean isFromGet = false;
        	for(String paramFromGet:queryStringArray){
        		if(key.equals(paramFromGet))
        		{
        			isFromGet = true;
        		}
        	}
        	if (!isFromGet) {
				continue;
			}
        	String[] paramArray = (String[])paramMap.get(key);
        	for(int i=0;i<paramArray.length;i++){
        			paramArray[i] = new String(paramArray[i].getBytes("iso-8859-1"),encoding);
        	}
        }
        
        
 // Pass control on to the next filter
        chain.doFilter(request, response);

    }


   
    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 if (value.equalsIgnoreCase("yes"))
            this.ignore = true;
        else
            this.ignore = false;

    }


    // ------------------------------------------------------ Protected Methods


   
    protected String selectEncoding(ServletRequest request) {

        return (this.encoding);

    }

}


web.xml:
Xml代码 复制代码
  1.  <!-- encoding filter -->  
  2. <filter>  
  3.     <filter-name>CharacterEncoding</filter-name>  
  4.     <filter-class>filter.SetCharacterEncodingFilter</filter-class>  
  5.     <init-param>  
  6.         <param-name>encoding</param-name>  
  7.         <param-value>utf-8</param-value>  
  8.     </init-param>  
  9. </filter>  
  10. <filter-mapping>  
  11.     <filter-name>CharacterEncoding</filter-name>  
  12.     <url-pattern>/*</url-pattern>  
  13. </filter-mapping>  
  <!-- encoding filter -->
	<filter>
		<filter-name>CharacterEncoding</filter-name>
		<filter-class>filter.SetCharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>



如果get和post中有参数同名结果可能不准确
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值