|
| 1 | +package org.javaee7.servlet.programmatic.login; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.PrintWriter; |
| 5 | +import javax.servlet.ServletException; |
| 6 | +import javax.servlet.annotation.WebServlet; |
| 7 | +import javax.servlet.http.HttpServlet; |
| 8 | +import javax.servlet.http.HttpServletRequest; |
| 9 | +import javax.servlet.http.HttpServletResponse; |
| 10 | + |
| 11 | +/** |
| 12 | + * @author Arun Gupta |
| 13 | + */ |
| 14 | +@WebServlet(urlPatterns = {"/LoginServlet"}) |
| 15 | +public class LoginServlet extends HttpServlet { |
| 16 | + |
| 17 | + /** |
| 18 | + * Processes requests for both HTTP <code>GET</code> and <code>POST</code> |
| 19 | + * methods. |
| 20 | + * |
| 21 | + * @param request servlet request |
| 22 | + * @param response servlet response |
| 23 | + * @throws ServletException if a servlet-specific error occurs |
| 24 | + * @throws IOException if an I/O error occurs |
| 25 | + */ |
| 26 | + protected void processRequest(HttpServletRequest request, HttpServletResponse response) |
| 27 | + throws ServletException, IOException { |
| 28 | + response.setContentType("text/html;charset=UTF-8"); |
| 29 | + PrintWriter out = response.getWriter(); |
| 30 | + String user = request.getParameter("user"); |
| 31 | + String password = request.getParameter("password"); |
| 32 | + |
| 33 | + if (user != null && password != null) { |
| 34 | + request.login(user, password); |
| 35 | + } |
| 36 | + |
| 37 | + userDetails(out, request); |
| 38 | + } |
| 39 | + |
| 40 | + private void userDetails(PrintWriter out, HttpServletRequest request) { |
| 41 | + out.println("isUserInRole?" + request.isUserInRole("g1")); |
| 42 | + out.println("getRemoteUser?" + request.getRemoteUser()); |
| 43 | + out.println("getUserPrincipal?" + request.getUserPrincipal()); |
| 44 | + out.println("getAuthType?" + request.getAuthType()); |
| 45 | + } |
| 46 | + |
| 47 | + // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> |
| 48 | + /** |
| 49 | + * Handles the HTTP <code>GET</code> method. |
| 50 | + * |
| 51 | + * @param request servlet request |
| 52 | + * @param response servlet response |
| 53 | + * @throws ServletException if a servlet-specific error occurs |
| 54 | + * @throws IOException if an I/O error occurs |
| 55 | + */ |
| 56 | + @Override |
| 57 | + protected void doGet(HttpServletRequest request, HttpServletResponse response) |
| 58 | + throws ServletException, IOException { |
| 59 | + processRequest(request, response); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Handles the HTTP <code>POST</code> method. |
| 64 | + * |
| 65 | + * @param request servlet request |
| 66 | + * @param response servlet response |
| 67 | + * @throws ServletException if a servlet-specific error occurs |
| 68 | + * @throws IOException if an I/O error occurs |
| 69 | + */ |
| 70 | + @Override |
| 71 | + protected void doPost(HttpServletRequest request, HttpServletResponse response) |
| 72 | + throws ServletException, IOException { |
| 73 | + processRequest(request, response); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Returns a short description of the servlet. |
| 78 | + * |
| 79 | + * @return a String containing servlet description |
| 80 | + */ |
| 81 | + @Override |
| 82 | + public String getServletInfo() { |
| 83 | + return "Short description"; |
| 84 | + }// </editor-fold> |
| 85 | + |
| 86 | +} |
0 commit comments