Guest User

Untitled

a guest
Jan 22nd, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. public class LoginServlet extends HttpServlet {
  2. private static final long serialVersionUID = 1L;
  3.  
  4. private final String username = "admin";
  5. private final String password = "password";
  6.  
  7. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  8.  
  9. //Obtener las variables de la vista
  10. String username = request.getParameter("username");
  11. String password = request.getParameter("pwd");
  12.  
  13.  
  14. if (this.username.equals(username) && this.password.equals(password)) {
  15. //Verificamos si existe una sesión anterior
  16. HttpSession oldSession = request.getSession(false);
  17. if (oldSession != null) {
  18. oldSession.invalidate();
  19. }
  20. //Creamos una nueva sesión
  21. HttpSession newSession = request.getSession(true);
  22.  
  23. //Configuración de la sesión a expirar en 5 minutos
  24. newSession.setMaxInactiveInterval(5*60);
  25.  
  26. //Creación del Cookie
  27. Cookie message = new Cookie("message", "Welcome");
  28. message.setHttpOnly(true);
  29. message.setSecure(true);
  30. response.addCookie(message);
  31. response.sendRedirect("admin/loginSuccess.jsp");
  32. } else {
  33. //Se direcciona a la pagina del login
  34. RequestDispatcher rd = getServletContext().getRequestDispatcher("/loginPage.jsp");
  35. PrintWriter out = response.getWriter();
  36. out.println("<font color=red>Either username or password is wrong.</font>");
  37. rd.include(request, response);
  38. }
  39. }
  40.  
  41. }
  42.  
  43. public class LogoutServlet extends HttpServlet {
  44. private static final long serialVersionUID = 1L;
  45.  
  46. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  47.  
  48. //Invalidar la sesión si existe
  49. HttpSession session = request.getSession(false);
  50.  
  51. if(session!=null) {
  52. session.invalidate();
  53. }
  54. response.sendRedirect(request.getContextPath()+"/loginPage.jsp");
  55. }
  56.  
  57. }
  58.  
  59. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  60. pageEncoding="ISO-8859-1"%>
  61. <!DOCTYPE html>
  62. <html>
  63. <head>
  64. <meta charset="ISO-8859-1">
  65. <title>Login Success Page</title>
  66. </head>
  67. <body>
  68. <%
  69. String message = null;
  70. String sessionID = null;
  71.  
  72. Cookie[] cookies = request.getCookies();
  73. if (cookies != null) {
  74. for (Cookie cookie : cookies) {
  75. if (cookie.getName().equals("message"))
  76. message = cookie.getValue()+ " " + cookie.getSecure();
  77. if (cookie.getName().equals("JSESSIONID"))
  78. sessionID = cookie.getValue();
  79. }
  80. }
  81. %>
  82.  
  83. <h3>Login Success</h3>
  84. <h4><%=message%></h4>
  85. <h4>
  86. Session ID =
  87. <%=sessionID%></h4>
  88. <br>
  89. <br>
  90. <form action="LogoutServlet" method="post">
  91. <input type="submit" value="Logout">
  92. </form>
  93. </body>
  94. </html>
  95.  
  96. <!DOCTYPE html>
  97. <html>
  98. <head>
  99. <meta charset="UTF-8">
  100. <title>Login Page</title>
  101. <h1>Please login to continue</h1>
  102. </head>
  103. <body>
  104. <form action="LoginServlet" method="post">
  105.  
  106. Username: <input type="text" name="username">
  107. <br>
  108. Password: <input type="password" name="pwd">
  109. <br><br>
  110. <input type="submit" value="Login">
  111. </form>
  112. </body>
  113. </html>
Add Comment
Please, Sign In to add comment