Advertisement
Guest User

Untitled

a guest
May 2nd, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.41 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.html"})
  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); //recupero la sessione corrente
  38.        
  39.         //controllo che un cliente o un venditore siano loggati, in tal caso li rispedisco alla loro rispettiva pagina.
  40.         if (session.getAttribute("clienteLoggedIn")!=null && (boolean)session.getAttribute("clienteLoggedIn")==true) //controllo il cliente
  41.             response.sendRedirect("cliente.html");//redirezione
  42.        
  43.         else if (session.getAttribute("venditoreLoggedIn")!=null && (boolean)session.getAttribute("venditoreLoggedIn")==true)//controllo il venditore
  44.             response.sendRedirect("venditore.html");//redirezione
  45.        
  46.        else{ //nessun accesso è statoe effettuato
  47.             Boolean autenticazioneRiuscita = false; //flag che uso per inviare un parametro con il quale mostrerò un messaggio d'errore
  48.  
  49.             if(request.getParameter("Submit") != null){
  50.                 // Preleva i dati inviati
  51.                 String username = request.getParameter("Username");
  52.                 String password = request.getParameter("Password");
  53.  
  54.                 //carico la lista degli oggetti
  55.                 ArrayList<TennisObjectSale> listaOggetti = TennisObjectSaleFactory.getInstance().getOggettiList();
  56.  
  57.                 //carico la lista dei clienti con il quale controllare user e psw
  58.                 ArrayList<Cliente> listaClienti = ClienteFactory.getInstance().getClientiList();
  59.                 for (Cliente u : listaClienti){
  60.                     if (u.getUsr().equals(username) &&u.getPsw().equals(password)){ //trovato il cliente
  61.                         session.setAttribute("loggedIn", true); //un generico utente si è loggato, potrebbe servirmi
  62.                         session.setAttribute("clienteLoggedIn", true);//un cliente si è loggato
  63.                         autenticazioneRiuscita=true;
  64.                         session.setAttribute("id", u.getCodiceFiscale()); //variabile di sessione
  65.                         session.setAttribute("cliente", u);//metto nell'attributo "cliente" della sessione i dati del cliente loggato
  66.                         request.setAttribute("cliente", u);//metto nell'attributo "cliente" i dati del cliente loggato
  67.                         session.setAttribute("oggetti", listaOggetti);//metto in "oggetti" la lista degli oggetti
  68.                         //request.getRequestDispatcher("cliente.jsp").forward(request, response);
  69.                         response.sendRedirect("cliente.html");
  70.                         //rimando a cliente.html che sarà letto dalla servlet
  71.                     }              
  72.                 }
  73.  
  74.                 //carico la lista dei venditori con il quale controllare user e psw
  75.                 //stesso funzionamento del caso del cliente
  76.                 ArrayList<Venditore> listaVenditori = VenditoreFactory.getInstance().getVenditoriList();
  77.                 for (Venditore u : listaVenditori){
  78.                     if (u.getUsr().equals(username) && u.getPsw().equals(password)){
  79.                         session.setAttribute("loggedIn", true);
  80.                         session.setAttribute("venditoreLoggedIn", true);
  81.                         autenticazioneRiuscita=true;
  82.                         session.setAttribute("id", u.getCodiceFiscale());
  83.                         request.setAttribute("venditore", u);
  84.                         session.setAttribute("venditore", u);
  85.                         //request.getRequestDispatcher("venditore.html").forward(request, response);
  86.                         response.sendRedirect("venditore.html");
  87.                     }              
  88.                 }
  89.  
  90.                 //se sono qua è perché l'autenticazione è fallita e non c'è stata alcuna redirezione verso le pagine del cliente/venditore
  91.                 if (autenticazioneRiuscita==false){
  92.                     //setto a false tutte le variabili che tengono conto degli utenti loggati
  93.                     /*
  94.                         essendo settate a false, vuol dire che ci hanno provato ma non ci sono riusciti => autenticazione fallita
  95.                         se invece neanche ci provano, il parametro non viene creato e la sua ricerca mi darà null,
  96.                         distimguendo i casi di autenticazione fallita e non effettuata
  97.                     */
  98.                     session.setAttribute("clienteLoggedIn", false);                  
  99.                     session.setAttribute("venditoreLoggedIn", false);
  100.                     session.setAttribute("loggedIn", false);
  101.  
  102.                     //rimando a login.jsp con la variabile che mi permette di stampare un messaggio di errore
  103.                     request.getRequestDispatcher("login.jsp?autenticazioneFallita=true").forward(request, response);
  104.                 }
  105.             }
  106.  
  107.             else //nel caso non sia stato inviato nessun form, rimando alla stessa pagina.
  108.                 request.getRequestDispatcher("login.jsp").forward(request, response);
  109.         }
  110.                    
  111.     }
  112.  
  113.     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  114.     /**
  115.      * Handles the HTTP <code>GET</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 doGet(HttpServletRequest request, HttpServletResponse response)
  124.             throws ServletException, IOException {
  125.         processRequest(request, response);
  126.     }
  127.  
  128.     /**
  129.      * Handles the HTTP <code>POST</code> method.
  130.      *
  131.      * @param request servlet request
  132.      * @param response servlet response
  133.      * @throws ServletException if a servlet-specific error occurs
  134.      * @throws IOException if an I/O error occurs
  135.      */
  136.     @Override
  137.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  138.             throws ServletException, IOException {
  139.         processRequest(request, response);
  140.     }
  141.  
  142.     /**
  143.      * Returns a short description of the servlet.
  144.      *
  145.      * @return a String containing servlet description
  146.      */
  147.     @Override
  148.     public String getServletInfo() {
  149.         return "Short description";
  150.     }// </editor-fold>
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement