|
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" |
|
pageEncoding="ISO-8859-1"%> |
|
<%! |
|
private Cookie getCookie(String name, Cookie[] cookies) |
|
{ |
|
for(Cookie cookie: cookies) |
|
{ |
|
if(cookie.getName().equals(name)) |
|
return cookie; |
|
} |
|
return null; |
|
} |
|
%> |
|
<!DOCTYPE html> |
|
<html lang="en"> |
|
<head> |
|
<meta charset="utf-8"> |
|
<meta http-equiv="X-UA-Compatible" content="IE=edge"> |
|
<meta name="viewport" content="width=device-width, initial-scale=1"> |
|
|
|
<title>Session and Cookies</title> |
|
|
|
<!-- Bootstrap --> |
|
<link href="css/bootstrap.min.css" rel="stylesheet"> |
|
|
|
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> |
|
<!-- WARNING: Respond.js doesn't work if you view the page via file:// --> |
|
<!--[if lt IE 9]> |
|
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> |
|
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> |
|
<![endif]--> |
|
</head> |
|
<body> |
|
<div class="container"> |
|
<h1>Session and cookies! </h1> |
|
<% |
|
Cookie c = getCookie("login", request.getCookies()); |
|
if(request.getMethod().equals("POST")) |
|
{ |
|
String username = request.getParameter("username"); |
|
String password = request.getParameter("password"); |
|
String radio = request.getParameter("cookiesession"); |
|
if(username != null && password != null && radio != null) |
|
{ |
|
if(username.equals("Federico") && password.equals("password")) |
|
{ |
|
if(radio.equals("session")) |
|
session.setAttribute("logged", username); |
|
else |
|
{ |
|
c = new Cookie("logged", username); |
|
response.addCookie(c); |
|
|
|
} |
|
} |
|
else |
|
{ |
|
%> |
|
<p class="bg-danger" style="padding:15px;">Username o password errate.</p> |
|
<% |
|
} |
|
} |
|
} |
|
if(request.getMethod().equals("GET")) |
|
{ |
|
if(request.getParameter("logout") != null && request.getParameter("logout").equals("y")) |
|
{ |
|
for(Cookie cookie : request.getCookies()) |
|
{ |
|
cookie.setMaxAge(0); |
|
response.addCookie(cookie); |
|
} |
|
c = null; |
|
session.invalidate(); |
|
} |
|
} |
|
|
|
|
|
if(request.getSession().getAttribute("logged") != null || c != null) |
|
{ |
|
String username = request.getSession().getAttribute("logged") != null ? (String) request.getSession().getAttribute("logged") : c.getValue(); |
|
|
|
out.println("<p class=\"bg-primary\"><h2>Welcome," + username + "</h2></p>"); |
|
out.println("<button type=\"submit\" class=\"btn btn-default\" onclick=\"javascript:window.location.assign('?logout=y\')\">Logout</button>"); |
|
|
|
} |
|
else |
|
{ |
|
%> |
|
<div class="row"> |
|
<div class="col-md-6"> |
|
<form method="post" action=""> |
|
<div class="form-group"> |
|
<label for="username">Username</label> |
|
<input name="username" type="text" class="form-control" id="username" placeholder="Enter username"> |
|
</div> |
|
<div class="form-group"> |
|
<label for="password">Password</label> |
|
<input name="password" type="password" class="form-control" id="password" placeholder="Enter Password"> |
|
</div> |
|
<div class="radio"> |
|
<label> |
|
<input type="radio" name="cookiesession" id="cookie" value="cookie" checked> |
|
Use cookies |
|
</label> |
|
</div> |
|
<div class="radio"> |
|
<label> |
|
<input type="radio" name="cookiesession" id="session" value="session"> |
|
Use sessions |
|
</label> |
|
</div> |
|
<button type="submit" class="btn btn-default">Login</button> |
|
</form> |
|
</div> |
|
</div> |
|
<% |
|
} |
|
%> |
|
</div> |
|
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> |
|
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> |
|
<!-- Include all compiled plugins (below), or include individual files as needed --> |
|
<script src="js/bootstrap.min.js"></script> |
|
</body> |
|
</html> |