The
javax.servlet.http Package
The preceding examples have
used the classes and interfaces defined in javax.servlet,
such as ServletRequest, ServletResponse, and GenericServlet, to illustrate the basic
functionality of servlets. However, when working with HTTP, you will normally
use the interfaces and classes in javax.servlet.http.
As you will see, its functionality makes it easy to build servlets that work
with HTTP requests and responses.
The following table
summarizes the interfaces used in this chapter:
Interface
: Description
HttpServletRequest : Enables servlets to read
data from an HTTP request.
HttpServletResponse : Enables servlets to write
data to an HTTP response.
HttpSession : Allows session data to be read
and written.
The following table
summarizes the classes used in this chapter. The most important of these is HttpServlet. Servlet developers
typically extend this class in order to process HTTP requests.
Class :
Description
Cookie : Allows state information to be stored
on a client machine.
HttpServlet : Provides methods to handle HTTP
requests and responses.
The
HttpServletRequest Interface
The HttpServletRequest interface enables a servlet to obtain
information about a client request. Several of its methods are shown in Table
38-5.
The
HttpServletResponse Interface
The HttpServletResponse interface enables a servlet to formulate an
HTTP response to a client. Several constants are defined. These correspond to
the different status codes that can be assigned to an HTTP response. For
example, SC_OK indicates that the
HTTP
request succeeded, and SC_NOT_FOUND indicates that the
requested resource is not available. Several methods of this interface are
summarized in Table 38-6.
The
HttpSession Interface
The HttpSession interface enables a servlet to read and write the state
information that is associated with an HTTP session. Several of its methods are
summarized in Table 38-7. All of these methods throw an IllegalStateException if the session has already been invalidated.
The
Cookie Class
The Cookie class encapsulates a cookie. A cookie is stored on a client and contains state information. Cookies
are valuable for tracking user activities. For example, assume that a user
visits an online store. A cookie can save the user’s name, address, and other
information. The user does not need to enter this data each time he or she
visits the store.
A servlet can write a cookie
to a user’s machine via the addCookie( )
method of the HttpServletResponse interface.
The data for that cookie is then included in the header of the HTTP response that is sent to the browser.
The names and values of
cookies are stored on the user’s machine. Some of the information that can be
saved for each cookie includes the following:
The name of the cookie
The value of the cookie
The expiration date of the cookie
The domain and path of the cookie
The expiration date
determines when this cookie is deleted from the user’s machine. If an
expiration date is not explicitly assigned to a cookie, it is deleted when the
current browser session ends.
The domain and path of the
cookie determine when it is included in the header of an HTTP request. If the
user enters a URL whose domain and path match these values, the cookie is then
supplied to the web server. Otherwise, it is not.
There is one constructor for Cookie. It has the signature shown
here: Cookie(String name, String value)
Here, the name and value of
the cookie are supplied as arguments to the constructor. The methods of the Cookie class are summarized in Table
38-8.
The
HttpServlet Class
The HttpServlet class extends GenericServlet.
It is commonly used when developing servlets that receive and process HTTP
requests. The methods defined by the HttpServlet
class are summarized in Table 38-9.
Method :
Description
Object clone( ) : Returns a copy of this
object.
String getComment( ) : Returns the comment.
String getDomain( ) : Returns the domain.
int getMaxAge( ) : Returns the maximum age (in
seconds).
String getName( ) : Returns the name.
String getPath( ) : Returns the path.
boolean getSecure( ) : Returns true if the
cookie is secure. Otherwise, returns false.
String getValue( ) : Returns the value.
int getVersion( ) : Returns the version.
boolean isHttpOnly(?) : Returns true if the
cookie has the HttpOnly attribute.
void setComment(String c) : Sets the comment to
c.
void setDomain(String d) : Sets the domain to
d.
void setHttpOnly(boolean httpOnly) : If
httpOnly is true, then the HttpOnly attribute is added to the cookie. If
httpOnly is false, the HttpOnly attribute is removed.
void setMaxAge(int secs) : Sets the maximum age
of the cookie to secs. This is the number of seconds after which the cookie is
deleted.
void setPath(String p) : Sets the path to p.
void setSecure(boolean secure) : Sets the
security flag to secure.
void setValue(String v) : Sets the value to v.
void setVersion(int v) : Sets the version to v.
Table
38-8 The Methods Defined by Cookie
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.