有两个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>

解决方案 »

  1.   

    这样肯定是乱码,一般post方法传送汉字,会经过自己写好的编码转化过滤器经过了转码,这样request得到的值不会乱,而LZ是通过get传送汉字,所以得把request对象重写,增加转码方法。贴下代码吧: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;
    }
    }
    使用:<%
    reqeust = new MyHttpServletRequest(request);
    title = (String) request.getParameter("title"); 
    out.println(title);
    %>
      

  2.   


    <% 
    //设置接收参数的编码方式 
    //这个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 
                                        

    %> 
      

  3.   

    使用表单传递参数可以通过get和post方式传递参数,也可以通过连接的方式传递参数,这个传递参数的方式实际上就是get方式传递参数,这种方式传递中文也可以产生乱码
    解决方法:修改服务器的<Connector URIEncoding="utf-8">
      

  4.   

    TO:gukuitian谢谢了,照你的方法去试了一下,问题总算解决了。呵呵,但还是有点模糊,
    在title = new String(request.getParameter("title").getBytes("iso8859-1"),"utf-8"); 中,其中.getBytes("iso8859-1"),"utf-8"):是指将utf-8转换为iso8859-1编码还是将iso8859转换为utf-8编码呢?
      

  5.   

    TO: swandragon
    谢谢了,用了楼上gukuitian这位朋友的方法,问题总算解决了。
    但修改服务器的<Connector URIEncoding="utf-8">,具体怎么修改?我还是有点不大明白。呵呵!
      

  6.   

    你在服务器中把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;
      }
      

  7.   

    TO:gukuitian
    reqeust = new MyHttpServletRequest(request);还是有问题。代码下的红线处提示:MyHttpServletRequest cannot be resolved to a type这样的错误。 MyHttpServletRequest的用法能不能再具体点。可不可以举个简单的例子来说明一下。
      

  8.   

    TO:zhangjihao
    谢谢了。呵呵,看起来有点复杂。用了楼上gukuitian这位朋友的方法,问题终于搞定了。