package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.SplitPageService;
import dao.StudentsDao;
import entity.Students;
import dao.UsersInfoDao;
import entity.UsersInfo;
public class StudentsServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String type=request.getParameter("type");
//模糊查询
if(type!=null&&type.equals("dim")){
String keywords=request.getParameter("txtkeywords");
List<Students> cardtypeList=StudentsDao.dimFindStudents(keywords);
request.setAttribute("cardTypeListSer", cardtypeList);
request.getRequestDispatcher("StudentsList.jsp?dim="+keywords.trim()).forward(request, response);
return;
}//修改
else if(type!=null&&type.equals("query")){
int id=Integer.parseInt(request.getParameter("studentid"));
Students cardType=StudentsDao.getStudentsWhere(" StudentID='"+id+"' ");
request.setAttribute("cardType", cardType);
request.getRequestDispatcher("StudentsList.jsp").forward(request, response);
return;
}//更新
else if(type!=null&&type.equals("update")){
String studentid=request.getParameter("studentid").toString();
String studentname=request.getParameter("studentname").toString();
String sex=request.getParameter("selectid").toString();
String nativePlace=request.getParameter("nativePlace").toString();
String address=request.getParameter("address").toString();
String department=request.getParameter("department").toString();
String professional=request.getParameter("professional").toString();
String dorm=request.getParameter("dorm").toString();
String phone=request.getParameter("phone").toString();
String score=request.getParameter("score").toString();
Students stu=new Students();
stu.setStudentID(studentid);
stu.setStudentName(studentname);
stu.setSex(sex);
stu.setNativePlace(nativePlace);
stu.setAddress(address);
stu.setDepartment(department);
stu.setProfessional(professional);
stu.setDorm(dorm);
stu.setPhone(phone);
stu.setScore(score);
int result=StudentsDao.updateStudents(stu);
if(result>0){
out.println("<script>alert('修改成功!');location.href='StudentsServlet';</script>");
}else{
out.println("<script>alert('修改失败!);history.go(-1);</script>");
}
return;
}else if(type!=null&&type.equals("add")){
String studentid=request.getParameter("astudentid").toString();
String studentname=request.getParameter("astudentname").toString();
String sex=request.getParameter("aselectid").toString();
String nativePlace=request.getParameter("anativePlace").toString();
String address=request.getParameter("aaddress").toString();
String department=request.getParameter("adepartment").toString();
String professional=request.getParameter("aprofessional").toString();
String dorm=request.getParameter("adorm").toString();
String phone=request.getParameter("aphone").toString();
String score=request.getParameter("ascore").toString();
int num=StudentsDao.selectStudentsId(studentid);
if(num>0)
{
out.println("<script language=\"javascript\">alert(\"已经存在该学号!\");" +
"window.location.href=\"StudentsServlet\";</script>");
}else{
UsersInfo info=new UsersInfo();
info.setUserName(studentid);
info.setPassWord(studentid);
info.setUserRole(1);
info.setUserState("0");
UsersInfoDao.insertUserInfo(info);
Students stu=new Students();
stu.setStudentID(studentid);
stu.setStudentName(studentname);
stu.setSex(sex);
stu.setNativePlace(nativePlace);
stu.setAddress(address);
stu.setDepartment(department);
stu.setProfessional(professional);
stu.setDorm(dorm);
stu.setPhone(phone);
stu.setScore(score);
int result=StudentsDao.insertStudents(stu);
if(result>0){
out.println("<script>alert('新增成功!');location.href='StudentsServlet';</script>");
}else{
out.println("<script>alert('新增失败!);history.go(-1);</script>");
}
}
return;
}else if(type!=null&&type.equals("del")){
String id=request.getParameter("id");
int result=StudentsDao.delStudents(id);
if(result>0){
out.println("<script>alert('删除成功!');location.href='StudentsServlet';</script>");
}else{
out.println("<script>alert('删除失败!);location.href='StudentsServlet';</script>");
}
}
//默认查询所有
String oper=request.getParameter("oper");//是首页、上一页、下一页、尾页
//得到总行数
SplitPageService.setRowCount(StudentsDao.getStudentsCount());
//得到总页数
SplitPageService.setPageCount();
//得到当前页
SplitPageService.setCurPage(oper);
//分页查询
List<Students> cardtypeList=StudentsDao.selectStudents(SplitPageService.curPage,SplitPageService.pageSize);
request.setAttribute("cardTypeList", cardtypeList);
//得到起始位置
int start=(SplitPageService.curPage-1)*SplitPageService.pageSize;
request.setAttribute("start",start);
request.getRequestDispatcher("StudentsList.jsp").forward(request, response);
}
}
评论0
最新资源