Advertisement
Guest User

register

a guest
May 9th, 2016
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package onlinelibrary.servlets;
  7.  
  8. import java.io.IOException;
  9. import java.io.PrintWriter;
  10. import javax.servlet.ServletException;
  11. import javax.servlet.annotation.WebServlet;
  12. import javax.servlet.http.HttpServlet;
  13. import javax.servlet.http.HttpServletRequest;
  14. import javax.servlet.http.HttpServletResponse;
  15. import onlinelibrary.model.Dao;
  16. import onlinelibrary.model.bean.User;
  17.  
  18. /**
  19. *
  20. * @author Balint
  21. */
  22. @WebServlet(name = "Register", urlPatterns = {"/Register"})
  23. public class Register extends HttpServlet {
  24.  
  25. /**
  26. * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  27. * methods.
  28. *
  29. * @param request servlet request
  30. * @param response servlet response
  31. * @throws ServletException if a servlet-specific error occurs
  32. * @throws IOException if an I/O error occurs
  33. */
  34. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  35. throws ServletException, IOException {
  36. response.setContentType("text/html;charset=UTF-8");
  37. try (PrintWriter out = response.getWriter()) {
  38. /* TODO output your page here. You may use following sample code. */
  39. out.println("<!DOCTYPE html>");
  40. out.println("<html>");
  41. out.println("<head>");
  42. out.println("<title>Servlet Register</title>");
  43. out.println("</head>");
  44. out.println("<body>");
  45. out.println("<h1>Servlet Register at " + request.getContextPath() + "</h1>");
  46. out.println("</body>");
  47. out.println("</html>");
  48. }
  49. }
  50.  
  51. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  52. /**
  53. * Handles the HTTP <code>GET</code> method.
  54. *
  55. * @param request servlet request
  56. * @param response servlet response
  57. * @throws ServletException if a servlet-specific error occurs
  58. * @throws IOException if an I/O error occurs
  59. */
  60. @Override
  61. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  62. throws ServletException, IOException {
  63. processRequest(request, response);
  64. }
  65.  
  66. /**
  67. * Handles the HTTP <code>POST</code> method.
  68. *
  69. * @param request servlet request
  70. * @param response servlet response
  71. * @throws ServletException if a servlet-specific error occurs
  72. * @throws IOException if an I/O error occurs
  73. */
  74. @Override
  75. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  76. throws ServletException, IOException {
  77.  
  78. Dao dao = new Dao();
  79. User user = new User();
  80. String name = request.getParameter("name");
  81. String email = request.getParameter("email");
  82. String password = request.getParameter("password");
  83. String adress = request.getParameter("adress");
  84.  
  85. user.setName(name);
  86. user.setEmail(email);
  87. user.setPassword(password);
  88. user.setAddress(adress);
  89.  
  90. if(!dao.checkEmail(email)){
  91. dao.addNewUser(user);
  92. response.sendRedirect("success.jsp");
  93. }else{
  94. response.sendRedirect("fail_registration.jsp");
  95. }
  96.  
  97.  
  98.  
  99.  
  100. processRequest(request, response);
  101. }
  102.  
  103. /**
  104. * Returns a short description of the servlet.
  105. *
  106. * @return a String containing servlet description
  107. */
  108. @Override
  109. public String getServletInfo() {
  110. return "Short description";
  111. }// </editor-fold>
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement