要多少分给多少分!!!恼人的SERVLET乱码问题!!

liuwenxian 2004-10-23 09:35:59
我的SERVLET代码就是这样的,不知道为什么从数据库读出的数据都是乱吗“??”
,有人有处理的办法么?我用的是Tomcat5.26!!!!
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class ShareSelect extends HttpServlet{
Connection con=null;
public void init(ServletConfig config) throws ServletException{
super.init(config);

//加载桥接器
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){
}
}


public String getcode(String s){
String str=s;
try{
str=new String(str.getBytes("iso-8859-1"),"GB2312");
return str;
}
catch(Exception e){
str="111111";
return str;
}
}

public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {

PrintWriter out=response.getWriter();
request.setCharacterEncoding("iso-8859-1");
response.setCharacterEncoding("GB2312");
response.setContentType("text/html;charset=GB2312");
out.println("<html>");
out.println("<body>");
Statement sql=null;
ResultSet rs=null;
String str1,str2,str3,str4,str5,str6,str7,str8,str9;
if(con==null){
try{
con=DriverManager.getConnection("jdbc:odbc:renshi","sa","");
sql=con.createStatement();
rs=sql.executeQuery("select * from jihualuyong ");
out.print("<table>");
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=200>"+"个人简述");
out.print("<th width=100>"+"申请职位");
out.print("<th width=100>"+"家庭住址");
out.print("<th width=100>"+"联系电话");
out.print("</tr>");
while(rs.next()){
str1=rs.getString(1);
str1=getcode(str1);
str2=rs.getString(2);
str2=getcode(str2);
str3=rs.getString(3);
str3=getcode(str3);
str4=rs.getString(4);
str4=getcode(str4);
str5=rs.getString(5);
str5=getcode(str5);
str6=rs.getString(6);
str6=getcode(str6);
str7=rs.getString(7);
str7=getcode(str7);
str8=rs.getString(8);
str8=getcode(str8);
str9=rs.getString(9);
str9=getcode(str9);
out.print("<tr>");
out.print("<td>"+str1+"</td>");
out.print("<td>"+str2+"</td>");
out.print("<td>"+str3+"</td>");
out.print("<td>"+str4+"</td>");
out.print("<td>"+str5+"</td>");
out.print("<td>"+str6+"</td>");
out.print("<td>"+str7+"</td>");
out.print("<td>"+str8+"</td>");
out.print("<td>"+str9+"</td>");
out.print("</tr>");
}
out.print("</table>");
}
catch(SQLException e){
}
}

else{
synchronized(con){
try{
sql=con.createStatement();
rs=sql.executeQuery("select * from jihualuyong");
out.print("<table>");
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=200>"+"个人简述");
out.print("<th width=100>"+"申请职位");
out.print("<th width=100>"+"家庭住址");
out.print("<th width=100>"+"联系电话");
out.print("</tr>");
while(rs.next()){
str1=rs.getString(1);
str1=getcode(str1);
str2=rs.getString(2);
str2=getcode(str2);
str3=rs.getString(3);
str3=getcode(str3);
str4=rs.getString(4);
str4=getcode(str4);
str5=rs.getString(5);
str5=getcode(str5);
str6=rs.getString(6);
str6=getcode(str6);
str7=rs.getString(7);
str7=getcode(str7);
str8=rs.getString(8);
str8=getcode(str8);
str9=rs.getString(9);
str9=getcode(str9);
out.print("<tr>");
out.print("<td>"+str1+"</td>");
out.print("<td>"+str2+"</td>");
out.print("<td>"+str3+"</td>");
out.print("<td>"+str4+"</td>");
out.print("<td>"+str5+"</td>");
out.print("<td>"+str6+"</td>");
out.print("<td>"+str7+"</td>");
out.print("<td>"+str8+"</td>");
out.print("<td>"+str9+"</td>");
out.print("</tr>");
}
out.print("</table>");
}
catch(SQLException e){
}
}
}
out.print("</body>");
out.print("</html>");
}

public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
doPost(request,response);
}
}
...全文
69 点赞 收藏 4
写回复
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuwenxian 2004-10-24
解决啦!!!麻烦大家啦,辛苦了 给分给分!!!
回复
boy 2004-10-23

public String gbTo(String s)
{
String s1 = null;
try
{
s1 = new String(s.getBytes("GB2312"), "8859_1");
}
catch(Exception exception) { }
return s1;
}
回复
qazxsw1982103 2004-10-23
去掉这两句试试
request.setCharacterEncoding("iso-8859-1");
response.setCharacterEncoding("GB2312");
回复
liuwenxian 2004-10-23
还有,为什么我设置了过滤器后,TOMCAT服务器也打开了,可是说找不到页面了呢???
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告