一个有关乱码的问题,请高手指点
大家好啊,我刚学jsp不久,在测试servlet时,从数据库中提取的信息,显示的是乱码,但我在servlet中已做了设置,感觉没有错误,请大家指导,先看看我的程序,是不是有问题啊,应该如何该写这是一个利用servlet+javaBean来查询数据库中信息的小例子,由三个代码组成
//////////////////contact.html/////////////
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<hr>
<center>
输入要操作是用户名,然后点击对应的操作。<br>
<form action="/zibian/contact" method="get">
<table><tr><td>name:
<input type="text" name="catid"></td></tr>
<tr><td><input type="submit" name="action" value="view"><input type="submit" name="action" value="delete"></td></tr>
</table>
</form>
</center>
</body>
</html>
/////////////////////ContactServlet1.java//////////////
package mypack;
import javax.servlet.*;
import javax.servlet.http.*;
import com.microsoft.jdbc.sqlserver.SQLServerDriver;
import java.sql.*;
import java.util.*;
import *;
import mypack.sqlBean1;
public class ContactServlet1 extends HttpServlet
{
Connection con;
sqlBean1 db=new sqlBean1();
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException
{
request.setCharacterEncoding("gb2312");
response.setContentType("text/html;charset=gb2312");
PrintWriter out=response.getWriter();
String action=(String)request.getParameter("action");
String catid=(String)request.getParameter("catid");
if(action.equals("delete"))
{
try
{
db.deleteContact(catid);
out.println("成功删除!?"+catid);
}
catch(Exception e)
{
out.println(" 删除失败后者没有这个信息:"+catid);
}
}
else
{
ResultSet rst=db.getContact(catid);
out.println("<table border=1><tr><td>编号</td><td>用户名</td><td>其他</td></tr>");
try
{
while(rst.next())
{
out.println("<tr>");
out.println("<td>"+rst.getString("catid")+"</td>");
out.println("<td>"+rst.getString("name")+"</td>");
out.println("<td>"+rst.getString("descn")+"</td>");
out.println("</tr>");
}
out.println("</table>");
rst.close();
}
catch(Exception e)
{
}
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException
{
doGet(request,response);
}
}
////////////sqlBean1.java 用来连接数据库,并负责查询的bean//////////////
package mypack;
import *;
import java.sql.*;
public class sqlBean1
{///
public Connection con=null;
public ResultSet rs=null;
public sqlBean1()
{/////构造函数
String CLASSFORNAME="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String SERVANDDB="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=jspdev";
String USER="sa";
String PWD="123";
try
{
Class.forName(CLASSFORNAME);
con=DriverManager.getConnection(SERVANDDB,USER,PWD);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void deleteContact(String catid)throws Exception
{
try
{ Statement stmt=con.createStatement();
stmt.execute("delete from category where catid='"+catid+"'");
stmt.close();
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
//从数据库中查询信息,注意先创建对象stm,result, 注意结果的返回,return result
public ResultSet getContact(String catid)
{
try
{
Statement stmt=con.createStatement();
ResultSet result=stmt.executeQuery("select * from category where catid='"+catid+"'");
return result;
}
catch(Exception e)
{
}
return null;
}
}