以上是控制台打印的结果,和数据库中保存的结果。
tomcat中的conf里的server.xmf添加了utf-8了,传到数据库里面就成乱码了
Jsp页面如下
String path = request.getContextPath();
%>
注册页面DIV {
WIDTH: 200px;
}
BODY {
MARGIN-TOP: 50px;
FONT-SIZE: 16px;
}
function dosubmit() {
var th = document.form1;
if (th.username.value == "") {
alert("用户名不能为空");
th.username.focus();
return;
}
if (th.realname.value == "") {
alert("姓名 不能为空");
th.realname.focus();
return;
}
if (th.password.value == "") {
alert("密码不能为空");
th.password.focus();
return;
}
th.action="/servlet/RegisterServlet"
th.submit();
}
servle如下:
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import po.Users;
import service.RegisterService;
import service.RegisterServiceImpl;
/**
* Servlet implementation class RegisterServlet
*/
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private RegisterService service = new RegisterServiceImpl();
/**
* @see HttpServlet#HttpServlet()
*/
public RegisterServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String path = request.getContextPath();
PrintWriter out = response.getWriter();
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String realname = request.getParameter("realname");
Users u= new Users();
u.setUsername(username);
u.setPassword(password);
u.setRealname(realname);
System.out.println(u.getUsername()+","+u.getPassword()+","+u.getRealname());
boolean flag = service.registerUser(u);
if(flag)
{
response.sendRedirect(path+"/register_success.jsp");
out.println("注册成功");
}
else
{
response.sendRedirect(path+"/register_failure.jsp");
out.println("注册失败");
}
}
}