Guest User

Untitled

a guest
Jun 17th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. public class LoginServlet extends HttpServlet {
  2.  
  3. /**
  4. * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  5. * methods.
  6. *
  7. * @param request servlet request
  8. * @param response servlet response
  9. * @throws ServletException if a servlet-specific error occurs
  10. * @throws IOException if an I/O error occurs
  11. * @throws java.lang.ClassNotFoundException
  12. * @throws TDA.ListaException
  13. */
  14.  
  15.  
  16. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  17. throws ServletException, IOException, ClassNotFoundException, ListaException {
  18. response.setContentType("text/html;charset=UTF-8");
  19. try (PrintWriter out = response.getWriter()) {
  20. /* TODO output your page here. You may use following sample code. */
  21. UserFile userF = new UserFile();
  22. String email = request.getParameter("email");
  23. String pass = request.getParameter("pwd");
  24. if (email != null) {
  25. if (userF.validarUsuario(email, pass)) {
  26. HttpSession sesion = request.getSession(true);
  27. sesion.setAttribute("usuario", email);
  28. response.sendRedirect("inicio.htm");
  29. } else {
  30. out.println("<h2>Fall&oacute; el inicio de sisi&oacuten</h2>");
  31. }
  32. }
  33. }
  34. }
  35.  
  36. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  37. /**
  38. * Handles the HTTP <code>GET</code> method.
  39. *
  40. * @param request servlet request
  41. * @param response servlet response
  42. * @throws ServletException if a servlet-specific error occurs
  43. * @throws IOException if an I/O error occurs
  44. */
  45. @Override
  46. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  47. throws ServletException, IOException {
  48. try {
  49. processRequest(request, response);
  50. } catch (ClassNotFoundException | ListaException ex) {
  51. Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
  52. }
  53. }
  54.  
  55. /**
  56. * Handles the HTTP <code>POST</code> method.
  57. *
  58. * @param request servlet request
  59. * @param response servlet response
  60. * @throws ServletException if a servlet-specific error occurs
  61. * @throws IOException if an I/O error occurs
  62. */
  63. @Override
  64. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  65. throws ServletException, IOException {
  66. try {
  67. processRequest(request, response);
  68. } catch (ClassNotFoundException | ListaException ex) {
  69. Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
  70. }
  71. }
  72.  
  73. /**
  74. * Returns a short description of the servlet.
  75. *
  76. * @return a String containing servlet description
  77. */
  78. @Override
  79. public String getServletInfo() {
  80. return "Short description";
  81. }// </editor-fold>
  82.  
  83. }
  84.  
  85. public boolean validarUsuario(String correo, String pass) throws IOException, ClassNotFoundException, ListaException {
  86. File file = new File("./UserData.dat");
  87. ListaEnlazada listaE = readUser(file);
  88. UserInfo user;
  89. boolean correcto = false;
  90.  
  91. for (int i = 1; i <= listaE.getSize(); i++) {
  92. user = (UserInfo) listaE.getNodo(i).elemento;
  93. if (user.getCorreo().equals(correo) && user.getPass().equals(pass)) {
  94. correcto = true;
  95. }
  96. }
  97. return correcto;
  98. }
  99.  
  100. public ListaEnlazada readUser(File file) throws IOException, ClassNotFoundException {
  101. ListaEnlazada listaE = new ListaEnlazada();
  102. //Valida si el archivo existe
  103. if (file.exists()) {
  104. try ( //Lee el archivo
  105. ObjectInputStream objectInput = new ObjectInputStream(new FileInputStream(file))) {
  106. Object listTemp = objectInput.readObject();
  107. //casting del objeto
  108. listaE = (ListaEnlazada) listTemp;
  109. objectInput.close();
  110. }
  111. }
  112. return listaE;
  113. }
Add Comment
Please, Sign In to add comment