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

A标签中传递的中文参数在另一个JSP页面用request.getParameter()接收时出现中文乱码有关问题

2012-03-20 
A标签中传递的中文参数在另一个JSP页面用request.getParameter()接收时出现中文乱码问题有两个JSP页面:tes

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;    }    } 


[解决办法]

HTML code
<% //设置接收参数的编码方式 //这个setCharacterEncoding只用于post,get是不好使的,//request.setCharacterEncoding("UTF-8"); //response.setCharacterEncoding("UTF-8"); String title = null; if (request.getParameter("title") != null) { //这个比较好用,试试看title = new String(request.getParameter("title").getBytes("iso8859-1"),"utf-8"); out.println("标题:" + title);                                     //乱码:标题:ä»·æ ¼æ??è´µç??MP4                                     } %>
[解决办法]
使用表单传递参数可以通过get和post方式传递参数,也可以通过连接的方式传递参数,这个传递参数的方式实际上就是get方式传递参数,这种方式传递中文也可以产生乱码
解决方法:修改服务器的<Connector URIEncoding="utf-8">
[解决办法]
你在服务器中把url的?后面的部分转下码。试一试

//url的?后面的部分
String parm = new String(httpServletRequest.getQueryString().getBytes("UTF-8"));


public static String setParam(String param){
String[] params = param.split("&");
String[] setParam = new String[2];
String rtn = "";
for(int i = 0;i < params.length;i++){
setParam = params[i].split("=");
if(setParam.length==2){
try {
rtn += setParam[0] + "=" + URLEncoder.encode(setParam[1], "gbk") + "&";
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
rtn = rtn.substring(0, rtn.length()-1);
System.out.println("rtn = " + rtn);
return rtn;
}
[解决办法]
嗯!用 #2 的解决了!

热点排行