Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LoginServlet extends HttpServlet {
- /**
- * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
- * methods.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- * @throws java.lang.ClassNotFoundException
- * @throws TDA.ListaException
- */
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException, ClassNotFoundException, ListaException {
- response.setContentType("text/html;charset=UTF-8");
- try (PrintWriter out = response.getWriter()) {
- /* TODO output your page here. You may use following sample code. */
- UserFile userF = new UserFile();
- String email = request.getParameter("email");
- String pass = request.getParameter("pwd");
- if (email != null) {
- if (userF.validarUsuario(email, pass)) {
- HttpSession sesion = request.getSession(true);
- sesion.setAttribute("usuario", email);
- response.sendRedirect("inicio.htm");
- } else {
- out.println("<h2>Falló el inicio de sisión</h2>");
- }
- }
- }
- }
- // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
- /**
- * Handles the HTTP <code>GET</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- try {
- processRequest(request, response);
- } catch (ClassNotFoundException | ListaException ex) {
- Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- /**
- * Handles the HTTP <code>POST</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- try {
- processRequest(request, response);
- } catch (ClassNotFoundException | ListaException ex) {
- Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- /**
- * Returns a short description of the servlet.
- *
- * @return a String containing servlet description
- */
- @Override
- public String getServletInfo() {
- return "Short description";
- }// </editor-fold>
- }
- public boolean validarUsuario(String correo, String pass) throws IOException, ClassNotFoundException, ListaException {
- File file = new File("./UserData.dat");
- ListaEnlazada listaE = readUser(file);
- UserInfo user;
- boolean correcto = false;
- for (int i = 1; i <= listaE.getSize(); i++) {
- user = (UserInfo) listaE.getNodo(i).elemento;
- if (user.getCorreo().equals(correo) && user.getPass().equals(pass)) {
- correcto = true;
- }
- }
- return correcto;
- }
- public ListaEnlazada readUser(File file) throws IOException, ClassNotFoundException {
- ListaEnlazada listaE = new ListaEnlazada();
- //Valida si el archivo existe
- if (file.exists()) {
- try ( //Lee el archivo
- ObjectInputStream objectInput = new ObjectInputStream(new FileInputStream(file))) {
- Object listTemp = objectInput.readObject();
- //casting del objeto
- listaE = (ListaEnlazada) listTemp;
- objectInput.close();
- }
- }
- return listaE;
- }
Add Comment
Please, Sign In to add comment