×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Nhan Tran
Added: Aug 8, 2021 3:22 AM
Views: 3674
Tags: no tags
  1. private static final String HOME_PAGE = "SearchAccountServlet";
  2.     static Logger log = Logger.getLogger(UpdateAccountServlet.class);
  3.  
  4.     /**
  5.      * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  6.      * methods.
  7.      *
  8.      * @param request servlet request
  9.      * @param response servlet response
  10.      * @throws ServletException if a servlet-specific error occurs
  11.      * @throws IOException if an I/O error occurs
  12.      */
  13.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  14.             throws ServletException, IOException {
  15.         response.setContentType("text/html;charset=UTF-8");
  16.         String url = HOME_PAGE;
  17.         String username, password, role, id;
  18.         id = request.getParameter("aid");
  19.         username = request.getParameter("txtUsername");
  20.         password = request.getParameter("txtPassword");
  21.         role = request.getParameter("txtRole");
  22.         try {
  23.  
  24.             AccountDAO dao = new AccountDAO();
  25.             if (password != null && username != null) {
  26.                 Account a = new Account();
  27.                 a.setId(id);
  28.                 a.setPassword(password);
  29.                 a.setUsername(username);
  30.                 a.setRole(role);
  31.                 dao.update(a);
  32.             }
  33.             if (username == null) {
  34.                 Account a = dao.getAccount(id);
  35.                 request.setAttribute("acc", a);
  36.                 url = "Update.jsp";
  37.             }
  38.         } catch (SQLException | NamingException ex) {
  39.             log.warn(ex.getClass().getSimpleName() + ":  " + ex.getMessage());
  40.         } finally {
  41.             request.getRequestDispatcher(url).forward(request, response);
  42.         }
  43.     }