初学jsp,什么都不懂,我写了一个简单的处理数据库表的jsp程序,
可是从数据库中显示到页面上的中文变成了乱码,看了别人的解决方法好像也不行,各位高手帮我看看是怎么回事啊!!<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%request.setCharacterEncoding("GB2312");%>
<%//@page pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=sdb";
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from depts";
//ResultSet rs=sdb.executeQuery(sql);
ResultSet rs=stmt.executeQuery(sql);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<!-- TemplateBeginEditable name="doctitle" -->
<title>无标题文档</title>
<!-- TemplateEndEditable -->
<!-- TemplateBeginEditable name="head" -->
<!-- TemplateEndEditable -->
</head><body>
<%String id = "";
String name = "";
while(rs.next()) {
id = new String(rs.getString(1).getBytes("ISO-8859-1"),"gb2312");
name = new String(rs.getString(2).getBytes("ISO-8859-1"),"gb2312");
//id=rs.getString(1);
//name=rs.getString(2);
%>
<p>您的第一个字段内容为:<%=id%>
您的第二个字段内容为:<%=name%></p>
<p>
<%}%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
可是从数据库中显示到页面上的中文变成了乱码,看了别人的解决方法好像也不行,各位高手帮我看看是怎么回事啊!!<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%request.setCharacterEncoding("GB2312");%>
<%//@page pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=sdb";
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from depts";
//ResultSet rs=sdb.executeQuery(sql);
ResultSet rs=stmt.executeQuery(sql);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<!-- TemplateBeginEditable name="doctitle" -->
<title>无标题文档</title>
<!-- TemplateEndEditable -->
<!-- TemplateBeginEditable name="head" -->
<!-- TemplateEndEditable -->
</head><body>
<%String id = "";
String name = "";
while(rs.next()) {
id = new String(rs.getString(1).getBytes("ISO-8859-1"),"gb2312");
name = new String(rs.getString(2).getBytes("ISO-8859-1"),"gb2312");
//id=rs.getString(1);
//name=rs.getString(2);
%>
<p>您的第一个字段内容为:<%=id%>
您的第二个字段内容为:<%=name%></p>
<p>
<%}%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
解决方案 »
- Tomcat之死新说---SSH无异常篇
- 为什么我的JSP代码加入了邮件检测便不能运行了?
- 请教一个application的问题,求帮助!!
- myeclipse中创建web项目无法访问一直出现400错误
- include动作和元素 的问题
- applet只能在有jre的机器上运行?
- 函数定义问题
- 路径问题:我的 first_test.jsp 放在什么地方,怎么执行?http:/localhost/first_test.jsp就可以吗?
- 用JAVA如何实现PB/VB的某些功能
- 寻找jsp译文…………急!急!急!急!
- sturts 一个.do执行完跳转到另外一个.do,如何使第一个.do的form和第二个.do的Form直接不进行数据传递
- web客户端交互性非常高,使用什么技术比较好
<%//@page pageEncoding="GB2312"%>--------------------------------
<%@ page language="java" pageEncoding="GB2312"%>
name = new String(rs.getString(2).getBytes("ISO-8859-1"),"gb2312");
这两句话为什么要转换编码?数据库中是ISO-8859-1编码?如果数据库中是这个编码的话应该不会有中文显示吧,我们开发中从数据库取数据,不转换编码。只有在从上一页面取得中文数据的时候才转换。因为tomcat默认是ISO-8859-1编码,所以它在传递数据的时候会将编码转换成ISO-8859-1。这种直接从数据库取数据应该没有转换的必要。
char改成nchar
varchar改成nvarchar
加这个<%@ page pageEncoding= "GBK "% > 没用的。
我自己解决了,
id = new String(rs.getString(1).getBytes("ISO-8859-1"),"gb2312");
name = new String(rs.getString(2).getBytes("ISO-8859-1"),"gb2312");
改成
id = new String(rs.getString(1).getBytes("GBK"));
name = new String(rs.getString(2).getBytes("GBK"));
就好了