日期:2014-05-18  浏览次数:20835 次

JSP 页面于页面传递中文 出现乱吗
<a   href= 'UpdatePage.jsp?username=
<%=rs.getString( "UserName ")%> &Phone= <%=rs.getString( "Phone ")   %> &Address= <%=rs.getString( "Address ").toString()   %> &Email= <%=rs.getString( "Email ")   %> &Sex= <%=rs.getString( "Sex ")   %> '> <%=rs.getString( "UserName ")%>
传递参数   其他英文的还好都没出现乱吗
唯一一个Address参数   是中文的,   我在另一个页面
一个文本框接收的时候
使用的方法是     value= " <%=request.getParameter( "Address ").getBytes( "gb2312 ").toString()   %> "可是显示出来的却上乱码[B@1627c16
而我要是把.getBytes   ( "gb2312 ")去掉的话那么就出现乱码  
把gb2312   改成估计通用的utf-8也是出现[B@1627c16

------解决方案--------------------
1.编码问题:
(1)首先确定JSP页面头部是否有: <%@ page contentType= "text/html; charset=GBK " %>
(2)类似这样的转码:
String param= new String(request.getParameter( "param ").getBytes( "ISO-8859-1 "), "GBK ");
(3)添加filter字符过滤器,具体做法:

先添加类:

import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.FilterChain;
import java.io.IOException;
import org.apache.log4j.Logger;
import java.net.URLEncoder;

/** *//**
* 请求中中文字符串过滤类
*/

public class SetEncodingFilter
implements Filter ...{
private Logger logger=Logger.getLogger(this.getClass());

public void init(FilterConfig filterConfig) throws ServletException ...{
}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException ...{
logger.info( "请求转码过滤器=================== ");
request.setCharacterEncoding( "gb2312 ");
chain.doFilter(request,response);
}

public void destroy() ...{
}
}
再注册类到XML里:

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


(4)如果是通过 "a.jsp?param=中文 "传递参数,则需要:
a.在传参数之前先把参数进行转码:java.net.URLEncoder.encode(param);
取值用java.net.URLDncoder.dncode(param);再转回中文
b.在你的Tomcat目录--> conf目录--> server.xml里找出这段:

<Connector
port= "8080 " maxThreads= "150 " minSpareThreads= "25 " maxSpareThreads= "75 "
enableLookups= "false " redirectPort= "8443 " acceptCount= "100 "
debug= "0 " connectionTimeout= "20000 "
disableUploadTimeout= "true " <!--在里边加上这个参数--> URIEncoding= "gb2312 "
/>


(5)数据库乱码,