jsp传递中文参数的问题
response.sendRedirect( "updata.jsp?UserName= "+UserName);
我用这句跳转页面,UserName是中文的,但是传到updata页面就变成乱码,我该怎么解决!!
我试过用
<%@ page pageEncoding= "GBK " %>
<%@ page contentType= "text/html; charset=GBK " %>
<% request.setCharacterEncoding( "GBK ");%>
都不行!!
------解决方案--------------------确实,url传中文参数常常会出问题的
页面上 <%@ page contentType= "text/html; charset=GBK " %> 就足够啦
还要对中文字符集进行转码,web服务器默认的编码规则,如果是tomcat的话,可以如下配置
找到server.xml文件,修改这段配置文件,在最后加上 URIEncoding= 'GBK '
<Connector port= "8080 "
maxThreads= "150 " minSpareThreads= "25 " maxSpareThreads= "75 "
enableLookups= "false " redirectPort= "8443 " acceptCount= "100 "
debug= "0 " connectionTimeout= "20000 "
disableUploadTimeout= "true " URIEncoding= 'GBK '/>
------解决方案--------------------要用filter。
在web.xml文件中添加:
<filter>
<filter-name> Set Character Encoding </filter-name>
<filter-class>
com.SetCharacterEncodingFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name> Set Character Encoding </filter-name>
<url-pattern> *.do </url-pattern>
</filter-mapping>
SetCharacterEncodingFilter 类代码如下:
package com.db;
/**
* @author cool
*
*/
import
java.io.IOException;
import javax.servlet.*;
public class SetCharacterEncodingFilter implements Filter {
public void init(FilterConfig config) throws
ServletException {
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws
IOException, ServletException {
request.setCharacterEncoding( "GB2312 ");
chain.doFilter(request, response);
}
}
------解决方案--------------------楼上的应该可以解决楼主的问题,呵呵。
我传一个在项目中应用过的方法给楼主看看:
/* ISO8859码转成GBK
* 创建日期:
* @return boolean
* @param source java.lang.String
*/
public static String LatinToGBK(String s) {
if (s == null)
return null;
try
{
byte[] b = s.getBytes( "8859_1 ");
String s2 = new String(b, "GBK ");
return s2;
} catch (Exception e) {
e.printStackTrace();
}
return s;
}