Java Web—session

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 2 <html>
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 5 <title>Insert title here</title>
 6 </head>
 7 <body>
 8 主页面
 9 <%
10 //检查session信息,得到session
11 Object ob=session.getAttribute("username");
12 if(ob!=null)
13 {
14     out.print("欢迎登录"+ob.toString());
15     }
16 else
17 {
18     //5秒后跳转到session2的页面
19     response.setHeader("refresh", "3;URL=session3.jsp");
20     out.print("会话超时,重新登陆");    
21 }
22 %>
23 <br>
24 <a href=session3.jsp>退出登录</a>
25 </body>
26 </html>
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <% 
11 //登录验证信息
12 String un=request.getParameter("username");
13 String pw=request.getParameter("password");
//如果登录正确,则跳转到主页面:session1
14 if(un!=null&&pw!=null) 15 { 16 if(un.equals("lhy")&&pw.equals("12345")) 17 { 18 session.setAttribute("username",un); 19 response.sendRedirect("session1.jsp"); 20 } 21 else 22 { 23 out.print("输出有误"); 24 } 25 }
//如果失败则显示登陆错误
26 else 27 { 28 out.print("登陆错误"); 29 } 30 //如果登录正确,跳转到主页面:session1 31 32 //如果失败,显示登录错误 33 %> 34 </body> 35 </html>
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <%
11 //登录页面
//注销
12 session.invalidate(); 13 %> 14 <form action="session2.jsp" method="post"> 15 用户名<input type="text" name="username"> 16 <br> 17 密码<input type="password" name="password"> 18 <br> 19 <input type="submit" value="登录"> 20 </form> 21 </body> 22 </html>
原文地址:https://www.cnblogs.com/yg6405816/p/5625775.html