Session机制(是对cookie的作用的提升,使用较多)

本文详细介绍了Session的工作原理及其在Web应用中的使用方式。包括如何通过Session ID进行用户状态跟踪、Session对象的各种属性和方法,以及如何在不同页面间传递数据。

1.Session作用类似于购物车,第一次,放入物品,可以获得Session的id,并可以设置id失效的时间,这样便于多次将物品放在购物车里面,使用的就是获取的Session的id;

 

2.Session的常用方法:sessionid,获取首次访问的id值,及在jsp页面直接的跳转,这个是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>
    
    IsNew: <%= session.isNew() %>
    <br><br>
    
    MaxInactiveInterval: <%= session.getMaxInactiveInterval() %>
    <br><br>
    
    CreateTime: <%= session.getCreationTime() %>
    <br><br>

    LastAccessTime: <%= session.getLastAccessedTime() %>
    <br><br>
    
    <% 
        Object username = session.getAttribute("username");
        if(username == null){
            username = "";
        }
    %>
    
    <form action="<%= response.encodeURL("hello.jsp") %>" method="post">
        
        username: <input type="text" name="username" 
            value="<%= username %>"/>
        <input type="submit" value="Submit"/>
    
    </form>
</body>
</html>

 

3.hello.jsp页面,有注销和重新登陆的功能,点击注销跳转到logout.jsp页面,有实现注销session的方法,点击重新登陆返回到login页面

<%@ 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>
    
    IsNew: <%= session.isNew() %>
    <br><br>
    
    MaxInactiveInterval: <%= session.getMaxInactiveInterval() %>
    <br><br>
    
    CreateTime: <%= session.getCreationTime() %>
    <br><br>

    LastAccessTime: <%= session.getLastAccessedTime() %>
    <br><br>
    
    Hello: <%= request.getParameter("username") %>
    <br><br>
    
    <% 
        session.setAttribute("username", request.getParameter("username")); 
    %>
    
    <a href="<%= response.encodeURL("login.jsp") %>">重新登录</a>    
    &nbsp;&nbsp;&nbsp;&nbsp;
    <a href="<%= response.encodeURL("logout.jsp") %>">注销</a>    
</body>
</html>

 

4.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>
    
    IsNew: <%= session.isNew() %>
    <br><br>
    
    MaxInactiveInterval: <%= session.getMaxInactiveInterval() %>
    <br><br>
    
    CreateTime: <%= session.getCreationTime() %>
    <br><br>

    LastAccessTime: <%= session.getLastAccessedTime() %>
    <br><br>
    
    Bye: <%= session.getAttribute("username") %>
    <br><br>
    
    <a href="login.jsp">重新登录</a>    
    
    <% 
        session.invalidate();
    %>
</body>
</html>

 

转载于:https://www.cnblogs.com/lxnlxn/p/5823496.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值