jsp之内置对象session过期时间

通过设置内建对象session对象的过期时间,使用户提交的信息保存300秒,超时后让用户重新登录。保存到session中,并在有效期限内刷新页面,显示用户的提交信息,如果超过预设时间则弹出对话框,确认后返回提交信息页面

5_2_1.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 '5_2_1.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>  
  <body>
      <form action="5_2_3.jsp" method="post">
          用户名:<input type="text" name="name"><br/><br>
          密码    : <input type="password" name="password"><br><br>
          
          <input type="submit" name="submit" value="提交">
          <input type="reset" name="reset" value="取消">
      </form>
  
  </body>
</html>

5_2_3.jsp

<%
    String name=request.getParameter("name");
      String password = request.getParameter("password");
      session.setAttribute("username", name);
      session.setAttribute("userpassword", password);
       response.sendRedirect("5_2_2.jsp");
     %>

5_2_2.jsp

<%@ page import="java.text.SimpleDateFormat" 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 '5_2_2.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
    
  </head>
  <body>
  
<%@page import="java.util.Date"%>
     <%
         String username=(String)session.getAttribute("username");
         String userPassword=(String)session.getAttribute("userpassword");
         out.print("用户名:"+username+"<br>");
         out.print("密码:"+userPassword+"<br>");
         if(session.isNew()){
             //设置过期时间
             session.setMaxInactiveInterval(1);
            // session.setAttribute("expire", 10);             
             response.sendRedirect("5_2_1.jsp");
         }
         else{
         
              Date d = new Date();
		      SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		      String atime = df.format(session.getCreationTime());
		      String now = df.format(d);
		      out.print("session建立时间:"+atime+"<br>");
	          out.print("当前时间:"+now+"<br>");
              //获取session建立时间              
              long create_time = session.getCreationTime();
              long access_time = session.getLastAccessedTime();               
              long current_time = System.currentTimeMillis();
              //session存在时间            
              long exist_time = (current_time-create_time)/1000;
              out.print("session已经存在"+exist_time+"秒");
             response.setHeader("refresh", "1");
              if(exist_time>=300){
                  out.println("session已过期");
                  session.invalidate(); 
                  %>
                 <script>
                     alert("session已过期");
                 </script>
                  <% 
                  //response.sendRedirect("5_2_1.jsp");
              }         
         }
      %>
  </body>
</html>

实现效果:
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

相关推荐
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页