17-07-2012, 12:07 PM
Java Servlets
servlets.pdf (Size: 109.21 KB / Downloads: 209)
Servlet Life Cycle
To better understand the behavior of servlets, let’s take a look at the life cycle of servlets.
A servlet is basically a small Java program that runs within a Web server. It can receive
requests from clients and return responses. The whole life cycle of a servlet breaks up into 3
phases:
• Initialization: A servlet is first loaded and initialized usually when it is requested by the
corresponding clients. Some websites allow the users to load and initialize servlets when
the server is started up so that the first request will get responded more quickly.
• Service: After initialization, the servlets serve clients on request, implementing the application
logic of the web application they belong to.
• Destruction: When all pending requests are processed and the servlets have been idle
for a specific amount of time, they may be destroyed by the server and release all the
resources they occupy.
Servlet Context
All servlets belong to one servlet context. The javax.servlet.ServletContext interface in
the Java Servlet API is responsible for the state of its servlets and knows about resources and attributes
available to the servlets in the context. Here we will only look at how ServletContext
attributes can be used to share information among a group of servlets.
There are three ServletContext methods dealing with context attributes: getAttribute,
setAttribute and removeAttribute. In addition the servlet engine may provide ways to
configure a servlet context with initial attribute values. This serves as a welcome addition to
the servlet initialization arguments for configuration information used by a group of servlets,
for instance a database identifier , a style sheet URL for an application, the name of a mail
server, etc.
Handling Cookies
Cookies are small bits of textual information that a Web server sends to a browser and that
the browser returns unchanged when visiting the same Web site or domain later. By having
the server read information it sent the client previously, the site can provide visitors with a
number of conveniences:
• Identifying a user during an e-commerce session. Many on-line stores use a ”shopping
cart” metaphor in which the user selects an item, adds it to his shopping cart, then
continues shopping. Since the HTTP connection is closed after each page is sent, when
the user selects a new item for his cart, how does the store know that he is the same user
that put the previous item in his cart? Cookies are a good way of accomplishing this. In
fact, this is so useful that servlets have an API specifically for this, and servlet authors
don’t need to manipulate cookies directly to make use of it. This is discussed later in
Session Tracking
An HttpSession class was introduced in the Servlet API. Instances of this class can hold
information for one user session between requests. You start a new session by requesting an
HttpSession object from the HttpServletRequest in your doGet or doPost method:
HttpSession session = request.getSession(true);
This method takes a boolean argument. true means a new session shall be started if none
exist, while false only returns an existing session. The HttpSession object is unique for
one user session. The Servlet API supports two ways to associate multiple requests with a
session: cookies and URL rewriting. If cookies are used, a cookie with a unique session ID is
sent to the client when the session is established.