package com.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.model.bean.UserInfo;
import com.model.service.UserInfoService;
/**
-
Servlet implementation class UserInfoServlet
*/
public class UserInfoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;/**
- @see HttpServlet#HttpServlet()
*/
public UserInfoServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
- @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
/**
-
@see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding("utf-8");
System.out.println("进入dopost方法");
UserInfo user=new UserInfo();
getParam(request,user);
String error=validate(user);
if("".equals(error)){
//校验通过
try {
//校验用户名是否重复
boolean flag=UserInfoService.getInstance().valiadateUsername(user); //返回true即重复
if(flag){ //用户名重复 返回该页面 即请求转发
//请求转发的实现方式
RequestDispatcher rd=request.getRequestDispatcher("User.html");
rd.forward(request, response);
}else{//用户名不重复 跳转页面 即重定向方式
UserInfoService.getInstance().saveUser(user);
//重定向的方式跳转到result.html页面
response.sendRedirect("login.html");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
//校验不通过
//请求转发实现
RequestDispatcher rd=request.getRequestDispatcher("User.html");
rd.forward(request, response);
}
- @see HttpServlet#HttpServlet()
}
private String validate(UserInfo user) {
StringBuffer sbf=new StringBuffer("");
//用户名不能为空
if(user.getUsername()==null||"".equals(user.getUsername())){
sbf.append("用户名不为空");
}
if(user.getPhone()!=null&&user.getPhone().length()!=11){
sbf.append("电话号码必须是11位");
}
return sbf.toString();
}
private void getParam(HttpServletRequest request, UserInfo user) {
String username=request.getParameter("username");
String password=request.getParameter("password");
String newpassword=request.getParameter("newpassword");
String name=request.getParameter("name");
String email=request.getParameter("email");
String phone=request.getParameter("phone");
String role=request.getParameter("role");
String skin=request.getParameter("skin");
user.setUsername(username);
user.setPassword(password);
user.setNewpassword(newpassword);
user.setName(name);
user.setEmail(email);
user.setPhone(phone);
user.setRole(role);
user.setSkin(skin);
}
}
网友评论