UsersServlet.java package com.jxnd.servlet;import java.io.IOException;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.List;import javax.servlet.ServletException;import javax.servlet.annota
package com.jxnd.servlet; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; 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.jxnd.bean.StuBean; import com.jxnd.biz.IStuBiz; import com.jxnd.biz.StuBizImpl; import com.jxnd.tools.Tools; @WebServlet("/UsersServlet") @SuppressWarnings("all") public class UsersServlet extends HttpServlet { private static final long serialVersionUID = 1L; IStuBiz biz = new StuBizImpl(); Class c = UsersServlet.class; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置编码格式 request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); //获取方法类型 String type = request.getParameter("type"); try { Method me = c.getMethod(type, HttpServletRequest.class,HttpServletResponse.class); me.invoke(this, request,response); } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { e.printStackTrace(); } } /** * 修改密码的方法 * @param request * @param response * @throws ServletException * @throws IOException */ public void updatePassword(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ StuBean bean = (StuBean) request.getSession().getAttribute("bean"); String stuName = bean.getStuName(); String op = request.getParameter("op"); String np = request.getParameter("np1"); String msg = ""; int i = biz.updatePassword(stuName, op, np); if(i == 3){ bean.setPassword(np); request.getSession().setAttribute("bean", bean); response.sendRedirect("UsersServlet?type=getStuPage&pageIndex=1"); return; }if(i == 1){ msg = "原始密码错误!"; }if(i == 2){ msg = "服务器错误!"; } request.setAttribute("errorMsg", msg); request.getRequestDispatcher("/updatePassword.jsp").forward(request, response); } /** * 分页的方法 * @param request * @param response * @throws ServletException * @throws IOException */ public void getStuPage(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ StuBean bean = new StuBean(); //获取传过来的页数指针以及页数大小 int pageIndex = Integer.parseInt(request.getParameter("pageIndex")); int pageSize = Integer.parseInt(request.getParameter("pageSize")==null?"10":request.getParameter("pageSize")); int count = biz.getCount(bean); Listlist = biz.getStuPage(pageIndex, pageSize); request.setAttribute("list", list); request.setAttribute("pageIndex", pageIndex); request.setAttribute("pageSize", pageSize); request.setAttribute("count", count); request.setAttribute("pageMsg", Tools.paging(count, pageSize, pageIndex, "UsersServlet?type=getStuPage")); request.getRequestDispatcher("success.jsp").forward(request, response); } /** * 登录 * @param request * @param response * @throws ServletException * @throws IOException */ public void login(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ //获取表单中的数据 String stuName = request.getParameter("stuName"); String password = request.getParameter("password"); StuBean bean = new StuBean(); //封装表单数据 bean.setStuName(stuName); bean.setPassword(password); //基本验证 if(stuName != null && !"".equals(stuName) && password != null && !"".equals(password)){ //内容验证,过滤非法文字,此处省略...... //boolean flag = biz.login(stuName, password); boolean flag = biz.conn(bean); if(flag){ request.getSession().setAttribute("bean", bean); response.sendRedirect("/StudentProject/UsersServlet?type=getStuPage&pageIndex=1&pageSize=10"); }else{ request.getRequestDispatcher("error.jsp").forward(request, response); } } } /** * 封装数据,将数据放到修改页面中 * @param request * @param response * @throws ServletException * @throws IOException */ public void updateStuServlet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ //获取一个StuBean对象*** StuBean bean = new StuBean(); //获取业务逻辑功能类 IStuBiz biz = new StuBizImpl(); //获取修改对象的ID int id = Integer.parseInt(request.getParameter("id")); //获取数据BEAN //StuBean bean = biz.findUserById(id); bean = biz.getBean(bean, id); //将数据BEAN设置到request内置对象中 request.setAttribute("bean", bean); //跳转到相应的jsp页面中 request.getRequestDispatcher("update.jsp").forward(request, response); } /** * 根据id删除数据 * @param request * @param response * @throws ServletException * @throws IOException */ public void delStuById(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ //获取一个StuBean对象 StuBean bean = new StuBean(); //获取表单数据 int id = Integer.parseInt(request.getParameter("id").trim()); bean.setId(id); //int flag = biz.delStuById(id); int flag = biz.deleteById(bean); if(flag > 0){ request.getRequestDispatcher("UsersServlet?type=getStuPage").forward(request, response); } } /** * 注册功能 * @param request * @param response * @throws ServletException * @throws IOException */ public void register(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ //获取一个StuBean对象 StuBean bean = new StuBean(); //获取表单中的数据 bean.setId(Integer.parseInt(request.getParameter("id"))); bean.setStuName(request.getParameter("stuName")); bean.setPassword(request.getParameter("password")); bean.setSex(request.getParameter("sex")); boolean flag = biz.register(bean); if(flag){ request.setAttribute("msg", "注册成功!"); request.getRequestDispatcher("login.jsp").forward(request, response); }else{ request.getRequestDispatcher("error2.jsp").forward(request, response); } } /** * 修改数据 * @param request * @param response * @throws ServletException * @throws IOException */ public void modifyStu(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ //创建一个数据BENA StuBean bean = new StuBean(); //封装表单中的数据 bean.setStuName(request.getParameter("stuName")); bean.setPassword(request.getParameter("password")); bean.setSex(request.getParameter("sex")); //获取表单中ID int id = Integer.parseInt(request.getParameter("id")); bean.setId(id); int pageIndex = Integer.parseInt(request.getParameter("pageIndex")); int pageSize = Integer.parseInt(request.getParameter("pageSize")); //boolean flag = biz.modifyStu(bean, id); int flag = biz.modifyById(bean); if(flag>0){ request.getRequestDispatcher("UsersServlet?type=getStuPage&pageIndex="+pageIndex+"&pageSize="+pageSize).forward(request, response); } } }