Chapter 06 Servlet
Chapter 06 Servlet
Servl
et
■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
Servl
■ Webet
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)
•CGI technology enables the web server to call an
external program and pass HTTP request information
to the external program to process the request.
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
ServletContext
Context
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
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.
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.
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.
<servlet-mapping>
<servlet-name>MyServlet1</servlet-name>
<url-pattern>/sample/MyServlet1</url-pattern>
</servlet-mapping>
■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.
A session stores the variables and Cookies are stored on the user's
their values within a file in a computer as a text file.
temporary directory on the server.
The session ends when the user Cookies end on the lifetime set by
logout from the application or closes the user.
his web browser.
It can store an unlimited amount of It can store only limited data.
data.
We can store as much data as we The maximum size of the browser's
want within a session, but there is a cookies is 4 KB.
maximum memory limit, which a
script can use at one time, and it is
128 MB.