Post on 05-Apr-2018
7/31/2019 Java Serv Lets
1/26
JavaJava ServletsServlets
Key points to be rememberedKey points to be remembered ServletsServlets areare server side Java Applicationsserver side Java Applications
that can be loaded dynamicallythat can be loaded dynamically
Java Applets are used to create Client sideJava Applets are used to create Client side
ApplicationsApplications
7/31/2019 Java Serv Lets
2/26
JavaJava ServletsServlets
PrehistoryPrehistory In general, web server has to handle largeIn general, web server has to handle large
amount of requests concurrently & thus it isamount of requests concurrently & thus it is
not able to handle all transactions that keepnot able to handle all transactions that keep
coming incoming in
For this purpose, some applications haveFor this purpose, some applications have
been designed to extend the functionality ofbeen designed to extend the functionality ofweb serversweb servers
7/31/2019 Java Serv Lets
3/26
JavaJava ServletsServlets
PrehistoryPrehistory Example:Example: Server Side ApplicationsServer Side Applications
CGICGI
ServletsServlets ASPASP
7/31/2019 Java Serv Lets
4/26
JavaJava ServletsServlets
PrehistoryPrehistory
Server
Call for CGI A
Call for CGI B
Call for CGI A
Process for CGI A
Process for CGI B
Process for CGI A
CGI Based Server
7/31/2019 Java Serv Lets
5/26
JavaJava ServletsServlets
PrehistoryPrehistory In traditional CGI,In traditional CGI, For each request web serverFor each request web server
calls for separate process for CGI scriptcalls for separate process for CGI scripthandling.handling.
At this instant, server passes all the data thatAt this instant, server passes all the data thatthe CGI program need to generate thethe CGI program need to generate theresponse with the help of environmentresponse with the help of environment
variablevariable
Limits the number of requestsLimits the number of requests takes moretakes moretime for executiontime for execution & extra resources& extra resources
7/31/2019 Java Serv Lets
6/26
JavaJava ServletsServlets
PrehistoryPrehistory To overcome this issue,To overcome this issue, FastCGIFastCGI is developedis developed
which creates the single persistent object forwhich creates the single persistent object for
each CGI programeach CGI program
7/31/2019 Java Serv Lets
7/26
JavaJava ServletsServlets
PrehistoryPrehistory
Server
Call for CGI A
Call for CGI B
Call for CGI A
Process for CGI A
Process for CGI B
Process for CGI A
FAST CGI Based Server
7/31/2019 Java Serv Lets
8/26
JavaJava ServletsServlets
PrehistoryPrehistory FastCGIFastCGI also creates overhead when aalso creates overhead when a
security crash occurssecurity crash occurs
7/31/2019 Java Serv Lets
9/26
JavaJava ServletsServlets
PrehistoryPrehistory JavaJava ServletsServlets are Java Programs that can beare Java Programs that can be
loaded dynamically & executed on theloaded dynamically & executed on thememory of servermemory of server
ServletServlet extends the functionality of serverextends the functionality of server andandruns inside the JVMruns inside the JVM
CGI andCGI and FastCGIFastCGI run multiple processes torun multiple processes tohandle different requests where ashandle different requests where as JavaJavaServletServlet handles each request usinghandles each request usingMultithreadingMultithreading CapabilityCapability
7/31/2019 Java Serv Lets
10/26
JavaJava ServletsServlets
PrehistoryPrehistory
Call for Servlet A
Call for Servlet B
Call for Servlet A
Servlet A
Servlet B
Servlet Based Server
ServerJVM
7/31/2019 Java Serv Lets
11/26
JavaJava ServletsServlets
MertitsMertits ServletServlet becomes more efficient & scalablebecomes more efficient & scalable
Portable across all OS and web serversPortable across all OS and web servers
7/31/2019 Java Serv Lets
12/26
JavaJava ServletsServlets
ServletsServlets APIAPI It isIt is not a part of JDKnot a part of JDK
Have toHave to install JSDKinstall JSDK (Java(Java ServletServletDevelopment Kit)Development Kit)
Need 2 packagesNeed 2 packages
Javax.servletJavax.servlet (used to create protocol(used to create protocol
independentindependent servletservlet))
Javax.servlet.httpJavax.servlet.http (used to create HTTP(used to create HTTP
servletservlet))
7/31/2019 Java Serv Lets
13/26
JavaJava ServletsServlets
ServletsServlets APIAPI AllAll servletservlet must implementmust implement ServletServlet InterfaceInterface
Javax.servletJavax.servlet must extendmust extendJavax.servlet.GenericServletJavax.servlet.GenericServlet
Javax.servlet.httpJavax.servlet.http must extendmust extend
Javax.servlet.http.HttpServletJavax.servlet.http.HttpServlet wherewhereHttpServletHttpServlet is the subclass ofis the subclass of GenericServletGenericServlet
7/31/2019 Java Serv Lets
14/26
JavaJava ServletsServlets
ServletsServlets APIAPI AA servletservlet worksworks based onbased on RequestRequest--ResponseResponse
CycleCycle
A request is sent to theA request is sent to the ServletServlet by the clientby the client
thethe servletservlet process the request & generatesprocess the request & generates
the response and sends the response back tothe response and sends the response back to
the clientthe client
7/31/2019 Java Serv Lets
15/26
JavaJava ServletsServlets
ServletsServlets APIAPI With reference to the technical idea, AWith reference to the technical idea, A servletservlet
can be created in 2 wayscan be created in 2 ways
GenericServletGenericServlet handles the requesthandles the request--responseresponsecycle based oncycle based on service()service() methodmethod
This methodThis method accept 2 parametersaccept 2 parameters
ServletRequestServletRequest is a object specifies theis a object specifies theclient requestclient request
ServletResponseServletResponse is a object that specifiesis a object that specifies
the response of thethe response of the servletservlet to that requestto that request
7/31/2019 Java Serv Lets
16/26
JavaJava ServletsServlets
ServletsServlets APIAPI HttpServletHttpServlet handles the requesthandles the request--responseresponse
cycle based on eithercycle based on either doGetdoGet()() oror doPostdoPost()() oror
doServicedoService()() methodmethod
doGetdoGet() corresponds to GET method of HTTP() corresponds to GET method of HTTP
protocolprotocol
doPostdoPost() corresponds to POST method of() corresponds to POST method of
HTTP protocolHTTP protocol
7/31/2019 Java Serv Lets
17/26
JavaJava ServletsServlets
ServletsServlets APIAPI doServicedoService() method is a default method which() method is a default method which
in turn callsin turn calls doOptionsdoOptions() method &() method & doTracedoTrace()()
that causes a response with a messagethat causes a response with a message
containing all of the headers sent in the tracecontaining all of the headers sent in the tracerequestrequest
7/31/2019 Java Serv Lets
18/26
JavaJava ServletsServlets
ServletsServlets APIAPI HttpServletHttpServlet handles the requesthandles the request--responseresponse
cycle based on eithercycle based on either doGetdoGet()() oror doPostdoPost()()
methodmethod
This methodThis method accept 2 parametersaccept 2 parameters
HttpServletRequestHttpServletRequest is a object specifiesis a object specifies
the client requestthe client request HttpServletResponseHttpServletResponse is a object thatis a object that
specifies the response of thespecifies the response of the servletservlet to thatto that
requestrequest
7/31/2019 Java Serv Lets
19/26
JavaJava ServletsServlets
ServletServlet Life CycleLife Cycle AA servletservlet is a Server based Java Applicationis a Server based Java Application
program that runs within a Web server.program that runs within a Web server.
It can receive requests from clients and returnIt can receive requests from clients and returnresponses.responses.
The wholeThe whole life cycle of alife cycle of a servletservlet breaks up intobreaks up into3 phases3 phases::
InitializationInitialization
ServiceService
DestructionDestruction
7/31/2019 Java Serv Lets
20/26
7/31/2019 Java Serv Lets
21/26
JavaJava ServletsServlets
ServletServlet
Life CycleLife Cycle
InitializationInitialization A servlet is first loaded and initialized
usually when it is requested by thecorresponding clients.
Loading of servlet will be taken care byServlet Container
After creating the instances, the servletcontainer calls the init() method and passesthe servlet initialization parameters to theinit() method.
7/31/2019 Java Serv Lets
22/26
JavaJava ServletsServlets
ServletServlet
Life CycleLife Cycle
InitializationInitialization The init() must be called by the servlet
container before the servlet can service anyrequest.
The initialization parameters persist untillthe servlet is destroyed.
The init() method is called only oncethroughout the life cycle of the servlet.
7/31/2019 Java Serv Lets
23/26
JavaJava ServletsServlets
ServletServlet Life CycleLife Cycle
ServiceService After initialization, the servlets serve clients
on request, implementing the applicationlogic of the web application they belong to.
i.e., After successfully completing theinitialization process, the servlet will beavailable for service.
Servlet creates seperate threads for eachrequest. The servlet container calls theservice() method for servicing any request.
7/31/2019 Java Serv Lets
24/26
JavaJava ServletsServlets
ServletServlet Life CycleLife Cycle
ServiceService The service() method determines the kind of
request and calls the appropriate method(doGet() or doPost()) for handling therequest and sends response to the clientusing the methods of the response object
7/31/2019 Java Serv Lets
25/26
JavaJava ServletsServlets
ServletServlet Life CycleLife Cycle
DestructionDestruction If the servlet is no longer needed for
servicing any request, the servlet containercalls the destroy() method .
Like the init() method this method is alsocalled only once throughout the life cycle ofthe servlet.
Calling the destroy() method indicates to theservlet container not to sent the any requestfor service and the servlet releases all the
resources associated with it.
7/31/2019 Java Serv Lets
26/26
JavaJava ServletsServlets
ServletServlet Life CycleLife Cycle
DestructionDestruction At this instant, Java Virtual Machine claims
for the memory associated with theresources for garbage collection