Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. >>>>Login.html
  2.  
  3. <!DOCTYPE html>
  4. <!--
  5. To change this license header, choose License Headers in Project Properties.
  6. To change this template file, choose Tools | Templates
  7. and open the template in the editor.
  8. -->
  9. <html>
  10.     <head>
  11.         <title>Login</title>
  12.         <meta charset="UTF-8">
  13.         <meta name="viewport" content="width=device-width, initial-scale=1.0">
  14.     </head>
  15.     <body>
  16.         <form action="Login" method="POST">
  17.             Username: <input type="text" name="username" value="" />
  18.             <br>
  19.             Password: <input type="password" name="password" value="" />
  20.             <br>
  21.             <br>
  22.             <input type="submit" value="Login" />
  23.         </form>
  24.     </body>
  25. </html>
  26.  
  27.  
  28. >>>>LoginServlet
  29.  
  30. /*
  31.  * To change this license header, choose License Headers in Project Properties.
  32.  * To change this template file, choose Tools | Templates
  33.  * and open the template in the editor.
  34.  */
  35.  
  36. import java.io.IOException;
  37. import java.io.PrintWriter;
  38. import javax.servlet.ServletException;
  39. import javax.servlet.annotation.WebServlet;
  40. import javax.servlet.http.HttpServlet;
  41. import javax.servlet.http.HttpServletRequest;
  42. import javax.servlet.http.HttpServletResponse;
  43.  
  44. /**
  45.  *
  46.  * @author Administrator
  47.  */
  48. @WebServlet(urlPatterns = {"/Login"})
  49. public class LoginServlet extends HttpServlet {
  50.  
  51.     /**
  52.      * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  53.      * methods.
  54.      *
  55.      * @param request servlet request
  56.      * @param response servlet response
  57.      * @throws ServletException if a servlet-specific error occurs
  58.      * @throws IOException if an I/O error occurs
  59.      */
  60.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  61.             throws ServletException, IOException {
  62.         response.setContentType("text/html;charset=UTF-8");
  63.         try (PrintWriter out = response.getWriter()) {
  64.             /* TODO output your page here. You may use following sample code. */
  65.             out.println("<!DOCTYPE html>");
  66.             out.println("<html>");
  67.             out.println("<head>");
  68.             out.println("<title>Servlet LoginServlet</title>");            
  69.             out.println("</head>");
  70.             out.println("<body>");
  71.             String username = request.getParameter("username");
  72.             String password = request.getParameter("password");
  73.             if (username.equals("jaab") && password.equals("1234"))
  74.                 out.print("Hello, " + username + ".");
  75.             else
  76.                 out.print("Invalid login!");
  77.             out.println("</body>");
  78.             out.println("</html>");
  79.         }
  80.     }
  81.  
  82.     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  83.     /**
  84.      * Handles the HTTP <code>GET</code> method.
  85.      *
  86.      * @param request servlet request
  87.      * @param response servlet response
  88.      * @throws ServletException if a servlet-specific error occurs
  89.      * @throws IOException if an I/O error occurs
  90.      */
  91.     @Override
  92.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  93.             throws ServletException, IOException {
  94.         processRequest(request, response);
  95.     }
  96.  
  97.     /**
  98.      * Handles the HTTP <code>POST</code> method.
  99.      *
  100.      * @param request servlet request
  101.      * @param response servlet response
  102.      * @throws ServletException if a servlet-specific error occurs
  103.      * @throws IOException if an I/O error occurs
  104.      */
  105.     @Override
  106.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  107.             throws ServletException, IOException {
  108.         processRequest(request, response);
  109.     }
  110.  
  111.     /**
  112.      * Returns a short description of the servlet.
  113.      *
  114.      * @return a String containing servlet description
  115.      */
  116.     @Override
  117.     public String getServletInfo() {
  118.         return "Short description";
  119.     }// </editor-fold>
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement