功能描述:login.jsp 输入name登录,进入main.jsp,在main.jsp可以重新登陆,也可以注销当前用户。
login.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
</head>
<body>
SessionId:<%=session.getId() %><br><br>
IsNew:<%=session.isNew() %><br><br>
MaxInactiveInterval:<%=session.getMaxInactiveInterval() %><br><br>
CreationTime:<%=session.getCreationTime() %><br><br>
LastAccessedTime:<%=session.getLastAccessedTime() %><br><br>
<form action="<%=basePath%>session/main.jsp">
username:<input type="text" name="username"
<% Object obj=session.getAttribute("username");
if(obj==null){
obj="";
}%>
value=<%=obj.toString()%>>
<input type="submit" value="提交">
</form>
</body>
</html>
main.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'main.jsp' starting page</title>
</head>
<body>
SessionId:<%=session.getId() %><br><br>
IsNew:<%=session.isNew() %><br><br>
MaxInactiveInterval:<%=session.getMaxInactiveInterval() %><br><br>
CreationTime:<%=session.getCreationTime() %><br><br>
LastAccessedTime:<%=session.getLastAccessedTime() %><br><br>
用户名:<%=request.getParameter("username") %><br>
<a href="<%=basePath%>session/login.jsp?username=<%session.setAttribute("username",request.getParameter("username"));%>">返回登录页</a><br>
<a href="<%=basePath%>session/logout.jsp?username=<%session.setAttribute("username",request.getParameter("username"));%>">注销</a><br>
</body>
</html>
logout.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
</head>
<body>
SessionId:<%=session.getId() %><br><br>
IsNew:<%=session.isNew() %><br><br>
MaxInactiveInterval:<%=session.getMaxInactiveInterval() %><br><br>
CreationTime:<%=session.getCreationTime() %><br><br>
LastAccessedTime:<%=session.getLastAccessedTime() %><br><br>
<form action="<%=basePath%>session/main.jsp">
bye:<input type="text" name="username"
<% Object obj=session.getAttribute("username");
if(obj==null){
obj="";
}%>
value=<%=obj.toString()%>>
<a href="<%=basePath%>session/login.jsp">返回登录页</a><br>
<%session.invalidate(); %>
</form>
</body>
</html>
测试:
进入login.jsp,并输入内容:
点击提交,进入main.jsp:
点击main.jsp中的"返回登录页"按钮:
点击main.jsp中的"注销"按钮:
点击logout.jsp中的“返回登录页”按钮: