利用HttpSession 实现登录的重新登陆跟注销:
Login.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
sessionID:<%= session.getId() %>
<br><br>
session是否为新的:<%= session.isNew() %>
<br><br>
session有效时间:<%= session.getMaxInactiveInterval() %>
<br><br>
session创建时间:<%= session.getCreationTime()%>
<br><br>
session上一次的访问时间:<%= session.getLastAccessedTime() %>
<br><br>
<%
Object val = session.getAttribute("wzx");
if(val == null)
{
val = "";
}
%>
<!-- URL重写, 可在浏览器完全阻止cookie 的情况下,还能使用session -->
<form action="<%= response.encodeRedirectUrl("Hello.jsp") %>">
账号:<input type="text" name="wzx" value="<%= val%>">
<input type="submit" value="submit">
</form>
</body>
</html>
Hello.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
sessionID:<%= session.getId() %>
<br><br>
session是否为新的:<%= session.isNew() %>
<br><br>
session有效时间:<%= session.getMaxInactiveInterval() %>
<br><br>
session创建时间:<%= session.getCreationTime()%>
<br><br>
session上一次的访问时间:<%= session.getLastAccessedTime() %>
<br><br>
<%
session.setAttribute("wzx", request.getParameter("wzx"));
%>
Hello:<%= request.getParameter("wzx") %>
<br><br>
<a href="<%= response.encodeRedirectUrl("Login.jsp") %>">重新登陆</a>
<a href="<%= response.encodeRedirectUrl("Logout.jsp") %>">注销</a>
</body>
</html>
Logout.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
sessionID:<%= session.getId() %>
<br><br>
session是否为新的:<%= session.isNew() %>
<br><br>
session有效时间:<%= session.getMaxInactiveInterval() %>
<br><br>
session创建时间:<%= session.getCreationTime()%>
<br><br>
session上一次的访问时间:<%= session.getLastAccessedTime() %>
<br><br>
Bey:<%= session.getAttribute("wzx") %>
<br><br>
<%
session.invalidate();
%>
<a href=<%= response.encodeRedirectUrl("Login.jsp") %>">重新登陆</a>
</body>
</html>