You are on page 1of 29

 The Servlets Technology

Model
Web Application
 An application that is accessible
from the web.
 Web application is built of web
components that perform specific
task and are able to expose their
functionality on web server.
Web Application and web
server
 A web application reside in web application
server.
 Also provides low level services, such as HTTP
protocol implementation and database
connection management.
HTTP protocol
 It is request –response based stateless
protocol.
 Client sends an HTTP request for a resource
and server returns an HTTP response with the
desired resource.
 It is stateless because once server sends the
response it forgets about client.
Types of Request

 HTTP GET
protected void doGet(HttpServletRequest
req, HttpServletResponse res) throws
ServletException, java.io.IOException { ... }
 Called by the server to handle a GET
request.
 An 240 characters can be sent to the
server with such a request.
.
Types Of HTTP Request
 HTTP POST
 protected void doPost(HttpServletRequest req,

HttpServletResponse res) throws


ServletException, java.io.IOException { ... } Called
by the server to handle a POST request.
 An unlimited amount of information can be
sent to the server with such a request.
Types of HTTP Request
 HTTP PUT
 protected void doPut(HttpServletRequest req,

HttpServletResponse res) throws


ServletException, java.io.IOException { ... }
 Called by the server to process a PUT request.
It is used to post a file to the web server
Types of HTTP Request
 HTTP HEAD
 A browser calls a HEAD request when it wants to
examine the headers for a particular resource, such as
"Content-Type" or "Content-Length".
 This feature works with the browser's caching ability t
reduce downloads.
 HTTP servlets that support GET requests can impleme
the getLastModified method to support this feature
(otherwise the servlet will always appear modified and
its content body will always be returned).
Types of HTTP Request
HTTP Delete
protected void doDelete(HttpServletRequest req,
HttpServletResponse resp) throws ServletException,
java.io.IOException
 Called by the server (via the service method) to

allow a servlets to handle a DELETE request. The


DELETE operation allows a client to remove a
document or Web page from the server
Structure of HTTP
Response
 An HTTP message sent by server to client is
called HTTP Response.
 Has three parts
 HTTP version

 Response status code

 English phrase describing code


 What is Servlets?
 Servlets are java program that run on Web

or application servers, acting as middle


layer between request coming from Web
browsers or other HTTP clients and
databases or application on HTTP server.
Task of Servlets
 Read the explicit data sent by client.
 Read the implicit HTTP request sent by
browser.
 Generate the Result.
 Send the explicit data.

Send the implicit HTTP response data.
Why Web Pages
Dynamically?
 Web page is based on data sent by client.
 Web page is derived from data that changes
frequently.
Advantage of servlets over
CGI
Servlets CGI

Handles it by Thread. New process started for


each process.
Automatically parsing and You have to do yourself.
decoding.
Can communicate directly Requires server based
with web server. API.
Portable Not portable

Secure Not more secure


The javax.servlet Package
 Hierarchy of classes that are used to create a
servlet.

Class java.lang.Object Interface


javax.servlet.Servlet

Class Interface
javax.servlet.GenericServlet javax.servlet.ServletConfig

Interface
Class javax.io.Serializable
javax.servlet.HttpServlet
The javax.servlet Package (Contd.)
 A brief description of the classes and interfaces are as

follows:
Class/Interface Description
Nameclass
HTTPServlet Provides a HTTP specific
implementation of the
Servlet interface.

HTTPServletRequest Provides methods to


interface process requests from the
clients.
Class/Interface Name Description

HTTPServletResponse Response to the client is


interface sent in the form of a HTML
page through an object of
the HTTPServletResponse
ServletConfig class class.
Used to store the servlets
startup configuration values
and the initialization
parameters.
Life Cycle of Servlets

init()

Request

Client service()
(Browser)
Response

destroy()
Life Cycle of Servlets
 init method-The init method is called on a
servlet instance right after it has been
initialized
 The method can be overridden in a subclass to
provide one-time initialization of the servlet.
 If for some reason this initialization fails, you
should throw an UnavailableException.
 Without init method servlet will not brought
int service.
 Called only once.
Life Cycle of Servlets
 service()-
 Called each time when server receives request
for servlets.
 Service method checks the HTTP request
type(GET,POST,PUT,DELETE etc.) and calls
doGet,doPost,doPut,doDelete as appropriate.
 You can aso use service directly instead of
doGet() and doPost().
Advantages of using
doxxx() over service.
 You can later add support for other HTTP
request method by adding doPut,doPost
perhaps in subclass.
 You can add support for modification dates by
adding getLastModified method.
 Destroy() method
 To destroy servlet instance

 To close database connection,halt background

threads,write cookies and perform other cleanup


operation.
HttpServletRequest
interface
 Used to retrieve the parameter send by
client to server.
 Methods provides by HttpServletRequest
 String getParameter(String paramName)
 String getParmeterValues(String
paramname)
 Enumeration getParameterNames()
Methods to retrieve
Request Header
 String getHeader(String headerName)
 Enumeration getHeaders(String
headername)
 Enumeration getHeaderNames()
Example:
public void service(HttpServletRequest
req,HttpServletResponse res)
{
Enumeration header=req.getHeaderNames();
while(headers.hasMoreElements())
{
String header=(String)headers.nextElement();
String value=req.getHeader(header);
System.out.println(header+” “+value)
}
}
HttpServletResponse
 Used to send information back to
clients.
 Methods in HttpServletResponse
 getWriter()
 getOutputStream()
 setContentType()
Using PrintWriter
 getWriter() method is used to return
the reference of java.io.PrintWriter .
 PrintWriter is extensively used by
servlet to generate HTML pages
dynamically.
 Example:
 Public void doGet(HttpServletRequest
req,HttpServletResponse res)
{
PrintWriter pw=res.getWriter();
pw.println();
HttpServletResponse
methods for setting
headers
 void setHeader(String name,String
value)
 void setIntHeader(String name,int
value)
 Void setDataHeader(String name,long
millisc)

You might also like