zoukankan      html  css  js  c++  java
  • JavaWeb——HttpSession常用方法示例

    HttpSession接口中方法

    getId()

    getCreationTime()

    getLastAccessedTime()

    setMaxInactiveInterval()

    getMaxInactiveInterval()

    isNew():如果客户端请求消息中返回了一个与Servlet程序当前获得的HttpSession对象的会话标识号相同,则认为这个HttpSession对象不是新建的

    invalidate()

    getServletContext()

    setAttribute()

    getAttribute()

    removeAttribute()

    getAttributeNames()

     

    login.jsp

    <%--
      Created by IntelliJ IDEA.
      User: dell
      Date: 2019/7/10
      Time: 15:52
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
        SessionID:<%= session.getId()%>
    <br><br>
        IsNew:<%= session.isNew()%>
    <br><br>
        MaxInactive:<%=session.getMaxInactiveInterval()%>
    <br><br>
        CreateTime:<%= session.getCreationTime()%>
    <br><br>
        LastAssessTime:<%= session.getLastAccessedTime()%>
    <br><br>
    <%
        Object username = session.getAttribute("username");
        if (username ==null){
            username ="";
        }
    %>
    <body>
    <form action="hello.jsp" method="post">
        username: <input type="text" name="username" value="<%=username%>">
        <input type="submit" value="Submit">
    </form>
    </body>
    </html>
    

      

    hello.jsp

    <%--
      Created by IntelliJ IDEA.
      User: dell
      Date: 2019/7/10
      Time: 15:56
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    SessionID:<%= session.getId()%>
    <br><br>
    IsNew:<%= session.isNew()%>
    <br><br>
    MaxInactive:<%=session.getMaxInactiveInterval()%>
    <br><br>
    CreateTime:<%= session.getCreationTime()%>
    <br><br>
    LastAssessTime:<%= session.getLastAccessedTime()%>
    <br><br>
    Hello:<%= request.getParameter("username")%>
    <br><br>
    <%
        session.setAttribute("username",request.getParameter("username"));
    %>
    <a href="login.jsp">重新登录</a>
    <br><br><br>
    <a href="logout.jsp">注销</a>
    
    </body>
    </html>
    

      

    logout.jsp

    <%--
      Created by IntelliJ IDEA.
      User: dell
      Date: 2019/7/10
      Time: 16:07
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    SessionID:<%= session.getId()%>
    <br><br>
    IsNew:<%= session.isNew()%>
    <br><br>
    MaxInactive:<%=session.getMaxInactiveInterval()%>
    <br><br>
    CreateTime:<%= session.getCreationTime()%>
    <br><br>
    LastAssessTime:<%= session.getLastAccessedTime()%>
    <br><br>
    BYE:<%= session.getAttribute("username")%>
    <br><br>
    <a href="login.jsp">重新登录</a>
    <%
        session.invalidate();
    %>
    </body>
    </html>
  • 相关阅读:
    Python基础-字符串方法 、文件操作
    Python基础-列表、字典
    Python基础作业-用户登录
    LeetCode 78. Subsets
    LeetCode 77. Combinations
    LeetCode 76. Minimum Window Substring
    LeetCode 74. Search a 2D Matrix
    LeetCode 73. Set Matrix Zeroes
    LightOJ 1043
    LightOJ 1042
  • 原文地址:https://www.cnblogs.com/yangHS/p/11164726.html
Copyright © 2011-2022 走看看