又是jsp的中文问题,头都大!
环境: 英文win2k + tomcat + 英文sqlserver2000
很简单就是用一个类读取表中内容(在库里是中文的,没问题),问题是读出来的都变成了??,
页面如下 : test.jsp
<%@ page pageEncoding= "GBK "%>
<%@ page contentType= "text/html;charset=GBK " autoFlush= "true "%>
<%@ page import= "com.test.* "%>
<html>
<head>
<title> test </title>
</head>
<BODY>
<% request.setCharacterEncoding( "GBK ");
String testid= "294 ";
MyTest mt= new MyTest() ;
String name= mt.getName(testid);
String username= new String(name.getBytes( "ISO8859_1 "));
out.println( "用户名字: "+username);
%>
</body>
</html>
------解决方案--------------------用一个过滤器什么问题都解决,不用写什么request.setCharacterEncoding( "GBK ")
------解决方案-------------------- <%@ page pageEncoding= "GBK "%>
<%@ page contentType= "text/html;charset=GBK " autoFlush= "true "%>
????????????????????????呵呵,不显示中文也不用两个都写上吧
这个地方:
MyTest mt= new MyTest() ;
String name= mt.getName(testid);
String username= new String(name.getBytes( "ISO8859_1 "));
out.println( "用户名字: "+username);
改成:
MyTest mt= new MyTest() ;
String name= mt.getName(testid);
String username= new String(name.getBytes( "GB2312 "), "ISO-8859-1 ");
out.println( "用户名字: "+username);
试试看行不
你应该把逻辑装到Action中去做就清晰了
这么在页面上写太乱了!!!
------解决方案--------------------把String username= new String(name.getBytes( "GB2312 "), "ISO-8859-1 ");
该为String username= new String(name.getBytes( "ISO-8859-1 "), "GB2312 ");
试试
------解决方案--------------------把那该成 utf-8 就 OK 了
------解决方案--------------------在web.xml中加入过渡器
<!-- 字符编码转换 Character Encoding filter -->
<filter>
<filter-name> encodingFilter </filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name> encoding </param-name>
<param-value> UTF-8 </param-value>
</init-param>
</filter>
<!-- 字符编码的拦截器 Character Encoding filter -->
<filter-mapping>
<filter-name> encodingFilter </filter-name>
<url-pattern> *.do </url-pattern>
</filter-mapping>
------解决方案--------------------看看是不是sqlserver jdbc 驱动的原因. 以前也遇到过.不管怎么转就是不显示中文,后来换个新一点的驱动,一下就好了.