所有关于一个实体类的servlet封装在一起,成为主servlet的方法,通过反射调用方法 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setChar
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String type=request.getParameter("type"); sus=new StuUserServlet(); for(Method me:sus.getClass().getDeclaredMethods()){ if(me.getName().equalsIgnoreCase(type)){ try { System.out.println(me.getName()); me.invoke(this,request,response); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } } @SuppressWarnings("all") private void addStuUser(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ String username=URLDecoder.decode(request.getParameter("username"), "utf-8"); String password=request.getParameter("password"); int sno=Integer.parseInt(request.getParameter("sno")); String sql="insert into stuuser values(?,?,?)"; ArrayList list=new ArrayList(); list.add(username); list.add(password); list.add(sno); sub.CUDStuUser(sql, list); sus.pagHref(request, response); // response.sendRedirect("StuUserServlet?type=pagHref&pageIndex="+request.getParameter("pageIndex")+"&pageSize="+request.getParameter("pageSize")+"&maxIndex="+request.getParameter("maxIndex")+""); } @SuppressWarnings("all") private void alterStuUser(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ String username=URLDecoder.decode(request.getParameter("username"), "utf-8"); String password=request.getParameter("password"); int sno=Integer.parseInt(request.getParameter("sno")); int id=Integer.parseInt(request.getParameter("id")); String sql="update stuuser set username=?,password=?,sno=? where sno=? "; @SuppressWarnings("rawtypes") ArrayList list=new ArrayList(); list.add(username); list.add(password); list.add(sno); list.add(id); sub.CUDStuUser(sql, list); // response.sendRedirect("StuUserServlet?type=pagHref&pageIndex="+request.getParameter("pageIndex")+"&pageSize="+request.getParameter("pageSize")+"&maxIndex="+request.getParameter("maxIndex")+""); sus.pagHref(request, response); } @SuppressWarnings("unused") private void delStuUser(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ int id=Integer.parseInt(request.getParameter("id")); String sql="delete from stuuser where sno=?"; sub.delete(sql, id); System.out.println(request.getParameter("pageIndex")); // request.getRequestDispatcher("StuUserServlet?type=pagHref&pageIndex="+request.getParameter("pageIndex")+"&pageSize="+request.getParameter("pageSize")+"&maxIndex="+request.getParameter("maxIndex")+"").forward(request, response);; sus.pagHref(request, response); } @SuppressWarnings("all") private void loginCheck(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{ String name=request.getParameter("userName"); String pwd=request.getParameter("passWord"); if(name!=null&&pwd!=null&&name.length()>=4&&name.length()<=15&&pwd.length()>=3&&pwd.length()<=15){ String sql="select * from stuuser where username=? and password=?"; ArrayList list=new ArrayList(); ArrayListslist=new ArrayList (); list.add(name); list.add(pwd); slist=sub.login(sql, list); if(slist.size()>0){ request.getSession().setAttribute("stuUser", slist.get(0)); // request.getRequestDispatcher("StuUserServlet?type=UserAll").forward(request, response); sus.userAll(request, response); }else{ System.out.println("...1...."); request.setAttribute("msg", "用户名或者密码输入错误!"); request.getRequestDispatcher("/login.jsp").forward(request, response); } }else{ System.out.println(".2......"); request.setAttribute("msg", "用户名或者密码格式输入错误!"); request.getRequestDispatcher("/login.jsp").forward(request, response); } } @SuppressWarnings({ "rawtypes", "unchecked" }) private void pagHref(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ int pageIndex=Integer.parseInt(request.getParameter("pageIndex")); int pageSize=Integer.parseInt(request.getParameter("pageSize")); int maxIndex=Integer.parseInt(request.getParameter("maxIndex")); String sql="select rnum,username,password,sno from (select rownum rnum,s.* from (select * from stuUser order by sno) s) where rnum>? and rnum slist=new ArrayList (); list.add((pageIndex-1)*pageSize); list.add(pageIndex*pageSize+1); slist=(ArrayList ) sub.selStuUser(sql,list); request.setAttribute("slist", slist); request.setAttribute("pageIndex", pageIndex); request.setAttribute("pageSize", pageSize); request.setAttribute("maxIndex", maxIndex); request.getRequestDispatcher("/manager.jsp").forward(request, response); } @SuppressWarnings("all") private void rigister(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ String name=request.getParameter("username"); String password=request.getParameter("password"); String pwd2=request.getParameter("pwd2"); int sno=Integer.parseInt(request.getParameter("sno")); //懒得验证 String sql="select * from stuuser where username=?"; ArrayList list=new ArrayList(); list.add(name); List slist=sub.selStuUser( sql,list); System.out.println(slist.size()); if(slist.size()>0){ request.setAttribute("msg", "用户名已存在!"); request.setAttribute("username", name); request.setAttribute("password", password); request.setAttribute("pwd2", pwd2); request.setAttribute("sno",sno); request.getRequestDispatcher("register.jsp").forward(request, response); }else{ sql="insert into stuuser values(?,?,?)"; list=new ArrayList(); list.add(name); list.add(password); list.add(sno); if(sub.CUDStuUser(sql, list)!=0){ request.setAttribute("username",name); request.setAttribute("password",password); request.getRequestDispatcher("login.jsp").forward(request, response); }else{ request.setAttribute("msg1", "注册失败,请重新注册,或联系管理员!"); request.setAttribute("username", name); request.setAttribute("password", password); request.setAttribute("pwd2", pwd2); request.setAttribute("sno",sno); request.getRequestDispatcher("register.jsp").forward(request, response); } } } private void userAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ String sql="select * from stuuser "; List slist=sub.selStuUser( sql); request.setAttribute("slist", slist); int maxIndex=slist.size()%10==0?slist.size()/10:slist.size()/10+1; request.getRequestDispatcher("StuUserServlet?type=pagHref&maxIndex="+maxIndex+"&pageIndex="+1+"&pageSize="+10+"").forward(request, response); }