Advertisement
Guest User

questa è la servlet

a guest
May 3rd, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.88 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.  
  7. import M3_package.*;
  8. import java.io.IOException;
  9. import java.io.PrintWriter;
  10. import java.util.ArrayList;
  11. import javax.servlet.ServletException;
  12. import javax.servlet.annotation.WebServlet;
  13. import javax.servlet.http.HttpServlet;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16. import javax.servlet.http.HttpSession;
  17.  
  18. /**
  19.  *
  20.  * @author Simone Balloccu
  21.  */
  22. @WebServlet(name = "login_servlet", urlPatterns = {"/login_servlet"})
  23. public class login_servlet extends HttpServlet {
  24.  
  25.     /**
  26.      * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  27.      * methods.
  28.      *
  29.      * @param request servlet request
  30.      * @param response servlet response
  31.      * @throws ServletException if a servlet-specific error occurs
  32.      * @throws IOException if an I/O error occurs
  33.      */
  34.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  35.             throws ServletException, IOException {
  36.         response.setContentType("text/html;charset=UTF-8");
  37.        
  38.         HttpSession session = request.getSession(true);
  39.        
  40.         if(request.getParameter("button") != null)
  41.         {
  42.             String user = request.getParameter("user");
  43.             String password = request.getParameter("password");
  44.            
  45.             ArrayList<Utente> lista_Utenti = M3_Factory.getInstance().getListautenti();
  46.             for(Utente u : lista_Utenti)
  47.             {
  48.                 if(u.getUsername().equals(user) &&
  49.                    u.getPassword().equals(password))
  50.                 {
  51.                     session.setAttribute("loggato", true);
  52.                    
  53.                     if(u instanceof Venditore)
  54.                     {
  55.                         request.setAttribute("Venditore", u);
  56.                         request.setAttribute("cliente", M3_Factory.getInstance().getListaClienti());
  57.                         request.getRequestDispatcher("venditore.jsp").forward(request, response);
  58.                     }
  59.                     else
  60.                     {
  61.                         request.setAttribute("Cliente", u);
  62.                         request.getRequestDispatcher("cliente.jsp").forward(request, response);  
  63.                     }                    
  64.                 }
  65.             }
  66.         }
  67.         request.getRequestDispatcher("login.jsp").forward(request, response);
  68.  
  69.     }
  70.  
  71.     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  72.     /**
  73.      * Handles the HTTP <code>GET</code> method.
  74.      *
  75.      * @param request servlet request
  76.      * @param response servlet response
  77.      * @throws ServletException if a servlet-specific error occurs
  78.      * @throws IOException if an I/O error occurs
  79.      */
  80.     @Override
  81.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  82.             throws ServletException, IOException {
  83.         processRequest(request, response);
  84.     }
  85.  
  86.     /**
  87.      * Handles the HTTP <code>POST</code> method.
  88.      *
  89.      * @param request servlet request
  90.      * @param response servlet response
  91.      * @throws ServletException if a servlet-specific error occurs
  92.      * @throws IOException if an I/O error occurs
  93.      */
  94.     @Override
  95.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  96.             throws ServletException, IOException {
  97.         processRequest(request, response);
  98.     }
  99.  
  100.     /**
  101.      * Returns a short description of the servlet.
  102.      *
  103.      * @return a String containing servlet description
  104.      */
  105.     @Override
  106.     public String getServletInfo() {
  107.         return "Short description";
  108.     }// </editor-fold>
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement