package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.Arrays;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.entity.Admin;
import com.entity.Student;
import com.service.AdminService;
import com.service.impl.AdminServiceImpl;
@WebServlet("/servlet/AdminServlet")
public class AdminServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String method = req.getParameter("method");
if ("adminRegister".equals(method)) {
adminRegister(req,resp);
} else if("studentRegister".equals(method)){
studentRegister(req, resp);
}else if("studentShowAll".equals(method)){
studentShowAll(req,resp);
}else if("studentRemove".equals(method)){
studentRemove(req, resp);
}else if ("studentFindOne".equals(method)) {
studentFindOne(req, resp);
}else if("studentChange".equals(method)){
studentChange(req, resp);
}
}
//确认修改然后跳转
protected void studentChange(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
int stuno=Integer.parseInt(req.getParameter("sno"));
String stupwd=req.getParameter("pwd");
String stuname = req.getParameter("name");
String phone = req.getParameter("phone");
String gender = req.getParameter("gender");
Date birth = Date.valueOf(req.getParameter("birth"));
int clazzno = Integer.parseInt(req.getParameter("clazz"));
String remark = req.getParameter("remark");
Student stu = new Student(stuno, stupwd, stuname, phone, gender, birth, clazzno, remark);
AdminService admins = new AdminServiceImpl();
int update = admins.change(stu);
if (update>0) {
resp.sendRedirect("/sscs/servlet/AdminServlet?method=studentShowAll");
}
}
//修改的方法
protected void studentFindOne(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
int sno = Integer.parseInt(req.getParameter("sno"));
AdminService admins = new AdminServiceImpl();
Student stu = admins.findOne(sno);
if (stu!=null) {
req.setAttribute("stu", stu);
req.getRequestDispatcher("/files/studentUpdate.jsp").forward(req, resp);
}
}
//查询全部学生的方法
protected void studentShowAll(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
AdminService admins = new AdminServiceImpl();
List<Student> list = admins.findAll();
if (list.size()>0) {
req.setAttribute("list", list);
req.getRequestDispatcher("/files/studentList.jsp").forward(req, resp);
}
}
//删除单个学生
protected void studentRemove(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
int sno = Integer.parseInt(req.getParameter("sno"));
AdminService admins = new AdminServiceImpl();
int update = admins.remove(sno);
if (update>0) {
resp.sendRedirect("/sscs/servlet/AdminServlet?method=studentShowAll");
}
}
//添加学生的方法
protected void studentRegister(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String stupwd=req.getParameter("pwd");
String stuname = req.getParameter("name");
String phone = req.getParameter("phone");
String gender = req.getParameter("gender");
Date birth = Date.valueOf(req.getParameter("birth"));
int clazzno = Integer.parseInt(req.getParameter("clazz"));
String remark = req.getParameter("remark");
Student stu = new Student(0, stupwd, stuname, phone, gender, birth, clazzno, remark);
AdminService admins = new AdminServiceImpl();
int update = admins.registerstu(stu);
if (update>0) {
resp.sendRedirect(req.getContextPath()+"/servlet/AdminServlet?method=studentShowAll");
}else{
req.setAttribute("msg", "注册失败");
req.getRequestDispatcher("/files/studentRegister.jsp").forward(req, resp);
}
}
//添加管理员的方法
protected void adminRegister(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//接收数据
String userid = req.getParameter("uid");
String username = req.getParameter("uname");
String password = req.getParameter("pwd");
int age = Integer.parseInt(req.getParameter("age"));
float score = Float.parseFloat(req.getParameter("score"));
Date enterDate = Date.valueOf(req.getParameter("enterdate"));
String hobby =Arrays.toString( req.getParameterValues("hobby"));
Admin admin = new Admin(userid, username, password, age, score, enterDate, hobby);
//执行service层方法
AdminService admins = new AdminServiceImpl();
int update = admins.register(admin);
//判断跳转
if (update>0) {
//resp.sendRedirect("/sscs/index.html");
PrintWriter out = resp.getWriter();
out.print("<script type='text/javascript'>");
out.print("window.top.location.href='/sscs/index.html';");
out.print("</script>");
out.close();
}else{
req.setAttribute("msg", "注册失败");
req.getRequestDispatcher("/files/adminRegister.jsp").forward(req, resp);
}
}
}