zoukankan      html  css  js  c++  java
  • Session机制(是对cookie的作用的提升,使用较多)

    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>
  • 相关阅读:
    jquery中选择块并改变属性值的方法
    Bash Shell中Shift用法分享
    linux批量备份服务器配置文件和目录的脚本
    [Java开发之路](8)输入流和输出流
    cf 251 B Playing with Permutations 暴力 分类讨论
    CSS改变字体下划线颜色
    DICOM:C-GET服务
    Android平台录音音量计的实现
    【BZOJ】2186 沙拉公主的困惑
    【php】global的使用与php的全局变量
  • 原文地址:https://www.cnblogs.com/lxnlxn/p/5823496.html
Copyright © 2011-2022 走看看