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

A标签中传递的中文参数在另一个JSP页面用request.getParameter()接收时出现中文乱码问题
有两个JSP页面:test1.jsp和test2.jsp,两个页面的编码方式都是UTF-8。其中在test.jsp中有一个超链接: <a style = "text-decoration:none;" href = "test6.jsp?title=<%out.println("价格最贵的MP4");%>">超链接</a>。在test2.jsp中用request.getParameter("title")接收,地址栏中都能看到传过来的是中文:“http://localhost:8080/test/test2.jsp?title=价格最贵的MP4”但接收过来的老是乱码,只能显示出MP4这几个英文字母。自己找了老半天还是找不出问题的所在,所以只能请朋友们帮忙看下了。具体代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <base href="<%=basePath%>">
   
  <title>My JSP 'test1.jsp' starting page</title>
   
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">  
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

  </head>
  
  <body>
  <a style = "text-decoration:none;" href = "test2.jsp?title=<%out.println("价格最贵的MP4");%>">超链接</a>
  </body>
</html>

test2页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'test6.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<%
//设置接收参数的编码方式
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String title = null;
if (request.getParameter("title") != null) {
title = (String) request.getParameter("title");

out.println("标题:" + title);

  //乱码:标题:ä»·æ ¼æ??è´µç??MP4
   
}
%>
</body>
</html>


------解决方案--------------------
这样肯定是乱码,一般post方法传送汉字,会经过自己写好的编码转化过滤器经过了转码,这样request得到的值不会乱,而LZ是通过get传送汉字,所以得把request对象重写,增加转码方法。贴下代码吧:
Java code

package com.chinaot.web.servlet;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 说明:<br>
 * 
 * @author 张纪豪
 * @version
 * Build Time Sep 13, 2009
 */
public class MyHttpServletRequest extends HttpServletRequestWrapper {

    protected Log log = LogFactory.getLog(getClass());
    private static final String ENCODED = "UTF-8",DECODE="ISO8859-1";
    
    public MyHttpServletRequest(HttpServletRequest request) {
        super(request);
    }

    @Override
    public String getQueryString() {
        String queryString = super.getQueryString();
        if(queryString != null){
            try {
                return new String(queryString.getBytes(DECODE),ENCODED);
            } catch (UnsupportedEncodingException e) {
                log.info(e.getMessage(),e);
            }
        }
        return queryString;
    }

    @Override
    public String getParameter(String name) {
        String value = super.getParameter(name);
        if (value != null) {
            try {
                return new String(value.getBytes(DECODE), ENCODED);
            } catch (UnsupportedEncodingException e) {
                log.info(e.getMessage(),e);
            }
        }
        return value;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Map getParameterMap() {
        Map<String, String[]> values = super.getParameterMap();
        Map<String, String[]> valuesMap = new HashMap<String, String[]>();
        Set<String> names = values.keySet();
        for (Iterator<String> iter = names.iterator(); iter.hasNext();) {
            String name=  iter.next();
            String[] v = values.get(name);
            if(v != null) {
                String[] valuesCopy = new String[v.length];
                for(int i = 0; i < v.length; i++) {
                    try {
                        valuesCopy[i] = new String(v[i].getBytes(DECODE), ENCODED);
                    } catch (UnsupportedEncodingException e) {
                        log.info(e.getMessage(),e);
                    }
                }
                valuesMap.put(name, valuesCopy);// 向新Map放入转码过的值
            }            
        }
        return valuesMap;// 返回是副本
    }

    @Override
    public String[] getParameterValues(String name) {
        String[] values = super.getParameterValues(name);
        if(values != null) {
            String[] valuesCopy = new String[values.length];//原数组是只读的,不能修改
            for(int i = 0; i < values.length; i++) {
                try {
                    valuesCopy[i] = new String(values[i].getBytes(DECODE), ENCODED);
                } catch (UnsupportedEncodingException e) {
                    log.info(e.getMessage(),e);
                }
            }
            return valuesCopy;
        }
        return values;
    }

    
}