部分代码:

 

 
  
  1. package com.gym.user.action; 
  2.  
  3. import java.io.IOException; 
  4. import java.io.PrintWriter; 
  5. import java.util.ArrayList; 
  6. import java.util.List; 
  7.  
  8. import javax.servlet.RequestDispatcher; 
  9. import javax.servlet.ServletException; 
  10. import javax.servlet.http.HttpServlet; 
  11. import javax.servlet.http.HttpServletRequest; 
  12. import javax.servlet.http.HttpServletResponse; 
  13.  
  14. import com.gym.model.UserModel; 
  15. import com.gym.user.service.impl.UserServiceImpl; 
  16. import com.gym.utils.CheckOnline; 
  17. import com.gym.utils.Constant; 
  18. import com.gym.utils.Md5; 
  19.  
  20. public class UserAction extends HttpServlet { 
  21.  
  22.     /** 
  23.      * The doGet method of the servlet. <br> 
  24.      *  
  25.      * This method is called when a form has its tag value method equals to get. 
  26.      *  
  27.      * @param request 
  28.      *            the request send by the client to the server 
  29.      * @param response 
  30.      *            the response send by the server to the client 
  31.      * @throws ServletException 
  32.      *             if an error occurred 
  33.      * @throws IOException 
  34.      *             if an error occurred 
  35.      */ 
  36.     public void doGet(HttpServletRequest request, HttpServletResponse response) 
  37.             throws ServletException, IOException { 
  38.  
  39.         request.setCharacterEncoding("utf-8"); 
  40.  
  41.         String action = request.getParameter("action"); 
  42.  
  43.         // 检查用户是否在线 
  44.         if (!CheckOnline.isUserOnline(request)) { 
  45.             response.sendRedirect("../login.html");// 不在线,跳转到登录页面 
  46.             return
  47.         } 
  48.  
  49.         String userId = request.getSession().getAttribute("uId").toString(); 
  50.  
  51.         UserServiceImpl userServiceImpl = new UserServiceImpl(); 
  52.  
  53.         if (action == null) { // 显示个人中心首页 
  54.  
  55.             UserModel userModel = userServiceImpl.queryUserInfoById(userId); 
  56.             List userList = new ArrayList(); 
  57.             userList.add(userModel); 
  58.  
  59.             RequestDispatcher dispatcher = request 
  60.                     .getRequestDispatcher("/usercenter/index.jsp"); 
  61.             request.setAttribute("userList", userList); 
  62.             dispatcher.forward(request, response); 
  63.         } else if (action.equals("alterinfo")) { // 显示修改个人信息页面 
  64.  
  65.             UserModel userModel = userServiceImpl.queryUserInfoById(userId); 
  66.             List userList = new ArrayList(); 
  67.             userList.add(userModel); 
  68.  
  69.             RequestDispatcher dispatcher = request 
  70.                     .getRequestDispatcher("/usercenter/alterinfo.jsp"); 
  71.             request.setAttribute("userList", userList); 
  72.             dispatcher.forward(request, response); 
  73.  
  74.         } else if (action.equals("resetpwd")) { // 显示修改密码页面 
  75.  
  76.             RequestDispatcher dispatcher = request 
  77.                     .getRequestDispatcher("/usercenter/resetpwd.jsp"); 
  78.  
  79.             dispatcher.forward(request, response); 
  80.  
  81.         } else if (action.equals("mybook")) { 
  82.  
  83.             UserModel userModel = new UserModel(); 
  84.             userModel.setuId((String) request.getSession().getAttribute("uId")); 
  85.  
  86.             List myGroundBookList = userServiceImpl.queryMyBook(userModel); 
  87.  
  88.             RequestDispatcher dispatcher = request 
  89.                     .getRequestDispatcher("/usercenter/mybook.jsp"); 
  90.             request.setAttribute("myGroundBookList", myGroundBookList); 
  91.             dispatcher.forward(request, response); 
  92.  
  93.         } else if (action.equals("myrent")) { 
  94.  
  95.             UserModel userModel = new UserModel(); 
  96.             userModel.setuId((String) request.getSession().getAttribute("uId")); 
  97.  
  98.             List myEquipmentRentList = userServiceImpl.queryMyRent(userModel); 
  99.  
  100.             RequestDispatcher dispatcher = request 
  101.                     .getRequestDispatcher("/usercenter/myrent.jsp"); 
  102.             request.setAttribute("myEquipmentRentList", myEquipmentRentList); 
  103.             dispatcher.forward(request, response); 
  104.  
  105.         } else { 
  106.  
  107.             RequestDispatcher dispatcher = request 
  108.                     .getRequestDispatcher("/error.jsp"); 
  109.  
  110.             dispatcher.forward(request, response); 
  111.  
  112.         } 
  113.  
  114.     } 
  115.  
  116.     /** 
  117.      * The doPost method of the servlet. <br> 
  118.      *  
  119.      * This method is called when a form has its tag value method equals to 
  120.      * post. 
  121.      *  
  122.      * @param request 
  123.      *            the request send by the client to the server 
  124.      * @param response 
  125.      *            the response send by the server to the client 
  126.      * @throws ServletException 
  127.      *             if an error occurred 
  128.      * @throws IOException 
  129.      *             if an error occurred 
  130.      */ 
  131.     public void doPost(HttpServletRequest request, HttpServletResponse response) 
  132.             throws ServletException, IOException { 
  133.         request.setCharacterEncoding("utf-8"); 
  134.  
  135.         String action = request.getParameter("action"); 
  136.  
  137.         // 检查用户是否在线 
  138.         if (!CheckOnline.isUserOnline(request)) { 
  139.             response.sendRedirect("../login.html"); 
  140.             return
  141.         } 
  142.  
  143.         String userId = request.getSession().getAttribute("uId").toString(); 
  144.  
  145.         UserServiceImpl userServiceImpl = new UserServiceImpl(); 
  146.         UserModel userModel = new UserModel(); 
  147.  
  148.         if (action == null) { 
  149.  
  150.         } else if (action.equals("alter")) { // 执行修改个人信息动作 
  151.  
  152.             userModel.setuId(userId); 
  153.             userModel.setuEmail(request.getParameter("email")); 
  154.             userModel.setuIdCard(request.getParameter("idcard")); 
  155.             userModel.setuPhone(request.getParameter("phone")); 
  156.             userModel.setuName(request.getParameter("name")); 
  157.  
  158.             switch (userServiceImpl.alterUserInfo(userModel)) { 
  159.             case Constant.SUCCESS: 
  160.                 request.getSession() 
  161.                         .setAttribute("uName", userModel.getuName()); // 更新session 
  162.  
  163.                 response.sendRedirect("../success.jsp"); 
  164.                 break
  165.  
  166.             case Constant.ERROR: 
  167.                 response.sendRedirect("../error.jsp?errorCode=" 
  168.                         + Constant.ERROR); 
  169.                 break
  170.  
  171.             default
  172.                 break
  173.             } 
  174.  
  175.         } else if (action.equals("resetpwd")) { // 执行修改密码动作 
  176.  
  177.             userModel.setuId(userId); 
  178.             String oldPwd = request.getParameter("oldpwd"); 
  179.             String newPwd1 = request.getParameter("newpwd1"); 
  180.             String newPwd2 = request.getParameter("newpwd2"); 
  181.  
  182.             switch (userServiceImpl.alterUserPwd(userModel, oldPwd, newPwd1, 
  183.                     newPwd2)) { 
  184.             case Constant.SUCCESS: 
  185.                 request.getSession() 
  186.                         .setAttribute("uName", userModel.getuName()); // 更新session 
  187.                 request.getSession().setAttribute("uId", userModel.getuId()); // 更新session 
  188.  
  189.                 response.sendRedirect("../success.jsp"); 
  190.                 break
  191.  
  192.             case Constant.ERROR: 
  193.                 response.sendRedirect("../error.jsp?errorCode=" 
  194.                         + Constant.ERROR); 
  195.                 break
  196.  
  197.             case Constant.USERPWDERROR: 
  198.                 response.sendRedirect("../error.jsp?errorCode=" 
  199.                         + Constant.USERPWDERROR); 
  200.                 break
  201.  
  202.             case Constant.PASSWORDDIFFER: 
  203.                 response.sendRedirect("../error.jsp?errorCode=" 
  204.                         + Constant.PASSWORDDIFFER); 
  205.                 break
  206.  
  207.             default
  208.                 break
  209.             } 
  210.  
  211.         } 
  212.  
  213.     } 

 

转自: 广东海洋大学体育馆管理系统 源代码 - jsp代码库 - 云代码 http://yuncode.net/code/c_511527388c75a79