JavaWeb——HttpSession經常使用方法示例

HttpSession接口中方法html

getId()java

getCreationTime()session

getLastAccessedTime()jsp

setMaxInactiveInterval()post

getMaxInactiveInterval()this

isNew():若是客戶端請求消息中返回了一個與Servlet程序當前得到的HttpSession對象的會話標識號相同,則認爲這個HttpSession對象不是新建的spa

invalidate()code

getServletContext()orm

setAttribute()htm

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>
相關文章
相關標籤/搜索