Chapter 06 Servletppt 1
Chapter 06 Servletppt 1
t
Servlet :
Introduction
Servlet technology is used to create web
application (resides at server side and
generates dynamic web page).
Interfaces Description
Servlet Declare life cycle methods for servlet. To
implement this interface we have to extends
GenericServlet or HttpServlet classes.
<param-name>dname </param-name>
<param-value>
sun.jdbc.odbc.JdbcOdbcDriver
</param-value>
</context-param>
HttpServl
et
It extends GenericServlet class and
implements Servlet, ServletConfig and
Serializable interface.
It provides http specific methods such
as
doGet, doPost, doHead, doTrace etc.
HttpServl
et
• The most important are six doxxx methods that get
called when a related HTTP request method is
used.
• The six methods are doPost, doPut, doGet, doDelete,
doOptions and doTrace.
• For instance, the doGet method is invoked when the
servlet receives an HTTP request that was sent using
the GET method.
• Of the six doxxx methods, the doPost and the doGet
methods are the most frequently used.
Implementati
onServlet is just an ordinary Java class
A Java
Create two servlet file, one will save user name into
session and that session information is send to another
servlet. This example shows the session tracking.
RequestDispatc
her
The RequestDispatcher class enables your servlet to "call"
another servlet from inside another servlet.
We can obtain a RequestDispatcher from the
HttpServletRequest object.
object:
Example:
Persistent cookie:
It is valid for multiple session . It is not removed each time
when user closes the browser. It is removed only if
user logout or sign-out or clear cookies/cache memory of
browsers.
Cookie:
Pros/Cons
Advantages:
Simplest technique of maintaining the state.
Cookies are maintained at client side.
Disadvantages
It will not work if cookie is disabled from the browser.
Only textual information can be set in Cookie object.
Cookie:
Constructor
javax.servlet.http.Cookie class provides the
functionality of using cookies. It provides a lot of
useful methods for cookies.
Constructor Description
Cookie() constructs a cookie.
Cookie(String name, String value) constructs a cookie with a specified
name and value.
Cookie:
Methods
Useful
methods:
Method Description
public void setMaxAge(int expiry) Sets the maximum age of the cookie in
seconds.