Advertisement
Guest User

Untitled

a guest
Apr 25th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.03 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 amm.milestone;
  7.  
  8. import java.io.IOException;
  9. import java.util.ArrayList;
  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 javax.servlet.http.HttpSession;
  16.  
  17. /**
  18.  *
  19.  * @author Carlo
  20.  */
  21. @WebServlet(name = "Login", urlPatterns = {"/Login"})
  22. public class Login extends HttpServlet {
  23.  
  24.     /**
  25.      * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  26.      * methods.
  27.      *
  28.      * @param request servlet request
  29.      * @param response servlet response
  30.      * @throws ServletException if a servlet-specific error occurs
  31.      * @throws IOException if an I/O error occurs
  32.      */
  33.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  34.             throws ServletException, IOException {
  35.         response.setContentType("text/html;charset=UTF-8");
  36.        
  37.         HttpSession session = request.getSession(true);
  38.         Boolean autenticazioneRiuscita = false;
  39.        
  40.         if(request.getParameter("Submit") != null){
  41.             // Preleva i dati inviati
  42.             String username = request.getParameter("Username");
  43.             String password = request.getParameter("Password");
  44.            
  45.             //carico la lista degli oggetti
  46.             ArrayList<TennisObjectSale> listaOggetti = TennisObjectSaleFactory.getInstance().getOggettiList();
  47.            
  48.             //request.getRequestDispatcher("cliente.jsp").forward(request, response); //per test veloci
  49.            
  50.             //carico la lista dei clienti con il quale controllare user e psw
  51.             ArrayList<Cliente> listaClienti = ClienteFactory.getInstance().getClientiList();
  52.             for (Cliente u : listaClienti){
  53.                 if (u.getUsr().equals(username) &&u.getPsw().equals(password)){ //trovato il cliente
  54.                     //session.setAttribute("session", session);
  55.                     session.setAttribute("loggedIn", true); //un generico utente si è loggato, potrebbe servirmi
  56.                     session.setAttribute("clienteLoggedIn", true);//un cliente si è loggato
  57.                     autenticazioneRiuscita=true;
  58.                     session.setAttribute("id", u.getCodiceFiscale()); //variabile di sessione
  59.                     session.setAttribute("cliente", u);//metto nell'attributo "cliente" della sessione i dati del cliente loggato
  60.                     request.setAttribute("cliente", u);//metto nell'attributo "cliente" i dati del cliente loggato
  61.                     session.setAttribute("oggetti", listaOggetti);//metto in "oggetti" la lista degli oggetti
  62.                     request.getRequestDispatcher("cliente.htm").forward(request, response);
  63.                     //rimando a cliente.htm che sarà letto dalla servlet
  64.                     //ho usato .htm perché .html mi dava problemi, il build falliva
  65.                 }              
  66.             }
  67.            
  68.             //carico la lista dei venditori con il quale controllare user e psw
  69.             //stesso funzionamento del caso del cliente
  70.             ArrayList<Venditore> listaVenditori = VenditoreFactory.getInstance().getVenditoriList();
  71.             for (Venditore u : listaVenditori){
  72.                 if (u.getUsr().equals(username) &&u.getPsw().equals(password)){
  73.                     //
  74.                     session.setAttribute("loggedIn", true);
  75.                     session.setAttribute("venditoreLoggedIn", true);
  76.                     autenticazioneRiuscita=true;
  77.                     session.setAttribute("id", u.getCodiceFiscale());
  78.                     request.setAttribute("venditore", u);
  79.                     session.setAttribute("venditore", u);
  80.                     request.getRequestDispatcher("venditore.jsp").forward(request, response);
  81.                 }              
  82.             }
  83.            
  84.         }
  85.        
  86.         //se sono qua è perché l'autenticazione è fallita
  87.        
  88.         if (autenticazioneRiuscita==false){
  89.             //setto a false tutte le variabili che tengono conto degli utenti loggati
  90.             session.setAttribute("clienteLoggedIn", false);                  
  91.             session.setAttribute("venditoreLoggedIn", false);
  92.             session.setAttribute("loggedIn", false);
  93.  
  94.             //rimando a login.jsp con la variabile che mi permette di stampare un messaggio di errore
  95.             request.getRequestDispatcher("login.jsp?autenticazioneFallita=true").forward(request, response);
  96.         }
  97.     }
  98.  
  99.     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  100.     /**
  101.      * Handles the HTTP <code>GET</code> method.
  102.      *
  103.      * @param request servlet request
  104.      * @param response servlet response
  105.      * @throws ServletException if a servlet-specific error occurs
  106.      * @throws IOException if an I/O error occurs
  107.      */
  108.     @Override
  109.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  110.             throws ServletException, IOException {
  111.         processRequest(request, response);
  112.     }
  113.  
  114.     /**
  115.      * Handles the HTTP <code>POST</code> method.
  116.      *
  117.      * @param request servlet request
  118.      * @param response servlet response
  119.      * @throws ServletException if a servlet-specific error occurs
  120.      * @throws IOException if an I/O error occurs
  121.      */
  122.     @Override
  123.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  124.             throws ServletException, IOException {
  125.         processRequest(request, response);
  126.     }
  127.  
  128.     /**
  129.      * Returns a short description of the servlet.
  130.      *
  131.      * @return a String containing servlet description
  132.      */
  133.     @Override
  134.     public String getServletInfo() {
  135.         return "Short description";
  136.     }// </editor-fold>
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement