1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 1999 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. *
  54. * ====================================================================
  55. *
  56. * This source code implements specifications defined by the Java
  57. * Community Process. In order to remain compliant with the specification
  58. * DO NOT add / change / or delete method signatures!
  59. */
  60. package javax.servlet.http;
  61. import java.util.Enumeration;
  62. import javax.servlet.ServletContext;
  63. /**
  64. *
  65. * Provides a way to identify a user across more than one page
  66. * request or visit to a Web site and to store information about that user.
  67. *
  68. * <p>The servlet container uses this interface to create a session
  69. * between an HTTP client and an HTTP server. The session persists
  70. * for a specified time period, across more than one connection or
  71. * page request from the user. A session usually corresponds to one
  72. * user, who may visit a site many times. The server can maintain a
  73. * session in many ways such as using cookies or rewriting URLs.
  74. *
  75. * <p>This interface allows servlets to
  76. * <ul>
  77. * <li>View and manipulate information about a session, such as
  78. * the session identifier, creation time, and last accessed time
  79. * <li>Bind objects to sessions, allowing user information to persist
  80. * across multiple user connections
  81. * </ul>
  82. *
  83. * <p>When an application stores an object in or removes an object from a
  84. * session, the session checks whether the object implements
  85. * {@link HttpSessionBindingListener}. If it does,
  86. * the servlet notifies the object that it has been bound to or unbound
  87. * from the session. Notifications are sent after the binding methods complete.
  88. * For session that are invalidated or expire, notifications are sent after
  89. * the session has been invalidatd or expired.
  90. *
  91. * <p> When container migrates a session between VMs in a distributed container
  92. * setting, all session atributes implementing the {@link HttpSessionActivationListener}
  93. * interface are notified.
  94. *
  95. * <p>A servlet should be able to handle cases in which
  96. * the client does not choose to join a session, such as when cookies are
  97. * intentionally turned off. Until the client joins the session,
  98. * <code>isNew</code> returns <code>true</code>. If the client chooses
  99. * not to join
  100. * the session, <code>getSession</code> will return a different session
  101. * on each request, and <code>isNew</code> will always return
  102. * <code>true</code>.
  103. *
  104. * <p>Session information is scoped only to the current web application
  105. * (<code>ServletContext</code>), so information stored in one context
  106. * will not be directly visible in another.
  107. *
  108. * @author Various
  109. * @version $Version$
  110. *
  111. *
  112. * @see HttpSessionBindingListener
  113. * @see HttpSessionContext
  114. *
  115. */
  116. public interface HttpSession {
  117. /**
  118. *
  119. * Returns the time when this session was created, measured
  120. * in milliseconds since midnight January 1, 1970 GMT.
  121. *
  122. * @return a <code>long</code> specifying
  123. * when this session was created,
  124. * expressed in
  125. * milliseconds since 1/1/1970 GMT
  126. *
  127. * @exception IllegalStateException if this method is called on an
  128. * invalidated session
  129. *
  130. */
  131. public long getCreationTime();
  132. /**
  133. *
  134. * Returns a string containing the unique identifier assigned
  135. * to this session. The identifier is assigned
  136. * by the servlet container and is implementation dependent.
  137. *
  138. * @return a string specifying the identifier
  139. * assigned to this session
  140. *
  141. * @exeption IllegalStateException if this method is called on an
  142. * invalidated session
  143. *
  144. */
  145. public String getId();
  146. /**
  147. *
  148. * Returns the last time the client sent a request associated with
  149. * this session, as the number of milliseconds since midnight
  150. * January 1, 1970 GMT, and marked by the time the container recieved the request.
  151. *
  152. * <p>Actions that your application takes, such as getting or setting
  153. * a value associated with the session, do not affect the access
  154. * time.
  155. *
  156. * @return a <code>long</code>
  157. * representing the last time
  158. * the client sent a request associated
  159. * with this session, expressed in
  160. * milliseconds since 1/1/1970 GMT
  161. *
  162. * @exeption IllegalStateException if this method is called on an
  163. * invalidated session
  164. *
  165. */
  166. public long getLastAccessedTime();
  167. /**
  168. * Returns the ServletContext to which this session belongs.
  169. *
  170. * @return The ServletContext object for the web application
  171. * @since 2.3
  172. */
  173. public ServletContext getServletContext();
  174. /**
  175. *
  176. * Specifies the time, in seconds, between client requests before the
  177. * servlet container will invalidate this session. A negative time
  178. * indicates the session should never timeout.
  179. *
  180. * @param interval An integer specifying the number
  181. * of seconds
  182. *
  183. */
  184. public void setMaxInactiveInterval(int interval);
  185. /**
  186. * Returns the maximum time interval, in seconds, that
  187. * the servlet container will keep this session open between
  188. * client accesses. After this interval, the servlet container
  189. * will invalidate the session. The maximum time interval can be set
  190. * with the <code>setMaxInactiveInterval</code> method.
  191. * A negative time indicates the session should never timeout.
  192. *
  193. *
  194. * @return an integer specifying the number of
  195. * seconds this session remains open
  196. * between client requests
  197. *
  198. * @see #setMaxInactiveInterval
  199. *
  200. *
  201. */
  202. public int getMaxInactiveInterval();
  203. /**
  204. *
  205. * @deprecated As of Version 2.1, this method is
  206. * deprecated and has no replacement.
  207. * It will be removed in a future
  208. * version of the Java Servlet API.
  209. *
  210. */
  211. public HttpSessionContext getSessionContext();
  212. /**
  213. *
  214. * Returns the object bound with the specified name in this session, or
  215. * <code>null</code> if no object is bound under the name.
  216. *
  217. * @param name a string specifying the name of the object
  218. *
  219. * @return the object with the specified name
  220. *
  221. * @exception IllegalStateException if this method is called on an
  222. * invalidated session
  223. *
  224. */
  225. public Object getAttribute(String name);
  226. /**
  227. *
  228. * @deprecated As of Version 2.2, this method is
  229. * replaced by {@link #getAttribute}.
  230. *
  231. * @param name a string specifying the name of the object
  232. *
  233. * @return the object with the specified name
  234. *
  235. * @exception IllegalStateException if this method is called on an
  236. * invalidated session
  237. *
  238. */
  239. public Object getValue(String name);
  240. /**
  241. *
  242. * Returns an <code>Enumeration</code> of <code>String</code> objects
  243. * containing the names of all the objects bound to this session.
  244. *
  245. * @return an <code>Enumeration</code> of
  246. * <code>String</code> objects specifying the
  247. * names of all the objects bound to
  248. * this session
  249. *
  250. * @exception IllegalStateException if this method is called on an
  251. * invalidated session
  252. *
  253. */
  254. public Enumeration getAttributeNames();
  255. /**
  256. *
  257. * @deprecated As of Version 2.2, this method is
  258. * replaced by {@link #getAttributeNames}
  259. *
  260. * @return an array of <code>String</code>
  261. * objects specifying the
  262. * names of all the objects bound to
  263. * this session
  264. *
  265. * @exception IllegalStateException if this method is called on an
  266. * invalidated session
  267. *
  268. */
  269. public String[] getValueNames();
  270. /**
  271. * Binds an object to this session, using the name specified.
  272. * If an object of the same name is already bound to the session,
  273. * the object is replaced.
  274. *
  275. * <p>After this method executes, and if the new object
  276. * implements <code>HttpSessionBindingListener</code>,
  277. * the container calls
  278. * <code>HttpSessionBindingListener.valueBound</code>. The container then
  279. * notifies any <code>HttpSessionAttributeListener</code>s in the web
  280. * application.
  281. * <p>If an object was already bound to this session of this name
  282. * that implements <code>HttpSessionBindingListener</code>, its
  283. * <code>HttpSessionBindingListener.valueUnbound</code> method is called.
  284. *
  285. * <p>If the value passed in is null, this has the same effect as calling
  286. * <code>removeAttribute()<code>.
  287. *
  288. *
  289. * @param name the name to which the object is bound;
  290. * cannot be null
  291. *
  292. * @param value the object to be bound
  293. *
  294. * @exception IllegalStateException if this method is called on an
  295. * invalidated session
  296. *
  297. */
  298. public void setAttribute(String name, Object value);
  299. /**
  300. *
  301. * @deprecated As of Version 2.2, this method is
  302. * replaced by {@link #setAttribute}
  303. *
  304. * @param name the name to which the object is bound;
  305. * cannot be null
  306. *
  307. * @param value the object to be bound; cannot be null
  308. *
  309. * @exception IllegalStateException if this method is called on an
  310. * invalidated session
  311. *
  312. */
  313. public void putValue(String name, Object value);
  314. /**
  315. *
  316. * Removes the object bound with the specified name from
  317. * this session. If the session does not have an object
  318. * bound with the specified name, this method does nothing.
  319. *
  320. * <p>After this method executes, and if the object
  321. * implements <code>HttpSessionBindingListener</code>,
  322. * the container calls
  323. * <code>HttpSessionBindingListener.valueUnbound</code>. The container
  324. * then notifies any <code>HttpSessionAttributeListener</code>s in the web
  325. * application.
  326. *
  327. *
  328. *
  329. * @param name the name of the object to
  330. * remove from this session
  331. *
  332. * @exception IllegalStateException if this method is called on an
  333. * invalidated session
  334. */
  335. public void removeAttribute(String name);
  336. /**
  337. *
  338. * @deprecated As of Version 2.2, this method is
  339. * replaced by {@link #removeAttribute}
  340. *
  341. * @param name the name of the object to
  342. * remove from this session
  343. *
  344. * @exception IllegalStateException if this method is called on an
  345. * invalidated session
  346. */
  347. public void removeValue(String name);
  348. /**
  349. *
  350. * Invalidates this session then unbinds any objects bound
  351. * to it.
  352. *
  353. * @exception IllegalStateException if this method is called on an
  354. * already invalidated session
  355. *
  356. */
  357. public void invalidate();
  358. /**
  359. *
  360. * Returns <code>true</code> if the client does not yet know about the
  361. * session or if the client chooses not to join the session. For
  362. * example, if the server used only cookie-based sessions, and
  363. * the client had disabled the use of cookies, then a session would
  364. * be new on each request.
  365. *
  366. * @return <code>true</code> if the
  367. * server has created a session,
  368. * but the client has not yet joined
  369. *
  370. * @exception IllegalStateException if this method is called on an
  371. * already invalidated session
  372. *
  373. */
  374. public boolean isNew();
  375. }