为什么我直接在servlet里面直接输出message不乱码,而跳转到web页面就出现乱码,而且存到数据库的也是乱码,我尝试了网上的各种方法,还是不成功。。
public class HandleRegister extends HttpServlet
{ public void init(ServletConfig config) throws ServletException{ super.init(config);try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");}catch(Exception e){} }public String handleString(String s){ try{ byte bb[]=s.getBytes("iso-8859-1");s=new String(bb);}catch(Exception ee){} return s; }public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ response.setHeader("content-type","text/html;charset=UTF-8");PrintWriter out=response.getWriter();Connection con; PreparedStatement sql;Register reg=new Register(); request.setAttribute("register",reg);String logname=request.getParameter("logname").trim(),password=request.getParameter("password").trim(),sex=request.getParameter("sex").trim(),email=request.getParameter("email").trim(),phone=request.getParameter("phone").trim(),message=request.getParameter("message");message= new String( message.getBytes("ISO-8859-1"),"gb2312");out.println(message);sex= new String( sex.getBytes("ISO-8859-1"),"gb2312");out.println(sex);int age=Integer.parseInt(request.getParameter("age").trim());String uri="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=Student";if(logname==null)logname="";if(password==null)password="";boolean isLD=true;for(int i=0;i<logname.length();i++){ char c=logname.charAt(i);if(!((c<='z'&&c>='a')||(c<='Z'&&c>='A')||(c<='9'&&c>='0'))) isLD=false;} boolean boo=logname.length()>0&&password.length()>0&&isLD;String backNews="";try{ con=DriverManager.getConnection(uri,"sa","sa");String insertCondition="INSERT INTO member VALUES (?,?,?,?,?,?,?,?)";sql=con.prepareStatement(insertCondition);if(boo){ sql.setString(1,handleString(logname));sql.setString(2,handleString(password));sql.setString(3,handleString(sex));sql.setInt(4,age);sql.setString(5,phone);sql.setString(6,email);sql.setString(7,handleString(message));sql.setString(8,"public.jpg");int m=sql.executeUpdate();if(m!=0){ backNews="注册成功";reg.setBackNews(backNews);reg.setLogname(logname);reg.setPassword(handleString(password));reg.setAge(age);reg.setSex(handleString(sex));reg.setEmail(handleString(email));reg.setPhone(phone);reg.setMessage(handleString(message));}}else{ backNews="信息填写不完整或名字中有非法字符";reg.setBackNews(backNews); }con.close();}catch(SQLException exp){ backNews="该会员名已被使用,请您更换名字"+exp;reg.setBackNews(backNews); }RequestDispatcher dispatcher= request.getRequestDispatcher("/ptext/User/showRegisterMess.jsp");dispatcher.forward(request, response);}public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ doPost(request,response);}
}
jsp页面:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<%request.setCharacterEncoding("gb2312");%>
</head><BODY><Font size=2>
<CENTER>
<FORM action="helpRegister" name=form method="post">
<BR>输入您的信息,会员名字必须由字母和数字组成,带*号项必须填写。
<table><tr><td>会员名称:</td> <td><Input type=text name="logname" >*</td></tr><tr><td>设置密码:</td><td><Input type=password name="password">*</td></tr><tr><td>性别:</td><td><Input type=radio name="sex" checked="o" value="男">男<Input type=radio name="sex" value="女">女</td></tr><tr><td>会员年龄:</td><td><Input type=text name="age" value="0"></td></tr><tr><td>电子邮件:</td><td><Input type=text name="email"></td></tr><tr><td>联系电话:</td><td><Input type=text name="phone"></td></tr></table><table><tr><td>输入您的个人简介:</td></tr><tr><td><TextArea name="message" Rows="6" Cols="30"></TextArea></td></tr><tr><td><Input type=submit name="g" value="提交"></td> </tr>
</table>
</Form></CENTER>
</BODY>
</html>
显示页面:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<%@ page import="mybean.data.Register"%>
<jsp:useBean id="register" type="mybean.data.Register" scope="request" />
</head>
<BODY bgcolor=cyan><Font size=2>
<CENTER><Font size=4 color=blue >
<jsp:getProperty name="register" property="backNews" /></Font>
<Font size=2><table><tr><td>注册的会员名称:</td><td><jsp:getProperty name="register" property="logname" /></td></tr><tr><td>注册的性别:</td> <td><jsp:getProperty name="register" property="sex" /></td></tr><tr><td>注册的会员年龄:</td><td><jsp:getProperty name="register" property="age" /></td></tr><tr><td>注册的电子邮件:</td><td><jsp:getProperty name="register" property="email" /></td></tr><tr><td>注册的联系电话:</td><td><jsp:getProperty name="register" property="phone" /></td></tr>
</table><table><tr><td>您输入的个人简介:</td></tr><tr><td><TextArea name="message" Rows="6" Cols="30">
<jsp:getProperty name="register" property="message" /></TextArea></td></tr>
</table>
</FONT></CENTER>
</BODY>
</html>