Chapter 06 Servlet
Chapter 06 Servlet
Chapter 06 Servlet
Servlet
■ Java servlet are the programs that run on a web
application server.
■ it act as a middle layer between requests from web
browser/ client and database/application on HTTP
server
Servlet
■ Web applications are helper applications that resides at web
server and build dynamic web pages.
■ A dynamic page could be anything like a page that randomly
chooses picture to display or even a page that displays the
current time.
Servlet : Defined in many ways
■ Servlet is a technology that is used to create web
application.
■ Servlet is an API that provides many interfaces and
classes including documentations.
■ Servlet is an interface that must be implemented
for creating any servlet.
■ Servlet is a class that extend the capabilities of the
servers and respond to the incoming request. It can
respond to any type of requests.
■ Servlet is a web component that is deployed on the
server to create dynamic web page.
CGI(common gateway interface)
HTTPd stands for Hypertext Transfer Protocol daemon. It usually is the main software part of an HTTP server better
known as a web server.
Disadvantages of CGI
ilesh Vishwasrao
Servlet life cycle
Types of Servlet
■ Generic Servlet:
■ It is in javax.servlet.GenericServlet package
■ It is protocol independent.
■ HTTP Servlet:
■ It is in javax.servlet.HttpServlet package
■ Built-in HTTP protocol support.
Types of Servlet
Generic Servlet HTTP Servlet
In doGet Method the parameters are appended In doPost, parameters are sent in separate line
to the URL and sent along with header in the body tag
information
Maximum size of data that can be sent using There is no maximum size for data
doGet is 240 bytes
Parameters are not encrypted Parameters are encrypted
DoGet method generally is used to query or to DoPost is generally used to update or post
get some information from the server some information to the server
DoGet is faster if we set the response content DoPost is slower compared to doGet since
length since the same connection is used. Thus doPost does not write the content length
increasing the performance
DoGet should be idempotent. i.e. doGet should This method does not need to be idempotent.
be able to be repeated safely many times Operations requested through POST can have
side effects for which the user can be held
accountable, for example, updating stored data
or buying items online.
DoGet should be safe without any side effects This method does not need to be either safe
for which user is held responsible
Servlet API
■ Servlet API consists of two important packages
that encapsulates all the important classes and
interface, namely :
■ javax.servlet
■ javax.servlet.http
Servlet Interface: javax.servlet
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>
ServletContext interface
ServletConfig Vs
Config ServletContextContext
One object per servlet Object is global to entire web
application
Object is created when Object is created when web
servlet class is loaded application deployed
It destroy when servlet is It will destroyed when web
destroyed or upload the class. application is un-deployed or
removed.
Config object is public to It can share information
particular servlet only. between the servlet
HttpServlet
■ 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.
RequestDispatcher
■ The RequestDispatcher class enables your servlet to "call"
another servlet from inside another servlet.
■ We can obtain a RequestDispatcher from the
HttpServletRequest object.
<servlet-mapping>
<servlet-name>MyServlet1</servlet-name>
<url-pattern>/sample/MyServlet1</url-pattern>
</servlet-mapping>
■ Non-persistent cookie:
■ It is valid for single session only. It is removed each time
when user closes the browser.
■ 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
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.
■ Other methods:
■ public void addCookie(Cookie ck):method of
HttpServletResponse interface is used to add cookie in
response object.
■ public Cookie[] getCookies():method of
HttpServletRequest interface is used to return all the
cookies from the browser.
Cookie: How to create?
Cookie ck[]=request.getCookies();
for(int i=0;i<ck.length;i++)
{
out.print("<br>"+ck[i].getName()+" "+ck[i].getValue());
//printing name and value of cookie
}
Cookie: Example
Cookie: Example
■ Create one Html file which send user name to
first servlet.
■ First servlet file set cookies of that user name and
call second servlet file.
■ Second servlet file retrieve name of user from
cookies instead of from session.
Session Cookies
Sessions are server-side files that store the Cookies are client-side files that contain user
user information information on a local computer.
A session stores the variables and their values Cookies are stored on the user's computer as a
within a file in a temporary directory on the text file.
server.
The session ends when the user logout from Cookies end on the lifetime set by the user.
the application or closes his web browser.
It can store an unlimited amount of data. It can store only limited data.
We can store as much data as we want within The maximum size of the browser's cookies is
a session, but there is a maximum memory 4 KB.
limit, which a script can use at one time, and
it is 128 MB.
Sessions are more secured compared to Cookies are not secure, as data is stored in a
cookies, as they save data in encrypted form. text file, and if any unauthorized user gets
access to our system, he can temper the data.