Java Tutorial/JSP/Session — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 05:07, 1 июня 2010
Get Session ID, creation time and last accessed time
<%@page import = "java.util.*" session="true"%>
<HTML>
<HEAD>
<TITLE>Using Sessions to Track Users</TITLE>
</HEAD>
<BODY>
<%
Integer counter = (Integer)session.getAttribute("counter");
if (counter == null) {
counter = new Integer(1);
} else {
counter = new Integer(counter.intValue() + 1);
}
session.setAttribute("counter", counter);
%>
<H1>Using Sessions to Track Users</H1>
Session ID: <%=session.getId()%>
<BR>
Session creation time: <%=new Date(session.getCreationTime())%>
<BR>
Last accessed time: <%=new Date(session.getLastAccessedTime())%>
<BR>
Number of times you"ve been here: <%=counter%>
</BODY>
</HTML>
Reference Session Value Across JSP Page
<html>
<head>
<title>Session Example</title>
</head>
<body>
<%
String val = request.getParameter("name");
if (val != null)
session.setAttribute("name", val);
%>
<center>
<h1>Session Example</h1>
Where would you like to go?<br><br>
== Session Scope Bean ==
<p>Counter.java</p>
<!-- start source code -->
<source lang="java">
package beans;
public class Counter
{
private int counter = 0;
public void setCounter(int value)
{
this.counter = value;
}
public int getCounter()
{
return this.counter;
}
public Counter()
{
}
}
Set and get variable to a session
<HTML>
<HEAD>
<TITLE>Using the Application Object</TITLE>
</HEAD>
<BODY>
<H1>Using the Application Object</H1>
<%
Integer counter = (Integer)session.getAttribute("counter");
String heading = null;
if (counter == null) {
counter = new Integer(1);
} else {
counter = new Integer(counter.intValue() + 1);
}
session.setAttribute("counter", counter);
Integer i = (Integer)application.getAttribute("i");
if (i == null) {
i = new Integer(1);
} else {
i = new Integer(i.intValue() + 1);
}
application.setAttribute("i", i);
%>
You have visited this page <%=counter%> times.
<BR>
This page has been visited by all users <%=i%> times.
</BODY>
</HTML>