HttpServletRequest and
HttpServletResponse
Communication with client connections happens through the
HttpServletRequest and HttpServletResponse object. These are
abstractions of the literal request stream received from and sent to the client.
Each of these objects adds higher-level, HTTP-specific methods to ease working
with requests and responses.
Table 12.3 is a list of the methods within the HttpServletRequest
object. A great majority of the methods are bean property accessors, which means
that you can reference them with Jython's automatic bean properties. This
may not be as great of an advantage here as it is for GUI programming because
there is no opportunity to leverage this facility in method keyword arguments.
Table 12.3 shows the methods and bean property names from the
HttpServletRequest object.
Table 12.3 HttpServletRequest Methods and Properties
|
Method and Property
|
Description
|
|
Method: getAuthType()
Property name: AuthType
|
Returns a string (PyString) describing the name of the
authentication type. The value is None if the user is
notauthenticated.
|
|
Method: getContextPath()
Property name: contextPath
|
Returns a string (PyString) describing the path information that
identifies the context requested.
|
|
Method: getCookies()
Property name: cookies()
|
Returns all cookies sent with the client's request as an array of javax.servlet.http.Cookie
objects.
|
|
Method: getDateHeader(name)
|
Retrieves the value of the specified header as a long type.
|
|
Method: getHeader(name)
|
Returns the value of the specified header as string (PyString).
|
|
Method: getHeaderNames()
Property name: headerNames
|
Returns all the header names contained within the request as an
Enumeration.
|
|
Method: getHeaders(name)
|
Returns all the values of the specified header name as an
Enumeration.
|
|
Method: getIntHeader(name)
|
Retrieves the specified header value as a Java int, which
Jython converts to a PyInteger.
|
|
Method: getMethod()
Property name: method
|
Returns the type of request made a string.
|
|
Method: getPathInfo()
Property name: pathInfo
|
All extra path information sent by the client.
|
|
Method: getPathTranslated()
Property name: pathTranslated
|
Returns the real path derived from extra path information in the
client's request.
|
|
Method: getQueryString()
Property name: queryString
|
Returns the query string from the client's request (the string after the
path).
|
|
Method: getRemoteUser()
Property name: remoteUser
|
Returns the login name of the client. None if the client is not
authenticated.
|
|
Method: getRequestedSessionId()
Property name: requestedSessionId
|
Returns the clients session ID.
|
|
Method: getRequestURI()
Property name: requestURI
|
Returns that segment of the between the protocol name and the query
string.
|
|
Method: getServletPath()
Property name: servletPath
|
Returns the portion of the URL that designates the current
Servlet.
|
|
Method: getSession()
Property name: session
|
Returns the current session, or creates on if needed. A session is an
instance of javax.servlet.http.HttpSession.
|
|
Method: getSession(create)
|
Returns the current session if one exists. If not, a new session is
created if the create value is true.
|
|
Method: getUserPrincipal()
Property name: userPrincipal
|
Returns a java.security.Principal object with the current
authentication information userPrincipal
|
|
Method: isRequestedSessionIdFromCookie()
|
Returns 1 or 0 depending on whether the current
session ID was from a cookie.
|
|
Method: isRequestedSessionIdFromURL()
|
Returns 1 or 0 depending on whether the current
session ID was from the requested URL string.
|
|
Method: isRequestedSessionIdValid()
|
Returns 1 or 0 depending on whether the requested
session ID is still valid.
|
|
Method: isUserInRole(role)
|
Returns 1 or 0 indicating whether the user is
listed in the specified role.
|
The HttpServletResponse object is used to send the
mime-encoded stream back to the client. HttpServletResponse defines
additional HTTP-specific methods that do not exist in a generic
ServletResponse object. The methods within the
HttpServletResponse object appear in Table 12.4. Whereas Jython adds
numerous automatic bean properties to the HttpServletRequest object,
the HttpServletResponse object only has one: status.
Table 12.4 HttpServletResponse Methods and Properties
|
Method and Property
|
Description
|
|
addCookie(cookie)
|
Add a cookie to the response.
|
|
addDateHeader(headerName, date)
|
Adds a header name with a date (long) value.
|
|
addHeader(headerName, value)
|
Adds a header name and value.
|
|
addIntHeader(headerName, value)
|
Adds a header name with an integer value.
|
|
containsHeader(headerName)
|
Returns a 1 or 0 depending whether the specified
header.
|
|
encodeRedirectUrl(url)
|
Encodes a URL for the sendRedirect method. For version 2.1 and
greater, use encodeRedirectURL instead.
|
|
encodeRedirectURL(url)
|
Encodes a URL for the sendRedirect method.
|
|
encodeURL(url)
|
Encodes a URL by including the session ID in it.
|
|
sendError(sc)
|
Sends an error using the status code.
|
|
sendError(sc, msg)
|
Sends an error using the specified status code and message.
|
|
sendRedirect(location)
|
Sends a temporary redirect to the specified location.
|
|
setDateHeader(headerName, date)
|
Sets a header name to the specified date (long) value.
|
|
setHeader(headerName, value)
|
Sets a header name to the specified value.
|
|
setIntHeader(headerName, value)
|
Sets a header name to the specified integer value.
|
|
setStatus(statusCode) status
|
Sets the response status code.
|
The HttpServletResponse class also contains fields
that correspond to the standard HTTP response codes. You can use these with
sendError(int) and setStatus(int). Table 12.5 lists the number
and error code for these status codes.
Table 12.5 HttpServletResponse Status CodesError
|
Code
|
Status
|
|
100
|
SC_CONTINUE
|
|
101
|
SC_SWITCHING_PROTOCOLS
|
|
200
|
SC_OK
|
|
201
|
SC_CONTINUE
|
|
202
|
SC_ACCEPTED
|
|
203
|
SC_NON_AUTHORITATIVE_INFORMATION
|
|
204
|
SC_NO_CONTENT
|
|
205
|
SC_RESET_CONTENT
|
|
206
|
SC_PARTIAL_CONTENT
|
|
300
|
SC_MULTIPLE_CHOICES
|
|
301
|
SC_MOVED_PERMANENTLY
|
|
302
|
SC_MOVED_TEMPORARILY
|
|
303
|
SC_SEE_OTHER
|
|
304
|
SC_NOT_MODIFIED
|
|
305
|
SC_USE_PROXY
|
|
400
|
SC_BAD_REQUEST
|
|
401
|
SC_UNAUTHORIZED
|
|
402
|
SC_PAYMENT_REQUIRED
|
|
403
|
SC_FORBIDDEN
|
|
404
|
SC_NOT_FOUND
|
|
405
|
SC_METHOD_NOT_ALLOWED
|
|
406
|
SC_NOT_ACCEPTABLE
|
|
407
|
SC_PROXY_AUTHENTICATION_REQUIRED
|
|
408
|
SC_REQUEST_TIMEOUT
|
|
409
|
SC_CONFLICT
|
|
410
|
SC_GONE
|
|
411
|
SC_LENGTH_REQUIRED
|
|
412
|
SC_PRECONDITION_FAILED
|
|
413
|
SC_REQUEST_ENTITY_TOO_LARGE
|
|
414
|
SC_REQUEST_URI_TOO_LONG
|
|
415
|
SC_UNSUPPORTED_MEDIA_TYPE
|
|
500
|
SC_INTERNAL_SERVER_ERROR
|
|
501
|
SC_NOT_IMPLEMENTED
|
|
502
|
SC_BAD_GATEWAY
|
|
503
|
SC_SERVICE_UNAVAILABLE
|
|
504
|
SC_GATEWAY_TIMEOUT
|
|
505
|
SC_HTTP_VERSION_NOT_SUPPORTED
|
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.