写的一个注册页面, 有个textarea,用来写自我介绍的,然后在servlet中用StringBuffer接受,数据库是用hibernate生成的,里面关于自我介绍的属性是StringBuffer类型的。当提交注册页面时,mysql中显示的自我介绍的内容是乱码,以StringBuferxxxxxxxxxxx开头的,后台打印自我介绍的内容,能正常显示,改成String类型正常。我mysql的字符集应该没问题的,只有用StringBuffer接受才出问题。请高手帮忙解决下 谢谢部分代码如下:实体类:package entity;import java.util.Date;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table(name="t_customer")
public class Customer
{
private int c_id;
private String c_name;
private String c_password;
private String c_sex;
private Date c_birthday;
private String c_education;
private StringBuffer c_intorduce;
private String c_favor;
@Id
@GeneratedValue
public int getC_id() {
return c_id;
}
public void setC_id(int cId) {
c_id = cId;
}
public String getC_name() {
return c_name;
}
public void setC_name(String cName) {
c_name = cName;
}
public String getC_password() {
return c_password;
}
public void setC_password(String cPassword) {
c_password = cPassword;
}
public String getC_sex() {
return c_sex;
}
public void setC_sex(String cSex) {
c_sex = cSex;
}
@Temporal(TemporalType.DATE)
public Date getC_birthday() {
return c_birthday;
}
public void setC_birthday(Date cBirthday) {
c_birthday = cBirthday;
}
public String getC_education() {
return c_education;
}
public void setC_education(String cEducation) {
c_education = cEducation;
}
public StringBuffer getC_intorduce() {
return c_intorduce;
}
public void setC_intorduce(StringBuffer cIntorduce) {
c_intorduce = cIntorduce;
}
public String getC_favor() {
return c_favor;
}
public void setC_favor(String cFavor) {
c_favor = cFavor;
}
}
servlet:@Override
protected void doGet(HttpServletRequest req, HttpServletResponse rep)
throws ServletException, IOException {
req.setCharacterEncoding("GBK");
rep.setContentType("text/html;charset=GBK");
PrintWriter out=rep.getWriter();
String c_name=req.getParameter("c_name");
String c_password=req.getParameter("c_password");
String c_sex=req.getParameter("c_sex");
String year=req.getParameter("year");
String month=req.getParameter("month");
String date=req.getParameter("date");
String c_birthday=year+"-"+month+"-"+date;
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
Date c_birth=new Date();
try {
c_birth = df.parse(c_birthday);
}
catch (ParseException e) {
e.printStackTrace();
}
String c_edu=req.getParameter("c_edu");
String c_favor=req.getParameter("c_favor");
/*StringBuffer c_intorduce=new StringBuffer();
c_intorduce.append(req.getParameter("c_intorduce"));
System.out.println(c_intorduce.toString());*/
String c_intorduce=req.getParameter("c_intorduce");
Customer customer=new Customer();
customer.setC_name(c_name);
customer.setC_password(c_password);
customer.setC_sex(c_sex);
customer.setC_birthday(c_birth);
customer.setC_education(c_edu);
customer.setC_favor(c_favor);
customer.setC_intorduce(c_intorduce);
CustomerDao dao=new CustomerDao();
dao.addcustomer(customer);
out.flush();
out.close();
}
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table(name="t_customer")
public class Customer
{
private int c_id;
private String c_name;
private String c_password;
private String c_sex;
private Date c_birthday;
private String c_education;
private StringBuffer c_intorduce;
private String c_favor;
@Id
@GeneratedValue
public int getC_id() {
return c_id;
}
public void setC_id(int cId) {
c_id = cId;
}
public String getC_name() {
return c_name;
}
public void setC_name(String cName) {
c_name = cName;
}
public String getC_password() {
return c_password;
}
public void setC_password(String cPassword) {
c_password = cPassword;
}
public String getC_sex() {
return c_sex;
}
public void setC_sex(String cSex) {
c_sex = cSex;
}
@Temporal(TemporalType.DATE)
public Date getC_birthday() {
return c_birthday;
}
public void setC_birthday(Date cBirthday) {
c_birthday = cBirthday;
}
public String getC_education() {
return c_education;
}
public void setC_education(String cEducation) {
c_education = cEducation;
}
public StringBuffer getC_intorduce() {
return c_intorduce;
}
public void setC_intorduce(StringBuffer cIntorduce) {
c_intorduce = cIntorduce;
}
public String getC_favor() {
return c_favor;
}
public void setC_favor(String cFavor) {
c_favor = cFavor;
}
}
servlet:@Override
protected void doGet(HttpServletRequest req, HttpServletResponse rep)
throws ServletException, IOException {
req.setCharacterEncoding("GBK");
rep.setContentType("text/html;charset=GBK");
PrintWriter out=rep.getWriter();
String c_name=req.getParameter("c_name");
String c_password=req.getParameter("c_password");
String c_sex=req.getParameter("c_sex");
String year=req.getParameter("year");
String month=req.getParameter("month");
String date=req.getParameter("date");
String c_birthday=year+"-"+month+"-"+date;
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
Date c_birth=new Date();
try {
c_birth = df.parse(c_birthday);
}
catch (ParseException e) {
e.printStackTrace();
}
String c_edu=req.getParameter("c_edu");
String c_favor=req.getParameter("c_favor");
/*StringBuffer c_intorduce=new StringBuffer();
c_intorduce.append(req.getParameter("c_intorduce"));
System.out.println(c_intorduce.toString());*/
String c_intorduce=req.getParameter("c_intorduce");
Customer customer=new Customer();
customer.setC_name(c_name);
customer.setC_password(c_password);
customer.setC_sex(c_sex);
customer.setC_birthday(c_birth);
customer.setC_education(c_edu);
customer.setC_favor(c_favor);
customer.setC_intorduce(c_intorduce);
CustomerDao dao=new CustomerDao();
dao.addcustomer(customer);
out.flush();
out.close();
}
ps,为什么你的属性名前面都有个“c_”啊,lz之前是做C++的吗