求Servlet的乱码解决办法!

liuwenxian 2004-10-22 12:45:29
各位兄弟姐妹能不能介绍一下你们都是怎么处理Servlet的乱码的呀,小弟我都为了这个问题烦恼了好几天了······救命啊!!!
...全文
202 点赞 收藏 2
写回复
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuwenxian 2004-10-23
我的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);
}
}
回复
mhz 2004-10-22
你能不能说清楚?
拜托!!!
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

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