JSP.6.1 Package javax.servlet.jspThe javax.servlet.jsp package contains a number of classes and interfaces that describe and define the contracts between a JSP page implementation class and the runtime environment provided for an instance of such a class by a conforming JSP container. JSP.6.1.1 JspPage and HttpJspPageTwo interfaces describe the interaction between a class implementing a JSP page and the JSP container: HttpJspPage and JspPage . Chapter JSP.3 describes the role of these two interfaces in detail. The JspPage contract is not described further here; see the javadoc documentation for details. The large majority of the JSP pages use the HTTP protocol and thus are based on the HttpJspPage contract. This interface has three methods , two of which can be redefined by the JSP author using a declaration scripting element:
JSP.6.1.2 JspWriterThe actions and template data in a JSP page is written using the JspWriter object that is referenced by the implicit variable out . This variable is initialized by code generated automatically by the JSP container (see the PageContext object in Section JSP.6.1.4). The initial JspWriter object is associated with the PrintWriter object of the ServletResponse in a way that depends on whether the page is or is not buffered. If the page is not buffered, output written to this JspWriter object will be written through to the PrintWriter directly, which will be created if necessary by invoking the getWriter() method on the response object. But if the page is buffered, the PrintWriter object will not be created until when the buffer is flushed, and operations like setContentType() are legal. Since this flexibility simplifies programming substantially, buffering is the default for JSP pages. Buffering raises the issue of what to do when the buffer is exceeded. Two approaches can be taken:
Both approaches are valid, and thus both are supported in the JSP technology. The behavior of a page is controlled by the autoFlush attribute, which defaults to true. In general, JSP pages that need to be sure that correct and complete data has been sent to their client may want to set autoFlush to false , with a typical case being that where the client is an application itself. On the other hand, JSP pages that send data that is meaningful even when partially constructed may want to set autoFlush to true ; a case may be when the data is sent for immediate display through a browser. Each application will need to consider its specific needs. An alternative considered was to make the buffer size unbounded, but this has the disadvantage that runaway computations may consume an unbounded amount of resources. The JspWriter interface includes behaviors from java.io.BufferedWriter and java.io.PrintWriter APIs but incorporates buffering and does not consume IOExceptions as PrintWriter does. If a particular use requires a PrintWriter , as in the case of desiring to insert an exception stack trace, one can be obtained by wrapping the JspWriter with a PrintWriter . The usual methods found in PrintWriter are available with the only modification being that the JspWriter methods do not consume IOExceptions . The additional methods are:
JSP.6.1.3 JspException and JspErrorJspException is a generic exception class. It currently has one subclass: JspError . A JspError has a message and it reflects an unexpected error that has been found during the execution of a JSP page. If uncaught, the error will result in an invocation of the error page machinery. JSP.6.1.4 PageContextA PageContext provides an object that encapsulates implementation-dependent features and provides convenience methods. A JSP page implementation class that uses a PageContext as shown in Figure JSP.6-1 will run unmodified in any compli-ant JSP container taking advantage of implementation-specific improvements like high performance JspWriters . JSP 1.1 compliant containers must generate JSP page implementation classes that use this PageContext object to provide insulation from platform implementation details. Figure JSP.6-1 Using PageContext to Provide Implementation Independencepublic void _jspService(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { JSPFactory factory = JSPFactory.getDefaultFactory(); // Get the page context object for this page PageContext pageContext = factory.getPageContext( this, request, response, null, // e.g. no errorPageURL, false, // e.g. no session JspWriter.DEFAULT_BUFFER, true // autoflush=true ); // Initialize implicit variables for scripting HttpSession session = pageContext.getSession(); JspWriter out = pageContext.getOut(); Object page = this; try { // body of JSP here ... } catch (Exception e) { out.clear(); pageContext.handlePageException(e); } finally { out.close(); factory.releasePageContext(pageContext); } } A PageContext object is initialized by a JSP container implementation early on in the processing of the _jspService() method. The PageContext implementation itself is implementation dependent, and is obtained via a creation method on the JspFactory . The PageContext provides a number of facilities, including:
JSP.6.1.4.1 CreationThe PageContext class is an abstract class, designed to be extended by conforming JSP container runtime environments to provide implementation-dependent implementations . An instance of a PageContext is created by a JSP container-specific implementation class at the beginning of its _jspService() method via an implementation-specific default JspFactory , as shown in Figure JSP.6-1: JSP.6.1.4.2 UsageThe PageContext object provides access to multiple functionality. Uniform Access to Multiple ScopesThese methods provide uniform access to the diverse scope objects. The implementation must use the underlying servlet machinery corresponding to that scope, so information can be passed back and forth between servlets and JSP pages.
Access to Implicit ObjectsThese methods provide convenience access to implicit objects and other objects that can be obtained by different ways.
Management of Nested ScopesThese methods enable the management of nested JspWriter streams to implement tag extensions. Note that pushBody() returns a BodyContent , while popBody() returns a JspWriter , which will need to be casted into a BodyContent in all but the top level.
Management of PageContext ObjectThe following two methods provide management of the PageContext object itself. These methods are not intended to be used by the JSP page author.
Forward and IncludeThese methods encapsulate forwarding and inclusion.
JSP.6.1.5 JspEngineInfoThe JspEngineInfo class provides information on the JSP container implementation. The only method currently available is:
JSP.6.1.6 JspFactoryThe JspFactory provides a mechanism to instantiate platform-dependent objects in a platform-independent manner. The PageContext class and the JspEngineInfo class are the only implementation-dependent classes that can be created from the factory. Typically, at initialization time, a JSP container will call the static setDefaultFactory() method in order to register its own factory implementation. JSP page implementation classes will use the getDefaultFactory() method in order to obtain this factory and use it to construct PageContext instances in order to process client requests . JspFactory objects are not intended to be used by JSP page authors. |