当前页面: 开发资料首页 → J2SE 专题 → JSP无法显示数据库中中文字符,会出现乱码!请路过的指点一下!
JSP无法显示数据库中中文字符,会出现乱码!请路过的指点一下!
摘要: JSP无法显示数据库中中文字符,会出现乱码!请路过的指点一下!
Tomcat 5.0+SQLSERVER2000
<%@ page contentType="text/html;chaset=GBK" %>
<%@ page import="java.sql.*" %>
<%!
Connection con=null;
%>
<body>
<%
Statement stmt=null;
ResultSet rs=null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException e){}
if(con==null){
try{
con=DriverManager.getConnection("jdbc:odbc:redsun","","");
stmt=con.createStatement();
rs=stmt.executeQuery("SELECT * FROM XS");
out.print("NO.1");
}catch(SQLException e1){
out.print("未联接数据库");
}
}else{
synchronized(con){
try{
con=DriverManager.getConnection("jdbc:odbc:redsun","","");
stmt=con.createStatement();
rs=stmt.executeQuery("SELECT * FROM XS");
out.print("NO.2");
}catch(SQLException e2){
out.print("未联接数据库2");
}
}
}
%>
<%
try{
out.print("<table BORDER>");
out.print("<tr>");
out.print("<th width=100>"+"学号");
out.print("<th width=100>"+"姓名");
out.print("<th width=100>"+"专业");
out.print("<th width=100>"+"性别");
out.print("<th width=100>"+"出生时间");
out.print("<th width=100>"+"总学分");
out.print("<th width=100>"+"备注");
out.print("</tr>");
while(rs.next()){
out.print("<tr>");
out.print("<td width=100>"+rs.getString(1));
out.print("<td width=100>"+rs.getString(2));
out.print("<td width=100>"+rs.getString(3));
out.print("<td width=100>"+rs.getByte(4));
out.print("<td width=100>"+rs.getDate(5));
out.print("<td width=100>"+rs.getInt(6));
out.print("<td width=100>"+rs.getString(7));
out.print("</tr>");
}
out.print("</table>");
con.close();
}catch(SQLException e2){}
%>
</body>
页面中中文字符全都无法显示!!请高手指点!
然后自己又修改了程序,但是运行后又出现异常.以后是修改后的程序与异常现象:
<%@ page contentType="text/html;chaset=GBK" %>
<%@ page import="java.sql.*" %>
<%!
Connection con=null;
%>
<body>
<%
Statement stmt=null;
ResultSet rs=null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException e){}
if(con==null){
try{
con=DriverManager.getConnection("jdbc:odbc:redsun","","");
stmt=con.createStatement();
rs=stmt.executeQuery("SELECT * FROM XS");
out.print("NO.1");
}catch(SQLException e1){
out.print("未联接数据库");
}
}else{
synchronized(con){
try{
con=DriverManager.getConnection("jdbc:odbc:redsun","","");
stmt=con.createStatement();
rs=stmt.executeQuery("SELECT * FROM XS");
out.print("NO.2");
}catch(SQLException e2){
out.print("未联接数据库2");
}
}
}
%>
<%
try{
out.print("<table BORDER>");
out.print("<tr>");
String str=new String("学号");
byte b[]=str.getBytes("ISO-8859-1");
str=new String(b);
out.print("<th width=100>"+str);
str=new String("姓名");
b=str.getBytes("ISO-8859-1");
str=new String(b);
out.print("<th width=100>"+str);
str=new String("专业");
b=str.getBytes("ISO-8859-1");
str=new String(b);
out.print("<th width=100>"+str);
str=new String("性别");
b=str.getBytes("ISO-8859-1");
str=new String(b);
out.print("<th width=100>"+str);
str=new String("出生时间");
b=str.getBytes("ISO-8859-1");
str=new String(b);
out.print("<th width=100>"+str);
str=new String("总学分");
b=str.getBytes("ISO-8859-1");
str=new String(b);
out.print("<th width=100>"+str);
str=new String("备注");
b=str.getBytes("ISO-8859-1");
str=new String(b);
out.print("<th width=100>"+str);
out.print("</tr>");
while(rs.next()){
out.print("<tr>");
str=rs.getString(1);
b=str.getBytes("ISO-8859-1");
str=new String(b);
out.print("<th width=100>"+str);
str=rs.getString(2);
b=str.getBytes("ISO-8859-1");
str=new String(b);
out.print("<th width=100>"+str);
str=rs.getString(3);
b=str.getBytes("ISO-8859-1");
str=new String(b);
out.print("<th width=100>"+str);
out.print("<th width=100>"+rs.getByte(4));
out.print("<th width=100>"+rs.getDate(5));
out.print("<th width=100>"+rs.getInt(6));
str=rs.getString(7);
b=str.getBytes("ISO-8859-1");
str=new String(b);
out.print("<th width=100>"+str);
out.print("</tr>");
}
out.print("</table>");
con.close();
}catch(SQLException e2){
out.print("error!!");
}
%>
</body>
异常现象:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:372)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
java.lang.NullPointerException
org.apache.jsp.MyJsp_jsp._jspService(MyJsp_jsp.java:134)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
<%@ page contentType="text/html;charset=GBK" %>
<%@ page import="java.sql.*" %>
<body>
<%
Connection con;
Statement stmt;
ResultSet rs;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException e3){}
try{
con=DriverManager.getConnection("jdbc:odbc:redsun","","");
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery("SELECT * FROM XS");
int showPage=1;
%>
<%
rs.absolute(showPage);
out.print("<table Border>");
out.print("<tr>");
out.print("<th width=100>"+"学号");
out.print("<th width=100>"+"姓名");
out.print("<th width=100>"+"专业");
out.print("<th width=100>"+"性别");
out.print("<th width=100>"+"出生时间");
out.print("<th width=100>"+"总学分");
out.print("<th width=100>"+"备注");
out.print("</tr>");
while(!rs.isAfterLast()){
out.print("<tr>");
out.print("<td width=100>"+rs.getString(1));
out.print("<td width=100>"+rs.getString(2));
out.print("<td width=100>"+rs.getString(3));
out.print("<td width=100>"+rs.getByte(4));
out.print("<td width=100>"+rs.getDate(5));
out.print("<td width=100>"+rs.getInt(6));
out.print("<td width=100>"+rs.getString(7));
out.print("</tr>");
rs.next();
}
out.print("</table>");
con.close();
}catch(SQLException e2){
out.print("未联接数据库");
}
%>
</form>
</body>
这样试验可以显示中文.晕~~为什么会这样呢?
str = rs.getString(1);
String cStr = new String(str.getBytes("ISO-8859-1"),"GB2312");
这样得到的cStr为中文编码!
回复楼上,这样依然不行.
没有乱码,但是显示是???????
还是不解......为什么??请路过的高手进来指点
另外问一下,怎么给分??
用个servlet吧!
都用成是utf-8
把<%@ page contentType="text/html;chaset=GBK" %>
更改为<%@ page contentType="text/html;chaset=gb2312" %>可以吗
加一个Filter的servet,进行字符的过滤。
package epim.base;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class EncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null) {
request.setCharacterEncoding(encoding);
}
}
// Pass control on to the next filter
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null) {
this.ignore = true;
} else if (value.equalsIgnoreCase("true")) {
this.ignore = true;
} else if (value.equalsIgnoreCase("yes")) {
this.ignore = true;
} else {
this.ignore = false;
}
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
然后再web.xml中配置:
EncodingFilter
epim.base.EncodingFilter
encoding
GBK
EncodingFilter
/*
TKS!
但不知道怎么给你加分?汗自己!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!~~~~~~~~~~~~~