Cse-Vii-Java and J2ee U6
Cse-Vii-Java and J2ee U6
com
Background
Definition : Servlets are modules of Java code that run in a server application (hence
the name "Servlets", similar to "Applets" on the client side) to answer client requests.
• Servlets are not tied to a specific client-server protocol but they are most commonly
used with HTTP and the word "Servlet" is often used in the meaning of "HTTP
Servlet".
• Servlets make use of the Java standard extension classes in the packages
javax. servlet (the basic Servlet framework) and javax. servlet .http
o Providing dynamic content, e.g. returning the results of a database query to the
client.
o Managing state information on top of the stateless HTTP, e.g. for an online
shopping cart system which manages shopping carts for many concurrent
customers and maps every request to the right customer.
The life cycle of a servlet is controlled by the container in which the servlet has been
deployed. When a request is mapped to a servlet, the container performs the following
steps:
c. Initializes the servlet instance by calling the init method. Initialization is covered
in Initializing a Servlet
3. If the container needs to remove the servlet, it finalizes the servlet by calling the
servlet’s destroy method.
HttpServletResponse res)
out.close();
• Two packages contain the classes and interfaces that are required to build servlets.
They are javax.servlet and javax.servlet.http.
• The javax.servlet and javax.servlet.http packages provide interfaces and classes for
writing servlets. All servlets must implement the Servlet interface, which defines
life cycle methods.
• The javax.servlet package contains a number of classes and interfaces that describe
and define the contracts between a servlet class and the runtime environment
provided for an instance of such a class by a conforming servlet container.
• The two classes in the servlet API that implement the Servlet interface are
GeneriISErvlet and HttpServlet .
• For most purposes, developers will extend HttpServlet to implement their servlets
while implementing web applications employing the HTTP protocol.
• The basic Servlet interface defines a service method for handling client requests. This
method is called for each request that the servlet container routes to an instance of
a servlet.
• Servlets can be used for handling both the GET Requests and the POST Requests.
• The HttpServlet class is used for handling HTTP GET Requests as it has som
specialized methods that can efficiently handle the HTTP requests. These methods
are;
doGet()
doPost()
doPut()
An individual developing servlet for handling HTTP Requests needs to override one
of these methods in order to process the request and generate a response. The servlet is
invoked dynamically when an end-user submits a form.
Example:
Here’s the code for ColServlet.java that overrides the doGet() method to retrieve data
from the HTTP Request and it then generates a response as well.
// import the java packages that are needed for the servlet to work
import javax.servlet. *;
import javax.servlet.http. *;
// defining a class
info .println(col);
info.close();
2. 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 like:
• Customizing a site.
• Focusing advertising.
To send cookies to the client, a servlet would create one or more cookies with the
appropriate names and values via new Cookie (name, value)
• The cookie is added to the Set-Cookie response header by means of the addCookie
method of HttpServletResponse. For example:
• To send cookies to the client, you created a Cookie then used addCookie to send a
Set-Cookie HTTP response header.
• To read the cookies that come back from the client, call getCookies on the
HttpServletRequest. This returns an array of Cookie objects corresponding to the
values that came in on the Cookie HTTP request header
• Once this array is obtained, loop down it, calling getName on each Cookie until
find one matching the name you have in mind. You then call getValue on the
matching Cookie, doing some processing specific to the resultant value.
String defaultValue) {
if (cookieName.equals(cookie.getName()))
return(cookie.getValue());
return(defaultValue);
• Session Tracking
Cookies.
• HTTP cookies can be used to store information about a shopping session, and
• each subsequent connection can look up the current session and then extract
• information about that session from some location on the server machine.
URL Rewriting.
• You can append some extra data on the end of each URL that identifies the session,
and the server can associate that session identifier with data it has stored about that
session.
• This is also an excellent solution, and even has the advantage that it works with
browsers that don't support cookies or where the user has disabled cookies.
• However, it has most of the same problems as cookies, namely that the server-side
program has a lot of straightforward but tedious processing to do. In addition, you
have to be very careful that every URL returned to the user (even via indirect
means like
• HTML forms have an entry that looks like the following: <INPUT
TYPE="HIDDEN" NAME="session" VALUE="...">.
• This means that, when the form is submitted, the specified name and value are
included in the GET or POST data.
• However, it has the major disadvantage that it only works if every page is
dynamically generated, since the whole point is that each session has a unique
identifier.
• Servlets solution :
• The servlet author doesn't need to bother with many of the details, doesn't have
to explicitly manipulate cookies or information appended to the URL, and is
automatically given a convenient place to store data that is associated with each
session.
example,
doSomethingWith(previousItems);
} else {
doSomethingElseWith(previousItems);