package cn.csdn.web.servlet;
import java.io.IOException;
import java.sql.Blob;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.csdn.web.domain.User;
import cn.csdn.web.service.UserService;
import cn.csdn.web.service.UserServiceImpl;
public class RegisterUserServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/* 第一步:设置编码 */
request.setCharacterEncoding("UTF-8");
/* 第二步:获取请求参数 */
String uname = request.getParameter("uname");
String upass = request.getParameter("upass");
String usex = request.getParameter("usex");
String uage = request.getParameter("uage");
String ubirth = request.getParameter("ubirth");
String usalary = request.getParameter("usalary");
/* 第三步:类型转换 */
Integer age = Integer.parseInt(uage);
DateFormat format = DateFormat.getDateInstance();
Date birth =null;
try {
birth = format.parse(ubirth);
} catch (ParseException e) {
System.out.println("异常产生了。。。。。。");
e.printStackTrace();
}
Double salary = Double.valueOf(usalary);
/* 第四步:创建实体对象 */
User entity = new User();
/* 第五步:为实体对象的属性赋值 */
entity.setName(uname);
entity.setPass(upass);
entity.setSex(usex);
entity.setAge(age);
/* 注册日期 为当前日期 */
entity.setRdate(new Timestamp(System.currentTimeMillis()));
entity.setBirth(birth);
entity.setSalary(salary);
/* 第六步:执行业务操作对象 */
/* 6.1创建业务对象 */
UserService uService = new UserServiceImpl();
/* 6.2执行业务操作 */
boolean flag = uService.insert(entity);
/* 第七步:执行判断 */
if (flag) {
// 注册成功
response.sendRedirect("../login/rsuccess.html");
} else {
// 注册失败
response.sendRedirect("../login/rerror.html");
}
}
}