日期:2014-05-18  浏览次数:20877 次

为什么插入数据库中的中文为乱吗
package   com.bean;

import   java.io.IOException;
import   java.io.PrintWriter;

import   javax.servlet.ServletException;
import   javax.servlet.http.HttpServlet;
import   javax.servlet.http.HttpServletRequest;
import   javax.servlet.http.HttpServletResponse;
//注册servlet
public   class   NewUserLoginServlet   extends   HttpServlet   {

public   NewUserLoginServlet()   {
super();
}

public   void   init()   throws   ServletException   {

}  

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

 
String   name   =   request.getParameter( "username ");
String   pass   =   request.getParameter( "password ");
String   sex   =   request.getParameter( "sex ");
String   email   =   request.getParameter( "email ");
String   Phone   =   request.getParameter( "tel ");

//   javaBean---------
PersonManager   pb   =   new   PersonManager();
pb.setName(name);
pb.setPass(pass);
pb.setSex(sex);
pb.setEmail(email);
pb.setPhone(Phone);

//调用insert()如果用户注册成功的话就从定向到指定网爷
boolean   result   =   ConnectionDatabase.insert(pb);

if   (result)
response.sendRedirect( "success.html ");
else
response.sendRedirect( "login.html ");

}

public   void   destroy()   {
super.destroy();

}

}


------解决方案--------------------
老问题了,设置编码方式,转码,过滤器
------解决方案--------------------
当数据参数通过请求传递时被自动转换成8859-1编码,所以你看到是乱码,需要进行响应的转码.
------解决方案--------------------
编码问题,我开始也碰到了,呵呵。
String name = new String(request.getParameter( "username ").getBytes( "ISO8859-1 ", "gb2312 "));
其余的同理!