7。中文乱码
在Tomcat中加入<%@ page contentType="text/html;charset=gb2312" %>
在Reisin中要加入<%@ page contentType="text/html;charset=gb2312" %>
并且在连接数据库时使用:
Conn=DriverManager.getConnection("jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=gb2312","root","");
关键是?useUnicode=true&characterEncoding=gb2312
在Tomcat中加入<%@ page contentType="text/html;charset=gb2312" %>
在Reisin中要加入<%@ page contentType="text/html;charset=gb2312" %>
并且在连接数据库时使用:
Conn=DriverManager.getConnection("jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=gb2312","root","");
关键是?useUnicode=true&characterEncoding=gb2312
<%@ page contentType="text/html;charset=gb2312" %>这句话我加了,数据库我用的是SQLserver2000,不是mySQL,应该不是这个问题
…………
这里增加 request.setCharasetEncoding("UTF-8");
String name=request.getParameter("username");
out.println("default:"+name);
out.println("GB2312:"+name);
输出的是正确的中文,难道是jakarta-tomcat-4.1.18不支持中文参数传递??
我不明白的就是为什么同一个程序在4.0.3下就可以,而在4.1.18下就乱码?
这里有问题!
应该用
username = URLEncoder.encode( username, "UTF-8" );
response.sendRedirect("login1.jsp?username="+username);
String cBytes = "GBK";
request.setCharacterEncoding(cBytes);然后在
String name=request.getParameter("username");
你用url传参数,通常无法还原,因为url通常是以utf-8字符集传递的,你还是最好把它放到session中,然后从session中取吧。
正确的编码。如果该文件没有得到正确运行,则从数据库中读取得数据为乱码。
假如平台为中文windows,则正确编码为gb2312;
如果是日文,则编程Shift_JIS。
说了这么多,什么意思呢?如果在中文windows平台上作JSP程序,而Jsp的编码却
为Shift_JIS,很显然会出现乱码。
所以我猜测,是不是在tomcat中也有一个这样的批处理文件或者设置文件,做一些编码的默认设定的呢?
out.println("login1.jsp?username="+username, 0);
<%!
/**
*set client url to new url
*@param secondTime : wait for time to jump
*eg:
String url = "login.jsp?userid=walksing" ;
out.println(getUrlTo(url, 5)); //
out.close(); //must has
*/
public static String getUrlTo(String url, int secondTime ){
StringBuffer s = new StringBuffer();
s.append("<meta http-equiv=\"refresh\" content=\""
+ secondTime + ";url=" + url + "\">");
return s.toString();
}
%>