Section 14.6. Implicit Objects

   

14.6 Implicit Objects

There are several useful objects that are available implicitly to a JSP. Some of these will be familiar from ColdFusion, chiefly the request , session , and application objects that correspond to the ColdFusion scopes.

These consist of the following:

  • page

  • request

  • response

  • out

  • config

  • exception

  • pageContext

  • session

  • application

In the Java chapters earlier we talked about fashioning our Java code to represent things in the world. The implicit JSP objects each represent different aspects of the application in its various interactions with the client. These are defined in the following sections.

Note

Remember that servlets and JSPs are part of the Java 2 Enterprise Edition. While your container (such as ColdFusion MX or Tomcat 4) will have an appropriate run time, you will not find these classes referenced in the regular Standard Edition API we have used throughout this book; you need to look at the J2EE 1.3.1 API. Currently, this URL is http://java.sun.com/j2ee/sdk_1.3/techdocs/api/.


14.6.1 page Object

This object represents an instance of the current page. You can think of the page object as being similiar to the this reference of a regular Java object. The page object is of type javax.lang.Object .

14.6.2 request Object

The request object represents an HTTP client request. You can think of this object as being very much like the request scope that you may have used in ColdFusion. Placing variables into the request scope in ColdFusion exposes them to the entire HTTP request, including custom tags (which have their own "black box"). The JSP request object is commonly used to access variables passed from one page to another. JSP does not make a distinction between form and URL scopes the way ColdFusion does. You use the request scope in both cases in JSP. The request object is of type javax.servlet.ServletRequest .

For example, here is how you would use the request scope's getParameter() method to retrieve parameters passed from one page into another, whether they come from a URL or an HTML form:

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

Note

What's often hard for ColdFusion developers to remember at first about this is that we're used to referencing the variable without any quotation marks so that ColdFusion evaluates it, like this: <cfoutput>#URL.name# </cfoutput> . In JSP we let the getParameter() method handle the evaluation, so we need to put double quotes around the name of the parameter.


You can also retrieve all of the parameters in the request as an enumeration object:

 ...  Enumeration e = request.getParameterNames(); while (e.hasMoreElements) {     String s = e.nextElement()     // do something here } ... 

Note

All parameters must be strings and are returned as java.lang.-Strings .


Here is how to get values of request parameters using a String array:

 String [] s = request.getParameterValues("someFormField"); 

Finally, you can use the request object to gather information from the HTTP headers in the request. These include the content type, content length, character encoding, security, and so forth. Here are some methods you will perhaps find useful:

  • getHeader(String name) returns the specified header as a string.

  • getPathInfo() returns extra path information associated with the URL.

  • getQueryString() returns the query string contained in the path.

  • getSession() returns the HttpSession object associated with this client.

Here is an example of using the request object to see how these work.

14.6.3 objects.jsp

 <%@page contentType="text/html"%>  <html> <head><title>request demo</title></head> <body> <html> Your browser: <%= request.getHeader("User-Agent") %> <br> Remote user: <%= request.getRemoteUser() %> <br> Remote address: <%= request.getRemoteAddr() %> <br> Remote host: <%= request.getRemoteHost() %> <br> Request URI: <%= request.getRequestURI() %> <br> Request protocol: <%= request.getProtocol() %> <br> Servlet path: <%= request.getServletPath() %> <br> Path info: <%= request.getPathInfo() %> <br> Path translated: <%= request.getPathTranslated() %> <br> Query string: <%= request.getQueryString() %> <br> Server name: <%= request.getServerName() %> </body> </html> 

The result of running this page should be an output of information roughly equivalent to the CGI variables you're used to using in ColdFusion:

 Your browser: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)  Remote user: null Remote address: 127.0.0.1 Remote host: localhost Request URI: /javaforcf/chp14/objects.jsp Request protocol: HTTP/1.1 Servlet path: /chp14/objects.jsp Path info: null Path translated: null Query string: name=eben Server name: localhost 

14.6.4 response Object

This object, familiar to ASP developers but less familiar to ColdFusion developers, represents the server's generated HTTP response. JSP uses an instance of the JspWriter class (the out object) to allow developers direct access to write out to the response.

The response is an object of type javax.servlet.ServletResponse . The response object is primarily useful when writing to the client is necessary inside, for example, a custom tag, or when you want to set information into the HTTP headers. For example:

 setHeader(String name, Object value); 

and

 addCookie(Cookie cookie); 

are sometimes useful.

14.6.5 out Object

The out object represents the character output stream for this page. The out object is of type javax.servlet.jsp.JspWriter . Its function is essentially to allow you to print out to the browser in a convenient manner. Note that this requires a scriptlet call, which is generally something you want to avoid when possible:

 <% out.println("<h1>Hello, " + name + "!") %> 

14.6.6 config Object

The config object is of type javax.servlet.ServletConfig . It allows you to gain information regarding the servlet underlying the JSP, such as the name of the servlet instance and any initialization parameters used.

14.6.7 exception Object

The exception object, as we saw in the last chapter, is only available on a specifically declared error page. An error page, like an error page in ColdFusion, is a custom error page to be sent to the browser instead of that used by default by the container. Each custom error page is associated with a certain HTTP status response in the container's configuration (such as the web.xml file) and can access this object. The exception object is of type javax.lang.Throwable . It defines a getMessage() method, which is generally used on such pages to print the error message. The printStackTrace() method outputs the stack trace. ColdFusion's custom error pages offer similiar functionality.

14.6.8 pageContext Object

This implicit object contains a reference to the javax.servlet.jsp.PageContext object for the JSP currently being processed . It affords the developer a way to explicitly gain references to some of the following objects:

  • getOut() is of the type JspWriter .

  • getPage() is of the type Object .

  • getRequest() is of the type ServletRequest .

  • getResponse() is of the type ServletResponse .

  • getServletConfig() is of the type ServletConfig .

  • getServletContext() is of the type ServletContext .

14.6.9 session Object

The session object, like its ColdFusion counterpart , maintains client-specific information. It is an object of type javax.servlet.http.HttpSession . In ColdFusion, you set variables into the session by simply prefixing the name of the session to the variable:

 <cfset session.username = FORM.user> 

In JSP, you use the setAttribute() and getAttribute() methods:

 <% session.setAttribute("username", request.getParameter("user") %> 

Recall from the last chapter that ColdFusion MX allows you to share variables between its Application and Session scopes and that of JSP.

14.6.10 application Object

The application object is of type javax.servlet.ServletContext . You can use the JSP application object to gain information regarding the servlet container. Especially useful are the logging methods. You can also set values into this scope that you want to be shared across an entire application. This object is not generally explicitly used as often as its ColdFusion counterpart, though developers will often set utility JavaBeans into the application scope.


   
Top


Java for ColdFusion Developers
Java for ColdFusion Developers
ISBN: 0130461806
EAN: 2147483647
Year: 2005
Pages: 206
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net