Advertisement
Guest User

Untitled

a guest
Apr 25th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.76 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("loggedIn", true); //un generico utente si è loggato, potrebbe servirmi
  55.                     session.setAttribute("clienteLoggedIn", true);//un cliente si è loggato
  56.                     autenticazioneRiuscita=true;
  57.                     session.setAttribute("id", u.getCodiceFiscale()); //variabile di sessione
  58.                     request.setAttribute("cliente", u);//metto nell'attributo "cliente" i dati del cliente loggato
  59.                     request.setAttribute("oggetti", listaOggetti);//metto in "oggetti" la lista degli oggetti
  60.                     request.getRequestDispatcher("cliente.htm").forward(request, response);
  61.                     //rimando a cliente.htm che sarà letto dalla servlet
  62.                     //ho usato .htm perché .html mi dava problemi, il build falliva
  63.                 }              
  64.             }
  65.            
  66.             //carico la lista dei venditori con il quale controllare user e psw
  67.             //stesso funzionamento del caso del cliente
  68.             ArrayList<Venditore> listaVenditori = VenditoreFactory.getInstance().getVenditoriList();
  69.             for (Venditore u : listaVenditori){
  70.                 if (u.getUsr().equals(username) &&u.getPsw().equals(password)){
  71.                     session.setAttribute("loggedIn", true);
  72.                     session.setAttribute("venditoreLoggedIn", true);
  73.                     autenticazioneRiuscita=true;
  74.                     session.setAttribute("id", u.getCodiceFiscale());
  75.                     request.setAttribute("venditore", u);
  76.                     request.getRequestDispatcher("venditore.jsp").forward(request, response);
  77.                 }              
  78.             }
  79.            
  80.         }
  81.        
  82.         //se sono qua è perché l'autenticazione è fallita
  83.        
  84.         if (autenticazioneRiuscita==false){
  85.             //setto a false tutte le variabili che tengono conto degli utenti loggati
  86.             session.setAttribute("clienteLoggedIn", false);                  
  87.             session.setAttribute("venditoreLoggedIn", false);
  88.             session.setAttribute("loggedIn", false);
  89.  
  90.             //rimando a login.jsp con la variabile che mi permette di stampare un messaggio di errore
  91.             request.getRequestDispatcher("login.jsp?autenticazioneFallita=true").forward(request, response);
  92.         }
  93.     }
  94.  
  95.     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  96.     /**
  97.      * Handles the HTTP <code>GET</code> method.
  98.      *
  99.      * @param request servlet request
  100.      * @param response servlet response
  101.      * @throws ServletException if a servlet-specific error occurs
  102.      * @throws IOException if an I/O error occurs
  103.      */
  104.     @Override
  105.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  106.             throws ServletException, IOException {
  107.         processRequest(request, response);
  108.     }
  109.  
  110.     /**
  111.      * Handles the HTTP <code>POST</code> method.
  112.      *
  113.      * @param request servlet request
  114.      * @param response servlet response
  115.      * @throws ServletException if a servlet-specific error occurs
  116.      * @throws IOException if an I/O error occurs
  117.      */
  118.     @Override
  119.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  120.             throws ServletException, IOException {
  121.         processRequest(request, response);
  122.     }
  123.  
  124.     /**
  125.      * Returns a short description of the servlet.
  126.      *
  127.      * @return a String containing servlet description
  128.      */
  129.     @Override
  130.     public String getServletInfo() {
  131.         return "Short description";
  132.     }// </editor-fold>
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement