Implicit objects are a set of Java objects that the JSP Container makes available to developers in each page. These objects may be accessed as built-in variables via scripting elements and can also be accessed programmatically by JavaBeans and Servlets.
The response object handles the output to the client. This object can be used for creating HTTP Headers, creating cookies, setting content type and redirecting workflow.
javax.servlet.http.HttpServletResponse
The following table summarises the most useful methods available to the
response object. For more details consult the Servlet
API.
| Method | Description |
|---|---|
setContentType() |
Sets the MIME type and character encoding for the page. |
addCookie(Cookie) |
Adds the specified cookie to the response. It can be called multiple times to set more than one cookie. |
containsHeader(name) |
Checks whether the response already includes this header.
|
setHeader(name, value) |
Creates an HTTP Header. |
sendRedirect(String) |
Sends a temporary redirect response to the client using the specified redirect location URL. |
The method sendRedirect() is useful for controlling the workflow of a web application. This is a very important and often used method and so lets look a bit more closely at how this works.
The sendRedirect() method works not by redirecting the workflow on the server but by instructing the client browser to make a request for the page defined in the method. Lets walk through an example to see more closely how this works.
Consider the example of a user logging in to a web application. First the user is displayed an page containing an HTML form in which the user can enter a password. When the form is submmited the browser makes a request to the JSP defined in the action attribute of the form and attaches the password to the request. This JSP can pick up the password using request.getParameter() and then checks to see if the password is valid. If the password is not valid then the JSP makes a call to the sendRedirect() method providing the URL of the login page. As this page executes the JVM executes the sendRedirect() method. When it does this it stops the execution of the page, attaches an HTTP header which instructs the client to redirect to the login page and serves the result to the client. The client process the HTTP headers and without attempting to display any HTML it automatically makes a request to the server for the login page. The following sequence diagram summarises this workflow.
Sequence diagram for the response.sendRedirect() method
When a browser encounters an HTTP header instructing it to immediately request another page. It leaves the whatever page is currently being displayed on screen and executes this second request in the background so to the user the whole process looks quite seemless.
There are two important points to be aware of when using the sendRedirect()
method. First of all is that the method will fail if the executing JSP has already sent some
page content to the browser already. When a web server sends a page to the client it does so
in chunks rather than one large file. So if one or more of these chunks have been sent to the
browser then a call to sendRedirect() will fail. To avoid this scenario you can
either increase the size of the chunks by using page buffer directive or preferrably structure
your JSP such that the call to sendRedirect() is placed before the opening
<HTML> tag.
Another point is about HTTP Headers. Although no page content is displayed when the
sendRedirect() method is executed HTTP Headers are processed. This means
that it is possible to write code that will create a cookie and then perform a redirect.
This makes sense. The code that creates a cookie doesn't actually create the cookie it
just adds an instruction to the HTTP Header that instructs the client to create the
cookie. Likewise the sendRedirect() method doesn't actually redirect but
instead it adds an instruction to the HTTP Header that this time instructs the
client to request another JSP.
The following workshop looks at using the request and response
objects.