Given a design goal, write JSP code using the appropriate implicit objects: (a) request, (b) response, (c) out, (d) session, (e) config, (f) application, (g) page, (h) pageContext, and (i) exception.

The request object.

Protocol dependent subtype of javax.servlet.ServletRequest, e.g: javax.servlet.http.HttpServletRequest.

This is the HttpServletRequest associated with the request, and lets you look at the request parameters (via getParameter), the request type (GET, POST, HEAD, etc.), and the incoming HTTP headers (cookies, referer, etc.).

The request triggering the service invocation. Has a request scope.

	String path = request.getContextPath();
	String name = request.getParameter("name");


The response object.

Protocol dependent subtype of javax.servlet.ServletResponse, e.g: javax.servlet.http.HttpServletResponse.

This is the HttpServletResponse associated with the response to the client. Note that, since the output stream (see out below) is buffered, it is legal to set HTTP status codes and response headers, even though this is not permitted in regular servlets once any output has been sent to the client.

The response to the request. Has a page scope.

The out object.

An object of type javax.servlet.jsp.JspWriter.

This is the PrintWriter used to send output to the client. However, in order to make the response object (see the previous section) useful, this is a buffered version of PrintWriter called JspWriter. Note that you can adjust the buffer size, or even turn buffering off, through use of the buffer attribute of the page directive. Also note that out is used almost exclusively in scriptlets, since JSP expressions automatically get placed in the output stream, and thus rarely need to refer to out explicitly.

An object that writes into the output stream. Has a page scope.


		<% out.println("Hello !"); %>


NOTE, JSP page authors are prohibited from writing directly to either the PrintWriter or OutputStream associated with the ServletResponse. The following example is INVALID:


		<% response.getWriter().println("Hello !"); %>



The session object.

An object of type javax.servlet.http.HttpSession.

This is the HttpSession object associated with the request. Sessions are created automatically, so this variable is bound even if there was no incoming session reference. The one exception is if you use the session attribute of the page directive to turn sessions off, in which case attempts to reference the session variable cause errors at the time the JSP page is translated into a servlet.

The session object created for the requesting client (if any). This variable is only valid for HTTP protocols. Has a session scope.

The config object.

An object of type javax.servlet.ServletConfig.

The ServletConfig for this JSP page. Has a page scope.

The application object.

An object of type javax.servlet.ServletContext.

The servlet context obtained from the servlet configuration object (as in the call this.getServletConfig().getContext()). Has an application scope.


	javax.servlet.RequestDispatcher rd;
	rd = application.getRequestDispatcher("/NextPage.jsp");
	rd.forward(request, response);



The page object.

An object of type java.lang.Object.

This is simply a synonym for this, and is not very useful in Java. It was created as a placeholder for the time when the scripting language could be something other than Java.

The instance of this page's implementation class processing the current request. Has a page scope.

The pageContext object.

A PageContext is an object that provides a context to store references to objects used by the page, encapsulates implementation-dependent features, and provides convenience methods. A JSP page implementation class can use a PageContext to run unmodified in any compliant JSP container while taking advantage of implementation-specific improvements like high performance JspWriters.

public abstract class PageContext extends JspContext {

	public abstract java.lang.Exception getException();
	public abstract java.lang.Object getPage(); // instance of Servlet
	public abstract javax.servlet.ServletRequest getRequest();
	public abstract javax.servlet.ServletResponse getResponse();
	public abstract javax.servlet.ServletConfig getServletConfig();
	public abstract javax.servlet.ServletContext getServletContext();
	public abstract javax.servlet.http.HttpSession getSession();
	public abstract void handlePageException(java.lang.Exception e);
	public abstract void include(java.lang.String relativeUrlPath);
	public abstract void forward(java.lang.String relativeUrlPath);


PageContext extends JspContext to provide useful context information for when JSP technology is used in a Servlet environment. A PageContext instance provides access to all the namespaces associated with a JSP page, provides access to several page attributes, as well as a layer above the implementation details. Implicit objects are added to the pageContext automatically. The PageContext class is an abstract class, designed to be extended to provide implementation dependent implementations thereof, by conformant JSP engine runtime environments. The following methods provide convenient access to implicit objects: getException(), getPage(), getRequest(), getResponse(), getSession(), getServletConfig() and getServletContext(). The following methods provide support for forwarding, inclusion and error handling: forward(), include(), and handlePageException().

The exception object.

An object of type java.lang.Throwable.

The uncaught Throwable that resulted in the error page being invoked. Has a page scope.

A JSP is considered an Error Page if it sets the page directive's isErrorPage attribute to true. If a page has isErrorPage set to true, then the exception implicit scripting language variable of that page is initialized.


<%@ page isErrorPage="true" %>
<%= exception.getMessage() %> <br>

With the following stack trace: <br>

	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	exception.printStackTrace(new PrintStream(baos));


Professional hosting     Belorussian informational portal         Free SCBCD 1.3 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     Free SCJP 5.0 (Tiger) Study Guide     Free Mock Exam Engine     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide