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. package javax.servlet.jsp;
  56. import java.io.IOException;
  57. import java.util.Enumeration;
  58. import javax.servlet.Servlet;
  59. import javax.servlet.ServletConfig;
  60. import javax.servlet.ServletContext;
  61. import javax.servlet.ServletException;
  62. import javax.servlet.ServletRequest;
  63. import javax.servlet.ServletResponse;
  64. import javax.servlet.http.HttpServletRequest;
  65. import javax.servlet.http.HttpSession;
  66. import javax.servlet.jsp.tagext.BodyContent;
  67. /**
  68. * <p>
  69. * A PageContext instance provides access to all the namespaces associated
  70. * with a JSP page, provides access to several page attributes, as well as
  71. * a layer above the implementation details. Implicit objects are added
  72. * the pageContext automatically.
  73. *
  74. * <p> The <code> PageContext </code> class is an abstract class, designed to be
  75. * extended to provide implementation dependent implementations thereof, by
  76. * conformant JSP engine runtime environments. A PageContext instance is
  77. * obtained by a JSP implementation class by calling the
  78. * JspFactory.getPageContext() method, and is released by calling
  79. * JspFactory.releasePageContext().
  80. *
  81. * <p> An example of how PageContext, JspFactory, and other classes can be
  82. * used within a JSP Page Implementation object is given elsewhere.
  83. *
  84. * <p>
  85. * The PageContext provides a number of facilities to the page/component author and
  86. * page implementor, including:
  87. * <ul>
  88. * <li>a single API to manage the various scoped namespaces
  89. * <li>a number of convenience API's to access various public objects
  90. * <li>a mechanism to obtain the JspWriter for output
  91. * <li>a mechanism to manage session usage by the page
  92. * <li>a mechanism to expose page directive attributes to the scripting environment
  93. * <li>mechanisms to forward or include the current request to other active components in the application
  94. * <li>a mechanism to handle errorpage exception processing
  95. * </ul>
  96. *
  97. * <p><B>Methods Intended for Container Generated Code</B>
  98. * <p>Some methods are intended to be used by the code generated by the
  99. * container, not by code written by JSP page authors, or JSP tag library authors.
  100. * <p>The methods supporting <B>lifecycle</B> are <code>initialize()</code>
  101. * and <code>release()</code>
  102. *
  103. * <p>
  104. * The following methods enable the <B>management of nested</B> JspWriter streams to
  105. * implement Tag Extensions: <code>pushBody()</code> and <code>popBody()</code>
  106. *
  107. * <p><B>Methods Intended for JSP authors</B>
  108. * <p>
  109. * Some methods provide <B>uniform access</B> to the diverse objects
  110. * representing scopes.
  111. * The implementation must use the underlying Servlet machinery
  112. * corresponding to that scope, so information can be passed back and
  113. * forth between Servlets and JSP pages. The methods are:
  114. * <code>setAttribute()</code>, <code>getAttribute()</code>,
  115. * <code>findAttribute()</code>, <code>removeAttribute()</code>,
  116. * <code>getAttributesScope()</code> and <code>getAttributeNamesInScope()</code> .
  117. *
  118. * <p>
  119. * The following methods provide <B>convenient access</B> to implicit objects:
  120. * <ul>
  121. * <code>getOut()</code>, <code>getException()</code>, <code>getPage()</code>
  122. * <code>getRequest()</code>, <code>getResponse()</code>,
  123. * <code>getSession()</code>, <code>getServletConfig()</code>
  124. * and <code>getServletContext()</code>.
  125. *
  126. * <p>
  127. * The following methods provide support for <B>forwarding, inclusion
  128. * and error handling</B>:
  129. * <code>forward()</code>, <code>include()</code>,
  130. * and <code>handlePageException()</code>.
  131. */
  132. abstract public class PageContext {
  133. /**
  134. * Page scope: (this is the default) the named reference remains available
  135. * in this PageContext until the return from the current Servlet.service()
  136. * invocation.
  137. */
  138. public static final int PAGE_SCOPE = 1;
  139. /**
  140. * Request scope: the named reference remains available from the ServletRequest
  141. * associated with the Servlet until the current request is completed.
  142. */
  143. public static final int REQUEST_SCOPE = 2;
  144. /**
  145. * Session scope (only valid if this page participates in a session):
  146. * the named reference remains available from the HttpSession (if any)
  147. * associated with the Servlet until the HttpSession is invalidated.
  148. */
  149. public static final int SESSION_SCOPE = 3;
  150. /**
  151. * Application scope: named reference remains available in the
  152. * ServletContext until it is reclaimed.
  153. */
  154. public static final int APPLICATION_SCOPE = 4;
  155. /**
  156. * Name used to store the Servlet in this PageContext's nametables.
  157. */
  158. public static final String PAGE = "javax.servlet.jsp.jspPage";
  159. /**
  160. * Name used to store this PageContext in it's own name table.
  161. */
  162. public static final String PAGECONTEXT = "javax.servlet.jsp.jspPageContext";
  163. /**
  164. * Name used to store ServletRequest in PageContext name table.
  165. */
  166. public static final String REQUEST = "javax.servlet.jsp.jspRequest";
  167. /**
  168. * Name used to store ServletResponse in PageContext name table.
  169. */
  170. public static final String RESPONSE = "javax.servlet.jsp.jspResponse";
  171. /**
  172. * Name used to store ServletConfig in PageContext name table.
  173. */
  174. public static final String CONFIG = "javax.servlet.jsp.jspConfig";
  175. /**
  176. * Name used to store HttpSession in PageContext name table.
  177. */
  178. public static final String SESSION = "javax.servlet.jsp.jspSession";
  179. /**
  180. * Name used to store current JspWriter in PageContext name table.
  181. */
  182. public static final String OUT = "javax.servlet.jsp.jspOut";
  183. /**
  184. * Name used to store ServletContext in PageContext name table.
  185. */
  186. public static final String APPLICATION = "javax.servlet.jsp.jspApplication";
  187. /**
  188. * Name used to store uncaught exception in ServletRequest attribute list and PageContext name table.
  189. */
  190. public static final String EXCEPTION = "javax.servlet.jsp.jspException";
  191. /**
  192. * <p>
  193. * The initialize method is called to initialize an uninitialized PageContext
  194. * so that it may be used by a JSP Implementation class to service an
  195. * incoming request and response within it's _jspService() method.
  196. *
  197. * <p>
  198. * This method is typically called from JspFactory.getPageContext() in
  199. * order to initialize state.
  200. *
  201. * <p>
  202. * This method is required to create an initial JspWriter, and associate
  203. * the "out" name in page scope with this newly created object.
  204. *
  205. * <p>
  206. * This method should not be used by page or tag library authors.
  207. *
  208. * @param servlet The Servlet that is associated with this PageContext
  209. * @param request The currently pending request for this Servlet
  210. * @param response The currently pending response for this Servlet
  211. * @param errorPageURL The value of the errorpage attribute from the page directive or null
  212. * @param needsSession The value of the session attribute from the page directive
  213. * @param bufferSize The value of the buffer attribute from the page directive
  214. * @param autoFlush The value of the autoflush attribute from the page directive
  215. *
  216. * @throws IOException during creation of JspWriter
  217. * @throws IllegalStateException if out not correctly initialized
  218. */
  219. abstract public void initialize(Servlet servlet, ServletRequest request, ServletResponse response, String errorPageURL, boolean needsSession, int bufferSize, boolean autoFlush) throws IOException, IllegalStateException, IllegalArgumentException;
  220. /**
  221. * <p>
  222. * This method shall "reset" the internal state of a PageContext, releasing
  223. * all internal references, and preparing the PageContext for potential
  224. * reuse by a later invocation of initialize(). This method is typically
  225. * called from JspFactory.releasePageContext().
  226. *
  227. * <p>
  228. * Subclasses shall envelope this method.
  229. *
  230. * <p>
  231. * This method should not be used by page or tag library authors.
  232. *
  233. */
  234. abstract public void release();
  235. /**
  236. * Register the name and object specified with page scope semantics.
  237. *
  238. * @param name the name of the attribute to set
  239. * @param attribute the object to associate with the name
  240. *
  241. * @throws NullPointerException if the name or object is null
  242. */
  243. abstract public void setAttribute(String name, Object attribute);
  244. /**
  245. * register the name and object specified with appropriate scope semantics
  246. *
  247. * @param name the name of the attribute to set
  248. * @param o the object to associate with the name
  249. * @param scope the scope with which to associate the name/object
  250. *
  251. * @throws NullPointerException if the name or object is null
  252. * @throws IllegalArgumentException if the scope is invalid
  253. *
  254. */
  255. abstract public void setAttribute(String name, Object o, int scope);
  256. /**
  257. * Return the object associated with the name in the page scope or null
  258. * if not found.
  259. *
  260. * @param name the name of the attribute to get
  261. *
  262. * @throws NullPointerException if the name is null
  263. * @throws IllegalArgumentException if the scope is invalid
  264. */
  265. abstract public Object getAttribute(String name);
  266. /**
  267. * Return the object associated with the name in the specified
  268. * scope or null if not found.
  269. *
  270. * @param name the name of the attribute to set
  271. * @param scope the scope with which to associate the name/object
  272. *
  273. * @throws NullPointerException if the name is null
  274. * @throws IllegalArgumentException if the scope is invalid */
  275. abstract public Object getAttribute(String name, int scope);
  276. /**
  277. * Searches for the named attribute in page, request, session (if valid),
  278. * and application scope(s) in order and returns the value associated or
  279. * null.
  280. *
  281. * @return the value associated or null
  282. */
  283. abstract public Object findAttribute(String name);
  284. /**
  285. * Remove the object reference associated with the given name,
  286. * look in all scopes in the scope order.
  287. *
  288. * @param name The name of the object to remove.
  289. */
  290. abstract public void removeAttribute(String name);
  291. /**
  292. * Remove the object reference associated with the specified name
  293. * in the given scope.
  294. *
  295. * @param name The name of the object to remove.
  296. * @param scope The scope where to look.
  297. */
  298. abstract public void removeAttribute(String name, int scope);
  299. /**
  300. * Get the scope where a given attribute is defined.
  301. *
  302. * @return the scope of the object associated with the name specified or 0
  303. */
  304. abstract public int getAttributesScope(String name);
  305. /**
  306. * Enumerate all the attributes in a given scope
  307. *
  308. * @return an enumeration of names (java.lang.String) of all the attributes the specified scope
  309. */
  310. abstract public Enumeration getAttributeNamesInScope(int scope);
  311. /**
  312. * The current value of the out object (a JspWriter).
  313. *
  314. * @return the current JspWriter stream being used for client response
  315. */
  316. abstract public JspWriter getOut();
  317. /**
  318. * The current value of the session object (an HttpSession).
  319. *
  320. * @return the HttpSession for this PageContext or null
  321. */
  322. abstract public HttpSession getSession();
  323. /**
  324. * The current value of the page object (a Servlet).
  325. *
  326. * @return the Page implementation class instance (Servlet) associated with this PageContext
  327. */
  328. abstract public Object getPage();
  329. /**
  330. * The current value of the request object (a ServletRequest).
  331. *
  332. * @return The ServletRequest for this PageContext
  333. */
  334. abstract public ServletRequest getRequest();
  335. /**
  336. * The current value of the response object (a ServletResponse).
  337. *
  338. * @return the ServletResponse for this PageContext
  339. */
  340. abstract public ServletResponse getResponse();
  341. /**
  342. * The current value of the exception object (an Exception).
  343. *
  344. * @return any exception passed to this as an errorpage
  345. */
  346. abstract public Exception getException();
  347. /**
  348. * The ServletConfig instance.
  349. *
  350. * @return the ServletConfig for this PageContext
  351. */
  352. abstract public ServletConfig getServletConfig();
  353. /**
  354. * The ServletContext instance.
  355. *
  356. * @return the ServletContext for this PageContext
  357. */
  358. abstract public ServletContext getServletContext();
  359. /**
  360. * <p>
  361. * This method is used to re-direct, or "forward" the current ServletRequest and ServletResponse to another active component in the application.
  362. * </p>
  363. * <p>
  364. * If the <I> relativeUrlPath </I> begins with a "/" then the URL specified
  365. * is calculated relative to the DOCROOT of the <code> ServletContext </code>
  366. * for this JSP. If the path does not begin with a "/" then the URL
  367. * specified is calculated relative to the URL of the request that was
  368. * mapped to the calling JSP.
  369. * </p>
  370. * <p>
  371. * It is only valid to call this method from a <code> Thread </code>
  372. * executing within a <code> _jspService(...) </code> method of a JSP.
  373. * </p>
  374. * <p>
  375. * Once this method has been called successfully, it is illegal for the
  376. * calling <code> Thread </code> to attempt to modify the <code>
  377. * ServletResponse </code> object. Any such attempt to do so, shall result
  378. * in undefined behavior. Typically, callers immediately return from
  379. * <code> _jspService(...) </code> after calling this method.
  380. * </p>
  381. *
  382. * @param relativeUrlPath specifies the relative URL path to the target resource as described above
  383. *
  384. * @throws ServletException
  385. * @throws IOException
  386. *
  387. * @throws IllegalArgumentException if target resource URL is unresolvable
  388. * @throws IllegalStateException if <code> ServletResponse </code> is not in a state where a forward can be performed
  389. * @throws SecurityException if target resource cannot be accessed by caller
  390. */
  391. abstract public void forward(String relativeUrlPath) throws ServletException, IOException;
  392. /**
  393. * <p>
  394. * Causes the resource specified to be processed as part of the current
  395. * ServletRequest and ServletResponse being processed by the calling Thread.
  396. * The output of the target resources processing of the request is written
  397. * directly to the ServletResponse output stream.
  398. * </p>
  399. * <p>
  400. * The current JspWriter "out" for this JSP is flushed as a side-effect
  401. * of this call, prior to processing the include.
  402. * </p>
  403. * <p>
  404. * If the <I> relativeUrlPath </I> begins with a "/" then the URL specified
  405. * is calculated relative to the DOCROOT of the <code> ServletContext </code>
  406. * for this JSP. If the path does not begin with a "/" then the URL
  407. * specified is calculated relative to the URL of the request that was
  408. * mapped to the calling JSP.
  409. * </p>
  410. * <p>
  411. * It is only valid to call this method from a <code> Thread </code>
  412. * executing within a <code> _jspService(...) </code> method of a JSP.
  413. * </p>
  414. *
  415. * @param relativeUrlPath specifies the relative URL path to the target resource to be included
  416. *
  417. * @throws ServletException
  418. * @throws IOException
  419. *
  420. * @throws IllegalArgumentException if the target resource URL is unresolvable
  421. * @throws SecurityException if target resource cannot be accessed by caller
  422. *
  423. */
  424. abstract public void include(String relativeUrlPath) throws ServletException, IOException;
  425. /**
  426. * <p>
  427. * This method is intended to process an unhandled "page" level exception
  428. * by redirecting the exception to either the specified error page for this
  429. * JSP, or if none was specified, to perform some implementation dependent
  430. * action.
  431. *
  432. * <p>
  433. * A JSP implementation class shall typically clean up any local state
  434. * prior to invoking this and will return immediately thereafter. It is
  435. * illegal to generate any output to the client, or to modify any
  436. * ServletResponse state after invoking this call.
  437. *
  438. * <p>
  439. * This method is kept for backwards compatiblity reasons. Newly
  440. * generated code should use PageContext.handlePageException(Throwable).
  441. *
  442. * @param e the exception to be handled
  443. *
  444. * @throws ServletException
  445. * @throws IOException
  446. *
  447. * @throws NullPointerException if the exception is null
  448. * @throws SecurityException if target resource cannot be accessed by caller
  449. *
  450. * @see #handlePageException(Throwable)
  451. */
  452. abstract public void handlePageException(Exception e) throws ServletException, IOException;
  453. /**
  454. * <p>
  455. * This method is identical to the handlePageException(Exception),
  456. * except that it accepts a Throwable. This is the preferred method
  457. * to use as it allows proper implementation of the errorpage
  458. * semantics.
  459. *
  460. * <p>
  461. * This method is intended to process an unhandled "page" level exception
  462. * by redirecting the exception to either the specified error page for this
  463. * JSP, or if none was specified, to perform some implementation dependent
  464. * action.
  465. *
  466. * <p>
  467. * A JSP implementation class shall typically clean up any local state
  468. * prior to invoking this and will return immediately thereafter. It is
  469. * illegal to generate any output to the client, or to modify any
  470. * ServletResponse state after invoking this call.
  471. *
  472. * @param t the throwable to be handled
  473. *
  474. * @throws ServletException
  475. * @throws IOException
  476. *
  477. * @throws NullPointerException if the exception is null
  478. * @throws SecurityException if target resource cannot be accessed by caller
  479. *
  480. * @see #handlePageException(Exception)
  481. */
  482. abstract public void handlePageException(Throwable t) throws ServletException, IOException;
  483. /**
  484. * Return a new BodyContent object, save the current "out" JspWriter,
  485. * and update the value of the "out" attribute in the page scope
  486. * attribute namespace of the PageContext
  487. *
  488. * @return the new BodyContent
  489. */
  490. public BodyContent pushBody() {
  491. return null; // XXX to implement
  492. }
  493. /**
  494. * Return the previous JspWriter "out" saved by the matching
  495. * pushBody(), and update the value of the "out" attribute in
  496. * the page scope attribute namespace of the PageConxtext
  497. *
  498. * @return the saved JspWriter.
  499. */
  500. public JspWriter popBody() {
  501. return null; // XXX to implement
  502. }
  503. }