各位大虾,本人现在做一系统,以Servlet做为逻辑处理页面,但显示的是乱码,我是从JSP页面传数据到Servlet处理,然后存入数据库,但从数据库中取出数据就出现乱码。但如果不插入数据库,而直接传回JSP页面的话,则没乱码
出现,所以我怀疑插入数据库这里出了问题,但采用了好多网上的方法,还是不行。连过滤器也用了,也解决不了,
不知道各位有其他的办法没?下面是部分Servlet代码:
response.setContentType("text/html; charset=gb2312");
PrintWriter out = response.getWriter();
Popwindow pop = new Popwindow();
String action=request.getParameter("action");
if (action.equals("add"))
{
Votes votes = null;
try {
votes = new Votes();
}
catch (Exception e) {
e.printStackTrace();
}
request.setCharacterEncoding("gb2312");
String maker = request.getParameter("maker");
String votes_str = request.getParameter("votes");
int votes_num=0;
if(votes_str!=null)
votes_num=Integer.parseInt(votes_str);
votes.setMaker(maker);
votes.setVotes(votes_num);
addVotes(votes);
out.println(pop.Js_ErrorInfo_redirect("分类信息添加成功!","../manager/Class_Add.jsp"));
}
if (action.equals("mod"))
{
request.setCharacterEncoding("gb2312");
String id = request.getParameter("id");
String maker = request.getParameter("maker");
String votes_str = request.getParameter("votes");
modVotes(maker,votes_str,id);
out.println(pop.Js_ErrorInfo_redirect("分类信息修改成功!","../manager/Class_Manage.jsp"));
}
out.flush();
out.close();
}
public void addVotes(Votes votes){
DBConnectionManager db = DBConnectionManager.getInstance();
Connection conn = db.getConnection("idb");
String add_sql = "insert into votes (maker,votes) values (?,?)";
try {
PreparedStatement ps =conn.prepareStatement(add_sql);
ps.setString(1,votes.getMaker());
ps.setInt(2,votes.getVotes());
ps.executeUpdate();
}
catch (Exception e) {
db.freeConnection("idb",conn);
e.printStackTrace();
}
db.freeConnection("idb",conn);
}
麻烦大家给点意见,急!!!