Advertisement
gbc921

ServletLogin_Marcell

Sep 26th, 2012
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.60 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package Servlet;
  6.  
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9.  
  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 almeidamarcell
  20.  */
  21. @WebServlet(name = "ServletLogin", urlPatterns = {"/ServletLogin"})
  22. public class ServletLogin extends HttpServlet {
  23.    
  24.    
  25.     /**
  26.      * Processes requests for both HTTP
  27.      * <code>GET</code> and
  28.      * <code>POST</code> methods.
  29.      *
  30.      * @param request servlet request
  31.      * @param response servlet response
  32.      * @throws ServletException if a servlet-specific error occurs
  33.      * @throws IOException if an I/O error occurs
  34.      */
  35.  
  36.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  37.             throws ServletException, IOException {
  38.         response.setContentType("text/html;charset=UTF-8");
  39.         PrintWriter out = response.getWriter();
  40.        
  41.            String account = request.getParameter("account");
  42.     String password = request.getParameter("password");
  43.     String pin = request.getParameter("pin");
  44.        
  45.     // MUDEI AQUI PRA ELE ENTRAR NO IF!!!
  46.         if (allowUser(account, password, pin)) {
  47.       out.println("<HTML><HEAD><TITLE>Access Denied</TITLE></HEAD>");
  48.       out.println("<BODY>Your login and password are invalid.<BR>");
  49.       out.println("You may want to <A HREF=\"./ServletLogin\">try again</A>");
  50.       out.println("</BODY></HTML>");
  51.     } else {
  52.       // login
  53.       HttpSession session = request.getSession();
  54.       session.setAttribute("logon.isDone", account);
  55.       //
  56.        
  57.        
  58.         try {
  59.         String target = (String) session.getAttribute("login.target");
  60.         if (target != null) {
  61.           response.sendRedirect(target);
  62.           return;
  63.         }
  64.       } catch (Exception ignored) {
  65.       }
  66.  
  67.      
  68.       response.sendRedirect("/ServletLogin");
  69.     }
  70.   }
  71.  
  72.   protected boolean allowUser(String account, String password, String pin) {
  73.     return true; // trust everyone
  74.   }
  75.  
  76.   // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  77.     /**
  78.      * Handles the HTTP
  79.      * <code>GET</code> method.
  80.      *
  81.      * @param request servlet request
  82.      * @param response servlet response
  83.      * @throws ServletException if a servlet-specific error occurs
  84.      * @throws IOException if an I/O error occurs
  85.      */
  86.     @Override
  87.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  88.             throws ServletException, IOException {
  89.         processRequest(request, response);
  90.     }
  91.  
  92.     /**
  93.      * Handles the HTTP
  94.      * <code>POST</code> method.
  95.      *
  96.      * @param request servlet request
  97.      * @param response servlet response
  98.      * @throws ServletException if a servlet-specific error occurs
  99.      * @throws IOException if an I/O error occurs
  100.      */
  101.     @Override
  102.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  103.             throws ServletException, IOException {
  104.         processRequest(request, response);
  105.     }
  106.  
  107.     /**
  108.      * Returns a short description of the servlet.
  109.      *
  110.      * @return a String containing servlet description
  111.      */
  112.     @Override
  113.     public String getServletInfo() {
  114.         return "Short description";
  115.     }// </editor-fold>
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement