Tomcat 5.0+SQLSERVER2000<%@ page contentType="text/html;chaset=GBK" %>
<%@ page import="java.sql.*" %><%!
Connection con=null;
%><html>
<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>
</html>
页面中中文字符全都无法显示!!请高手指点!
<%@ page import="java.sql.*" %><%!
Connection con=null;
%><html>
<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>
</html>
页面中中文字符全都无法显示!!请高手指点!
<%@ page contentType="text/html;chaset=GBK" %>
<%@ page import="java.sql.*" %><%!
Connection con=null;
%><html>
<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>
</html>
异常现象:
type Exception reportmessage 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 import="java.sql.*" %><html>
<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>
</html>
这样试验可以显示中文.晕~~为什么会这样呢?
String cStr = new String(str.getBytes("ISO-8859-1"),"GB2312");
这样得到的cStr为中文编码!
都用成是utf-8
更改为<%@ page contentType="text/html;chaset=gb2312" %>可以吗
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中配置:<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>epim.base.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
但不知道怎么给你加分?汗自己!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!~~~~~~~~~~~~~