jsp中request传递参数汉字乱码

来源:互联网 发布:软件开发xyhlrj 编辑:程序博客网 时间:2024/05/22 05:13

问题描述: 

1 表单提交的数据,用request.getParameter(“xxx”)返回的字符串为乱码或者?? 
2 直接通过url如http://localhost/a.jsp?name=中国,这样的get请求在服务端用request. getParameter(“name”)时返回的是乱码;按tomcat4的做法设置Filter也没有用或者用request.setCharacterEncoding("GBK");也不管用 

原因: 
1 tomcat的j2ee实现对表单提交即post方式提示时处理参数采用缺省的iso-8859-1来处理 
2 tomcat对get方式提交的请求对query-string 处理时采用了和post方法不一样的处理方式。(与tomcat4不一样,所以设置setCharacterEncoding(“gbk”))不起作用。 


解决办法: 

首先所有的jsp文件都加上: 
1 实现一个Filter.设置处理字符集为GBK。(在tomcat的webapps/servlet-examples目录有一个完整的例子。请参考web.xml和SetCharacterEncodingFilter的配置。) 

1)只要把%TOMCAT安装目录%/ webapps/servlets-examples/WEB-INF/classes/filters/SetCharacterEncodingFilter.class文件拷到你的webapp目录/filters下,如果没有filters目录,就创建一个。 
2)在你的web.xml里加入如下几行: 


<filter> 
<filter-name>Set Character Encoding</filter-name> 
<filter-class>filters.SetCharacterEncodingFilter</filter-class> 
<init-param> 
<param-name>encoding</param-name> 
<param-value>GBK</param-value> 
</init-param> 
</filter> 

<filter-mapping> 
<filter-name>Set Character Encoding</filter-name> 
<url-pattern>/*</url-pattern> 
</filter-mapping> 


3)完成. 

2 get方式的解决办法 
1) 打开tomcat的server.xml文件,找到区块,加入如下一行: 
URIEncoding=”GBK” 
完整的应如下: 

<Connector 
port="80" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" 
enableLookups="false" redirectPort="8443" acceptCount="100" 
debug="0" connectionTimeout="20000" 
disableUploadTimeout="true" 
URIEncoding="GBK" 
/> 



2)重启tomcat,一切OK。

一、JSP页面显示乱码 
下面的显示页面(display.jsp)就出现乱码: 
<html> 
<head> 
<title>JSP的中文处理</title> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
</head> 

<body> 
<% 
out.print("JSP的中文处理"); 
%> 
</body> 
</html> 
对不同的WEB服务器和不同的JDK版本,处理结果就不一样。原因:服务器使用的编码方式不同和浏览器对不同的字符显示结果不同而导致的。解决办法:在JSP页面中指定编码方式(gb2312),即在页面的第一行加上:<%@ page contentType="text/html; charset=gb2312"%>,就可以消除乱码了。完整页面如下: 
<%@ page contentType="text/html; charset=gb2312"%> 
<html> 
<head> 
<title>JSP的中文处理</title> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
</head> 

<body> 
<% 
out.print("JSP的中文处理"); 
%> 
</body> 
</html> 

二、表单提交中文时出现乱码 
下面是一个提交页面(submit.jsp),代码如下: 
<html> 
<head> 
<title>JSP的中文处理</title> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
</head> 

<body> 
<form name="form1" method="post" action="process.jsp"> 
<div align="center"> 
<input type="text" name="name"> 
<input type="submit" name="Submit" value="Submit"> 
</div> 
</form> 
</body> 
</html> 
下面是处理页面(process.jsp)代码: 
<%@ page contentType="text/html; charset=gb2312"%> 
<html> 
<head> 
<title>JSP的中文处理</title> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
</head> 

<body> 
<%=request.getParameter("name")%> 
</body> 
</html> 
如果submit.jsp提交英文字符能正确显示,如果提交中文时就会出现乱码。原因:浏览器默认使用UTF-8编码方式来发送请求,而UTF-8和GB2312编码方式表示字符时不一样,这样就出现了不能识别字符。解决办法:通过request.seCharacterEncoding("gb2312")对请求进行统一编码,就实现了中文的正常显示。修改后的process.jsp代码如下: 
<%@ page contentType="text/html; charset=gb2312"%> 
<% 
request.seCharacterEncoding("gb2312"); 
%> 
<html> 
<head> 
<title>JSP的中文处理</title> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
</head> 

<body> 
<%=request.getParameter("name")%> 
</body> 
</html> 

三、数据库连接出现乱码 
只要涉及中文的地方全部是乱码,解决办法:在数据库的数据库URL中加上useUnicode=true&characterEncoding=GBK就OK了。 

四、数据库的显示乱码 
在mysql4.1.0中,varchar类型,text类型就会出现中文乱码,对于varchar类型把它设为binary属性就可以解决中文问题,对于text类型就要用一个编码转换类来处理,实现如下: 
public class Convert { 
/** 把ISO-8859-1码转换成GB2312 
*/ 
public static String ISOtoGB(String iso){ 
String gb; 
try{ 
if(iso.equals("") || iso == null){ 
return ""; 

else{ 
iso = iso.trim(); 
gb = new String(iso.getBytes("ISO-8859-1"),"GB2312"); 
return gb; 


catch(Exception e){ 
System.err.print("编码转换错误:"+e.getMessage()); 
return ""; 



把它编译成class,就可以调用Convert类的静态方法ISOtoGB()来转换编码。 

以上都是我遇到问题时从网上搜的答案,但都没有解决问题。

我们同学的解决方法是他加入了一个类如下:

public String GBToUnicode(String strIn)

String strOut = null; 
if(strIn == null || (strIn.trim()).equals(""))return strIn; 
try{ 
byte[] b = strIn.getBytes("ISO8859_1"); 
strOut = new String(b,"GBK"); 
}
catch(Exception e)
{} 
return strOut; 
}

我自己的解决方法是调用getParameter时采用的方法是:

1. String name=new String(request.getParameter("uname").getBytes("ISO-8859-1"));

2. String name=request.getParameter("uname");
      byte[] b=a.getBytes("ISO8859-1");

     name=new String(b);

      用<%=name%> 即可输出传递过来的中文信息
3、中文作为参数传递乱码 
  当我们把一段中文字符作为参数传递个另一页面时,也会出现乱码情况,解决方法如下: 
  在参数传递时对参数编码,比如 
  RearshRes.jsp?keywords=" + java.net.URLEncoder.encode(keywords) 
  然后在接收参数页面使用如下语句接收 
  keywords=new String(request.getParameter("keywords").getBytes("8859_1"));

0 0