Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LoginServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- private final String username = "admin";
- private final String password = "password";
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- //Obtener las variables de la vista
- String username = request.getParameter("username");
- String password = request.getParameter("pwd");
- if (this.username.equals(username) && this.password.equals(password)) {
- //Verificamos si existe una sesión anterior
- HttpSession oldSession = request.getSession(false);
- if (oldSession != null) {
- oldSession.invalidate();
- }
- //Creamos una nueva sesión
- HttpSession newSession = request.getSession(true);
- //Configuración de la sesión a expirar en 5 minutos
- newSession.setMaxInactiveInterval(5*60);
- //Creación del Cookie
- Cookie message = new Cookie("message", "Welcome");
- message.setHttpOnly(true);
- message.setSecure(true);
- response.addCookie(message);
- response.sendRedirect("admin/loginSuccess.jsp");
- } else {
- //Se direcciona a la pagina del login
- RequestDispatcher rd = getServletContext().getRequestDispatcher("/loginPage.jsp");
- PrintWriter out = response.getWriter();
- out.println("<font color=red>Either username or password is wrong.</font>");
- rd.include(request, response);
- }
- }
- }
- public class LogoutServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- //Invalidar la sesión si existe
- HttpSession session = request.getSession(false);
- if(session!=null) {
- session.invalidate();
- }
- response.sendRedirect(request.getContextPath()+"/loginPage.jsp");
- }
- }
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="ISO-8859-1"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="ISO-8859-1">
- <title>Login Success Page</title>
- </head>
- <body>
- <%
- String message = null;
- String sessionID = null;
- Cookie[] cookies = request.getCookies();
- if (cookies != null) {
- for (Cookie cookie : cookies) {
- if (cookie.getName().equals("message"))
- message = cookie.getValue()+ " " + cookie.getSecure();
- if (cookie.getName().equals("JSESSIONID"))
- sessionID = cookie.getValue();
- }
- }
- %>
- <h3>Login Success</h3>
- <h4><%=message%></h4>
- <h4>
- Session ID =
- <%=sessionID%></h4>
- <br>
- <br>
- <form action="LogoutServlet" method="post">
- <input type="submit" value="Logout">
- </form>
- </body>
- </html>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Login Page</title>
- <h1>Please login to continue</h1>
- </head>
- <body>
- <form action="LoginServlet" method="post">
- Username: <input type="text" name="username">
- <br>
- Password: <input type="password" name="pwd">
- <br><br>
- <input type="submit" value="Login">
- </form>
- </body>
- </html>
Add Comment
Please, Sign In to add comment