📄 pi3httpsession.java
字号:
/*____________________________________________________________________________*\
*
Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.
These sources, libraries and applications are
FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
as long as the following conditions are adhered to.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*____________________________________________________________________________*|
*
* $Source: /cvsroot/pi3web/Pi3Web_200/Source/Servlet/org/pi3/servlet/core/Pi3HttpSession.java,v $
* $Date: 2003/05/13 18:42:21 $
*
Description:
Implementation of servlet API HttpSession interface.
\*____________________________________________________________________________*/
package org.pi3.servlet.core;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionBindingEvent;
import java.util.Hashtable;
import java.util.Enumeration;
/**
*
* Provides a way to identify a user across more than one page
* request or visit to a Web site.
*
* <p>The servlet engine uses this interface to create a session
* between an HTTP client and an HTTP server. The session persists
* for a specified time period, across more than one connection or
* page request from the user. A session usually corresponds to one
* user, who may visit a site many times. The server can maintain a
* session either by using cookies or by rewriting URLs.
*
* <p>This interface allows servlets to
* <ul>
* <li>View and manipulate information about a session, such as
* the session identifier, creation time, or context
* <li>Bind objects to sessions, allowing you to use an
* online shopping cart
* to hold data that persists across multiple user connections
* </ul>
*
* <p> <code>HttpSession</code> defines methods that
* store these types of data:
* <ul>
* <li>Standard session properties, such as a session identifier
* or session context
* <li>Data that the application provides, accessed using this
* interface and stored using a dictionary-like interface
* </ul>
*
* <p>The servlet can obtain data from the session, modify it,
* and return it to the session, as this example shows:
*
* <code>
* //Get the session object - "request" represents the HTTP servlet request
* HttpSession session = request.getSession(true);
*
* //Get the session data value - an Integer object is read from
* //the session, incremented, then written back to the session.
* //sessiontest.counter identifies values in the session
* Integer ival = (Integer) session.getValue("sessiontest.counter");
* if (ival==null)
* ival = new Integer(1);
* else
* ival = new Integer(ival.intValue() + 1);
* session.putValue("sessiontest.counter", ival);
* </code>
*
* <p>When an application stores an object in or removes it from a
* session, the session checks whether the object implements
* {@link HttpSessionBindingListener}. If it does,
* the servlet notifies the object that it has been bound to or unbound
* from the session.
*
* <p>An HTTP session represents the server's view of the session.
* The server considers a session new under any of these conditions:
* <ul>
* <li>The client does not yet know about the session
* <li>The session has not yet begun
* <li>The client chooses not to join the session, for example,
* if the server supports only cookies and the client rejects the
* cookies the server sends
* </ul>
* When the session is new, the {@link #isNew} method
* returns <code>true</code>.
*
* <p>You must write your servlet to handle cases in which
* the client has not joined a session. The following code
* example calls <code>isNew</code> to determine whether
* a session is new. If it is, the server requires the client
* to start a session by directing the client to a welcome
* page which the user must enter before gaining access to
* other pages on the site.
*
* <code>
* //Get the session object - "request" represents the HTTP servlet request
* HttpSession session = request.getSession(true);
*
* //insist that the client starts a session
* //before access to data is allowed
* //"response" represents the HTTP servlet response
* if (session.isNew()) {
* response.sendRedirect (welcomeURL);
* }
* </code>
*
*
* @author Various
* @version $Version$
*
*
* @see HttpSessionBindingListener
* @see Pi3HttpSessionContext
*
*/
public final class Pi3HttpSession implements HttpSession {
private String sid = null;
private Hashtable obj = new Hashtable();
private long tLastAccess = 0;
private long tCreated = 0;
private int maxInactiveInt = 0;
private boolean bValid = false;
private boolean bNew = false;
/** This constructor is used by the native side */
protected Pi3HttpSession(String sSID) {
sid = sSID;
bValid = true;
bNew = true;
tCreated = System.currentTimeMillis();
tLastAccess = tCreated;
maxInactiveInt = Constants.MAX_SESSION_INACTIVE_INTERVAL;
}
/** This method is used by the native side */
protected boolean isValid () {
return bValid;
}
/** This method is used by the native side */
protected String getSID () {
return sid;
}
/** This method is used by the native side */
protected void touch() throws IllegalStateException {
bNew = false;
long tCurrent = System.currentTimeMillis();
if (bValid) {
if ( maxInactiveInt != -1 ) {
bValid = tLastAccess + maxInactiveInt*1000 >= tCurrent;
};
if ((Constants.MAX_SESSION_LIFETIME != -1) && bValid ) {
bValid = tCreated + Constants.MAX_SESSION_LIFETIME*1000 >= tCurrent;
}
};
tLastAccess = tCurrent;
}
/**
*
* Returns an enumeration containing the names of all the objects
* bound to this session. This method is useful, for example,
* when you want to delete all the objects bound to this
* session. This method doesn't belong to the Servlet API 2.1
* but was added for the JspPagecontext implementation, where
* an Enumeration is required.
*
* @return an Enumeration of Strings specifying the
* names of all the objects bound to
* this session
*
* @exception IllegalStateException if the session is invalid
*
*/
public Enumeration getPi3ValueNames() {
if (!bValid) {
IllegalStateException e = new IllegalStateException();
throw e;
};
return obj.keys();
};
/**
*
* Returns the time when this session was created, measured
* in milliseconds since midnight January 1, 1970 GMT.
*
* @return a <code>long</code> integer specifying
* when this session was created relative
* to 1-1-1970 GMT
*
* @exception IllegalStateException if you attempt to get the session's
* creation time after the session has
* been invalidated
*
*/
public long getCreationTime () {
if (!bValid) {
IllegalStateException e = new IllegalStateException();
throw e;
};
return tCreated;
}
/**
*
* Returns a string containing the unique identifier assigned
* to this session. The identifier is assigned
* by the servlet engine and is implementation dependent.
*
* @return a string specifying the identifier
* assigned to this session
*
* @exeption IllegalStateException if the session is invalid
*
*/
public String getId () {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -