Java Reference
In-Depth Information
One of the more common uses for the
request
object is to access request parameters. You can
do this by calling the
request
object's
getParameter()
method, which is inherited from its
parent
javax.servlet.ServletRequest
, with the parameter name you are looking for. It will
return a string with the value matching the named parameter. An example of this can be found
in Listing 15.1.
L
ISTING
15.1
UseRequest.jsp
<%@ page errorPage=”errorpage.jsp” %>
<html>
<head>
<title>UseRequest</title>
</head>
<body>
<%
// Get the User's Name from the request
out.println(“<b>Hello: “ + request.getParameter(“user”) + “</b>”);
%>
</body>
</html>
You can see that this JSP calls the
request.getParameter()
method passing in the parameter
user
. This method looks for the key
user
in the parameter list and returns the value, if it is
found. Enter the following URL into your browser to see the results from this page:
http://
localhost
/djs/UseRequest.jsp?user=Bob
After loading this URL, you should see a screen similar to Figure 15.1.
The
response
Object
The JSP implicit object
response
represents the
javax.servlet.http.HttpServletResponse
object, which defines an object that provides the JSP with the capability to manipulate HTTP-
protocol-specific header information and return data to the client. The
response
object is
passed into the generated
_jspService()
method. You can see how it is passed in the follow-
ing code snippet:
public void _jspService(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
The most common use for the
response
object is writing HTML output back to the client
browser. You would normally call the
response.getWriter()
method, but the JSP API
abstracts you from this by providing the implicit
out
object, which will be discussed in a later
section of this chapter.