JavaWeb-023-HttpSession接口中的方法

来源:互联网 发布:flux电磁场仿真软件 编辑:程序博客网 时间:2024/06/01 13:15
  • getId方法:获取会话ID
  • getCreationTime方法:返回创建Session的时间。
  • getLastAccessedTime方法:返回客户端上一次发送与此会话关联的请求的时间。
  • setMaxInactiveInterval方法:指定在 servlet 容器使此会话失效之前客户端请求之间的时间间隔,以秒为单位。负数时间指示会话永远不会超时。
  • getMaxInactiveInterval方法:返回 servlet 容器在客户端访问之间将使此会话保持打开状态的最大时间间隔,以秒为单位。
  • isNew方法:如果客户端还不知道该会话,或者客户端选择不加入该会话,则返回 true。
    • 如果客户端请求消息中返回了一个与Servlet程序当前获得的HttpSession对象的会话标识号相同的会话标识号,则认为这个HttpSession对象不是新建的。
  • invalidate方法:使此会话无效,然后取消对任何绑定到它的对象的绑定。
  • getServletContext方法:获取ServletContext对象。
  • setAttribute方法:设置属性值。
  • getAttribute方法:获取属性值。
  • removeAttribute方法:移除属性值。
  • getAttributeNames方法:返回包含绑定到此会话的所有对象的名称的 String 对象的 Enumeration。
    微信公众号:JavaWeb架构师

测试代码

index.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>        <fieldset>        <marquee>            会话ID : <%= session.getId() %>            <br />            是新会话? : <%= session.isNew() %>            <br />            最大内部闲置: <%= session.getMaxInactiveInterval() %>            <br />            创建时间 : <%= session.getCreationTime() %>            <br />            最后一次访问时间 : <%= session.getLastAccessedTime() %>        </marquee>        <%            //2.获取重新登录的username            Object username = session.getAttribute("username") == null ? "" : session.getAttribute("username");         %>        <form action="hello.jsp" method="post">             <table>                <tr>                    <td><label for="userID">用户名:</label></td>                    <td><input type="text" name="username" value="<%=  username%>"  id="userID" /></td>                </tr>                <tr>                    <td></td>                    <td><input type="submit" value="登录" /></td>                </tr>                           </table>        </form>        </fieldset>    </body></html>

hello.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>        <fieldset>        <marquee behavior="alternate">            会话ID : <%= session.getId() %>            <br />            是新会话? : <%= session.isNew() %>            <br />            最大内部闲置: <%= session.getMaxInactiveInterval() %>            <br />            创建时间 : <%= session.getCreationTime() %>            <br />            最后一次访问时间 : <%= session.getLastAccessedTime() %>        </marquee>        <details>            <summary>你好!</summary>            <%= request.getParameter("username") %>        </details>        <%            //1.设置会话的属性值,想在重新登录的页面能直接显示上一次的登录名            session.setAttribute("username", request.getParameter("username"));        %>        <a href="index.jsp"> 重新登录 </a>        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;        <a href="logout.jsp">注销</a>        </fieldset>    </body></html>

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>        <fieldset>        <marquee behavior="alternate">            会话ID : <%= session.getId() %>            <br />            是新会话? : <%= session.isNew() %>            <br />            最大内部闲置: <%= session.getMaxInactiveInterval() %>            <br />            创建时间 : <%= session.getCreationTime() %>            <br />            最后一次访问时间 : <%= session.getLastAccessedTime() %>        </marquee>        <details>            <summary>再见</summary>            <%= session.getAttribute("username") %>        </details>        <%            //销毁session            session.invalidate();        %>        </fieldset>    </body></html>

微信公众号:JavaWeb架构师


其它

  • 源码下载
关注下方公众号,回复:javaweb_course.code
  • 欢迎加入交流群:451826376

  • 更多信息:www.itcourse.top

完整教程PDF版本下载

原创粉丝点击