package cn.eb.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.eb.biz.IUserBiz;
import cn.eb.biz.impl.UserBizImpl;
import cn.eb.entity.User;
public class UserServlet extends HttpServlet {
private IUserBiz userBiz= new UserBizImpl();
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action=request.getParameter("action");
//用户登陆
if(action.equals("login")){
String userId= request.getParameter("userId");
String password=request.getParameter("password");
String code=request.getParameter("code");
String numrand=(String) request.getSession().getAttribute("numrand");
User user =null;
try {
user = userBiz.findById(userId,password);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(user!=null && code.equals(numrand) ){
request.getSession().setAttribute("user", user);
response.sendRedirect("index.jsp");
}else{
response.sendRedirect("login.jsp");
}
}
//用户注销,仅删除session中的user
if(action.equals("remove")){
request.getSession().removeAttribute("user");
response.sendRedirect("index.jsp");
}
//用户注册
if(action.equals("add")){
String userId=request.getParameter("userId");
String userName=request.getParameter("userName");
String password=request.getParameter("password");
//转换性别类型
String sexstring=request.getParameter("sex");
boolean sex=true;
if(sexstring=="male"){
sex=true;
}else{sex=false;}
Date birthday=Date.valueOf(request.getParameter("userId"));
String identityCode=request.getParameter("identityCode");
String email=request.getParameter("email");
String mobile=request.getParameter("mobile");
String address=request.getParameter("address");
int status=1;
float login=1;
int ret=0;
try {
ret=userBiz.add(userId,userName,password,sex,birthday,identityCode,email,mobile,address,login,status);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(ret>0){
response.sendRedirect("success.jsp");
}else{
response.sendRedirect("error.jsp");
}
}
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}