package com.servlet;
import java.io.IOException;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.entity.Person;
import com.service.PersonService;
import com.service.impl.PersonServiceImpl;
@WebServlet("/DoUpdateOrAddPersonServlet")
public class DoUpdateOrAddPersonServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String strPid=request.getParameter("pid");
String pname=request.getParameter("pname");
int page=Integer.parseInt(request.getParameter("page"));
String strBir=request.getParameter("pbir");
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date pbir=null;
try {
pbir = new Date(sdf.parse(strBir).getTime());
} catch (ParseException e) {
e.printStackTrace();
}
int gradeid=Integer.parseInt(request.getParameter("gradeid"));
String pwd=request.getParameter("pwd");
PersonService ps=new PersonServiceImpl();
if(strPid!=null && !(strPid.equals(""))) {
int pid=Integer.parseInt(strPid);
if(ps.updatePersonById(new Person(pid, pname, page, pbir, pwd, gradeid))==1) {
response.sendRedirect("/PersonManage/GetAllPersonServlet");
}
}else {
if(ps.addPerson(new Person(pname, page, pbir, pwd, gradeid))==1) {
Cookie ck=new Cookie("regName", pname);
response.addCookie(ck);
response.getWriter().print("注册成功,3秒以后跳转到登录界面…………");
response.setHeader("refresh", "3,url='/PersonManage/login.jsp'");
}
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
|