Java Tutorial/JSTL/Session
Содержание
Get HTTP Session by using JSTL
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<html>
<head>
<title>Page Data Example</title>
</head>
<body>
<h3> </h3>
<table border="1" width="539">
<tr>
<td colspan="2" width="529" bgcolor="#0000FF">
<b>
<font color="#FFFFFF" size="4">HTTP
Session(pageContext.session.)</font>
</b>
</td>
</tr>
<tr>
<td width="210">Creation Time</td>
<td width="313"> 
<c:out value="${pageContext.session.creationTime}" />
</td>
</tr>
<tr>
<td width="210">Session ID</td>
<td width="313"> 
<c:out value="${pageContext.session.id}" />
</td>
</tr>
<tr>
<td width="210">Last Accessed Time</td>
<td width="313"> 
<c:out value="${pageContext.session.lastAccessedTime}" />
</td>
</tr>
<tr>
<td width="210">Max Inactive Interval</td>
<td width="313"> 
<c:out
value="${pageContext.session.maxInactiveInterval}" />
seconds</td>
</tr>
<tr>
<td width="210">You have been on-line for</td>
<td width="313"> 
<c:out
value="${(pageContext.session.lastAccessedTime-pageContext.session.creationTime)/1000}" />
seconds</td>
</tr>
</table>
</body>
</html>
Remove Attribute From Session
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<c:set var="userName" value="Mark" scope="session" />
<html>
<head>
<title>Chapter 5 - Set a scoped attribute</title>
</head>
<body>
This page sets a session-scoped attribute that is removed
by
== Set Variable Scope to Session ==
<!-- start source code -->
<source lang="java">
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<html>
<head>
<title>Set in Scope Examples</title>
</head>
<body>
<c:set var="test" value="Session Level Value" scope="session" />
<table border="1">
<tr>
<td>
<b>Default Level</b>
</td>
<td>
<c:out value="${test}" />
</td>
</tr>
<tr>
<td>
<b>Page Level</b>
</td>
<td>
<c:out value="${pageScope.test}" />
</td>
</tr>
<tr>
<td>
<b>Request Level</b>
</td>
<td>
<c:out value="${requestScope.test}" />
</td>
</tr>
<tr>
<td>
<b>Session Level</b>
</td>
<td>
<c:out value="${sessionScope.test}" />
</td>
</tr>
<tr>
<td>
<b>Application Level</b>
</td>
<td>
<c:out value="${applicationScope.test}" />
</td>
</tr>
</table>
</body>
</html>
Update Session Date using JSTL
<%@page contentType="text/html"%>
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.ru/jstl/fmt" prefix="fmt" %>
<html>
<head><title>View Session JSP </title></head>
<body>
<h2>Session Info From A JSP</h2>
The session id: <c:out value="${pageContext.session.id}"/>
<h3>Session date values formatted as Dates</h3>
<jsp:useBean id="timeValues" class="java.util.Date"/>
<c:set target="${timeValues}" value="${pageContext.session.creationTime}" property="time"/>
The creation time: <fmt:formatDate value="${timeValues}" type="both" dateStyle="medium" /><br><br>
<c:set target="${timeValues}" value="${pageContext.session.lastAccessedTime}" property="time"/>
The last accessed time: <fmt:formatDate value="${timeValues}" type="both" dateStyle="short" />
<c:out value="${timeValues}"/>
</body>
</html>
Use JSTL to Deal with Session
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<c:if test="${pageContext.request.method=="POST"}">
<c:set var="uid" value="${param.uid}" scope="session" />
<jsp:forward page="session2.jsp" />
</c:if>
<html>
<head>
</head>
<body>
<form method="POST">
<table border="1" cellpadding="0" cellspacing="0"
style="border-collapse: collapse" bordercolor="#111111"
width="42%" id="AutoNumber1">
<tr>
<td width="100%" colspan="2" bgcolor="#0000FF">
<p align="center">
<b>
<font size="4" color="#FFFFFF">Please Login</font>
</b>
</p>
</td>
</tr>
<tr>
<td width="19%">User Name</td>
<td width="81%">
<input type="text" name="uid" size="20" />
</td>
</tr>
<tr>
<td width="19%">Password</td>
<td width="81%">
<input type="password" name="pwd" size="20" />
</td>
</tr>
<tr>
<td width="100%" colspan="2">
<p align="center">
<input type="submit" value="Submit" name="action" />
<input type="reset" value="Reset" name="B2" />
</p>
</td>
</tr>
</table>
</form>
<p align="left">
<i>Note: you may use any ID/Password, security is not
checked.</i>
</p>
</body>
</html>
View Session Date using JSTL
<%@page contentType="text/html"%>
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.ru/jstl/fmt" prefix="fmt" %>
<html>
<head><title>View Session JSP </title></head>
<body>
<h2>Session Info From A JSP</h2>
The session id: <c:out value="${pageContext.session.id}"/><br><br>
The session creation time as a long value: <c:out value="${pageContext.session.creationTime}"/><br><br>
The last accessed time as a long value: <c:out value="${pageContext.session.lastAccessedTime}"/><br><br>
</body>
</html>