SlideShare a Scribd company logo
UNDERSTANDING JAVA
SERVLET
by Tanmoy Barman
Discussions
 Client-Server Architecture.
 HTTP protocol.
 Introduction to Servlet.
 J2ee container and architecture.
 Servlet Life Cycle.
 Functions of servlet container.
 Servlet API.
 Example of servlet.
 Session Management Techniques.
 Servlet Collaboration.
 Comparing Servlet with CGI.
Client server architecture
Client server architecture
 Server: typically a application for large
computers which can handle multiple request
simultaneously, can process much faster. The
application of server is written in such a way
that it cannot be access by the user directly
but it waits for another program to connect and
then it process the request as need by the
user.
Client server architecture
 Client: an application that runs on simple
desktop computers. It has an attractive user
interface but itself does not have the data to
manipulate instead it takes input from the
user, connects the server and the server
responds to the request as need by the client
using this application.
HTTP protocol
 HTTP(Hyper Text Transfer Protocol ) is a
communication protocol for displaying HTML
pages inside web browser.
 HTTP are stateless protocol because it treats
each request independently and have no
knowledge of where the request have came
from.
 A stateless protocol treats each request and
response pair as an independent transaction.
 Default port no of HTTP is 80.
HTTP protocol
 The communication between the web browser
and the server are usually done using two
methods:-
1. GET.
2. POST.
 GET method is used to request data from a
specified resource.
 POST method is used to submit data from a
specified resource.
HTTP protocol
 GET methods are cached but POST method
cannot be cached.
 GET methods can be bookmarked but POST
methods cannot be bookmarked.
 GET methods have length restrictions where as
POST methods have no length restrictions.
 GET methods remains in history of the web
browser whereas the POST methods are not
present in the history of the web browser.
 GET methods are idempotent but POST methods
are non-idempotent.
HTTP protocol
 The other HTTP methods are:-
1. Trace
2. Put
3. Options
4. Head
5. Delete
Introduction to Servlet
 Servlet are Java class files which resides on
the web server which produces dynamic
output web pages when a client request for the
particular Servlet.
 So to execute a Servlet we need a compiler
and a JVM machine inside a web server same
as when we need to compile and run a simple
java program inside our desktop.
Introduction to Servlet
 To deploy our servlet inside the web server we
need a servlet container.
 Servlet container is a java virtual machine
inside web server which provides compilation
and run time execution of servlet.
 “Apache tomcat” is an example of popular
servlet container.
J2ee container and architecture
J2ee container and architecture
 Containers are used for deploying and
execution service provided by the component.
 The various types of container in J2EE:-
A. Applet container
B. Application client container
C. EJB container
D. Servlet container
Servlet Life Cycle
 When the client request for the servlet through
the web browser the Servlet container loads
the servlet for the first time, it calls the init()
method. Before the servlet can respond to any
request the init() method need to be finished or
the servlet need to be initialize. After the init()
method the servlet container marked the
servlet as available.
Servlet Life Cycle
 For each request form the client the servlet
container calls the service method on the
servlet which is going to process the request.
The service method can use all the resources
which are marked as available in the init()
method.
 The destroy() method is called to clean up the
resources when the server shuts down or
there is a lack of resources. It also calls the
save the current state information of the
servlet which will be used when the servlet
again called for the next time.
Functions of servlet container
 Manage servlet life cycles.
 Register a servlet for one or more URLS.
 Encode MIME base response.
 Decode MIME base request.
 Handles HTTP protocols along with other
protocols(ftp,pop,smtp,telnet,etc).
 Provide network services with request and
responses cycles.
Servlet API
 The java servlet API consist of two Packages
which helps in implementation of the servlet:-
1. javax.servlet
2. javax.servlet.http
Servlet API
 javax.servlet:- It provides the core functionality
of the servlet, this API consist of classes and
interfaces which is generic and protocol
independent.
 javax.servlet.http:- This API consist of classes
and interfaces which are HTTP protocol
specific.
SERVLET API
SERVLET API
 There are two types of servlet available by default
which has been defined in servlet API
1. GenericServlet
2. HttpServlet
 GenericServlet : it defines the classes and
interfaces which are protocol independent and
generic which is define on the javax.servlet
package. It defines simple life cycle methods
such as init, service, destroy. To write generic
servlet we simply have to override the abstract
service() method.
SERVLET API
 Signature of GenericServlet:-
 public abstract class GenericServlet extends
java.io.lang implements
Servlet, ServletConfig, java.io.seriliziable
SERVLET API
 HttpServlet :- it defines classes and interfaces
which are http protocol specific. It extends the
GenericServlet class therfore it inherits the
GenericServlet methods.
 Signature of HttpServlet:-
 Public abstract class HttpServlet extends
GenericServlet implements java.io.sereliziable
Example of servlet
 To write our own servlet we have to extend the
HttpServlet class.
 Public class our_servlet extends HttpServlet {
Public void doGet(HttpServletRequest req,HttpServletResponse
resp)throws IOException,ServletException{
//code for servlet
//the logic of the application
}
}
Example of servlet
 teddy_test.java
Public void display extends HttpServlet{
Public void doGet(HttpServlet response, HttpSrvlet request)throws
IOException,ServletException{
out.println(“<HTML>”);
out.println(“< BODY>”);
response.setContentType(“text/html”);
PrintWriter out=resp.getWriter();
out.println(“<p>Hello World</p>”);
out.println(“</ BODY>”);
out.println(“< /HTML>”);
}
}
Example of servlet
 Reading Form Data using Servlet:-
 Servlets handles form data parsing automatically using the following
methods depending on the situation:
 getParameter(): -You call request.getParameter() method to get the
value of a form parameter.
 getParameterValues(): -Call this method if the parameter appears
more than once and returns multiple values, for example checkbox.
 getParameterNames(): -Call this method if you want a complete
list of all parameters in the current request.
Example of servlet
 To get information from an html page
 Let assume there are two text box present in
html page name “teddy_test.html”
<form ACTION=“teddy_servlet.java”>
Name:<input type=“text” name=“nme”>
Age:<input type=“text” name=“age”>
<input type=“submit”>
Example of servlet
 teddy_servlet.java
Public void display extends HttpServlet{
Public void doGet(HttpServlet response, HttpSrvlet request)throws
IOException,ServletException{
out.println(“<HTML>”);
out.println(“< BODY>”);
response.setContentType(“text/html”);
Printwriter out=resp.getWriter();
String name=req.getParameter(“name”);
String age=req.getParameter(“age”);
out.println(“<p>name:-”+name+”age:-”+age+”</p>”);
out.println(“</ BODY>”);
out.println(“< /HTML>”);
}
}
Session Management
Techniques
 As HTTP is a stateless protocol it cannot
remember the same user over multiple page
request and treats the same user as different
user.
 So, if a user request for a page the server
responds when the same user request for the
another page the server thinks as another
user.
 The value or the state of the page need to be
retained when we purchasing goods from
merchant sites or keep us out of daily login to
a particular sites.
Session Management
Techniques
 The are methods to retain this value over
many pages:-
1. Cookies.
2. Hidden from fields
3. Url rewriting
4. Http Session
Session Management
Techniques
 Cookies are textual information which are
stored on the client file system.
 Cookies are send by the web server to the
web browser.
 Cookies contain cookie name and cookie
value.
 Advantages:-
1. They are simple to maintain
2. They are maintained by the client no over
head of the server.
Session Management
Techniques
 Disadvantage:-
1. It will not work if it is disabled by the web
browser.
2. Easy to tamper and one person can
impersonate as another user.
 Syntax:-
Cookie ck_object=new
Cookie(“cookie_name”,cookie_value);
Session Management
Techniques
 Hidden from fields, in HTML we have to type
an extra field called “hidden” which will not be
shown to the user by the web browser but it
will pass information to the web server. It
contains value from the previous request
which will going to process the future request.
Session Management
Techniques
 Advantage:-
1. To process it we do not require any special
type of server.
2. It will work where cookies will be disable by
the web browser.
 Disadvantage:-
1. If an error occurred before the values are
saved then the values are lost.
Session Management
Techniques
 Syntax:-
<input type=“hidden” name=“” values=“”>
This field will not be shown to the user but it will
pass information to the app server.
Session Management
Techniques
 Url rewriting, it a technique of appending
values to the end of the url, so that when the
user clicks on the link the values of the current
page will be passed through the url.
 Advantage:-
1. No extra form submission is required as in
hidden from fields.
Session Management
Techniques
 Disadvantage:-
1. It can only pass textual information.
2. Work with link only.
 Syntax:-
http://url? name1=value1&name2=value2
 Appends value on the end of the url where
parameters are separated by „&‟ and
name, value pairs are separated by „=„.
Session Management
Techniques
 HttpSession object is completely maintained
and created by the Web server. To achieve this
there is HttpRequest.getSession() method.
This method returns true if there the session is
created or creates a session before returning.
It is used to identify the user across multiple
pages and they can live up to specific time
bound by the server.
Session Management
Techniques
 Syntax:-
HttpSession ses_object=request.getSession();
ses_object.setAttribute(“ses_name”,ses_value);
//to set the session on the web server.
ses_object.getAttribute(“ses_name”);
// to get the value of the session from the web server.
Servlet Collaboration
 Servlet collaboration is a technique of sharing
information between the servlets.
 In servlet collaboration on servlet pass the
information directly to the another servlet.
 To pass the information directly one servlet
need to know about the other.
Servlet Collaboration
 The servlet collaboration can be achieved by
following methods :-
1. forward
2. include
3. sendRedirect
Servlet Collaboration
 Forward and include is the method of
RequestDispatcher interface.
 Syntax:-
RequestDispatcher
rd=response.getRequestDispatcher(“url”);
rd.forward(request,reponse);
 sendRedirect is the method of
HttpServletResponse interface
 Syntax:-
response.sendRedirect(“url of the redirect
servlet”);
Servlet Collaboration
 Difference between sendRedirect and forward:-
 The sendRedirect method is used to send the
forwarded address to resources in a new domain
or server where as the forward method is used to
send the forwarded address to resources inside
the server.
 In case of forward method the servlet container
takes cares of everything, the client and browser
is not involved, whereas in case of sendRedirect
method the forwarded address is sent to the
browser of the client.
Servlet Collaboration
 In forward, the old request and response is
send along with the forwarded address, the old
request is going to process the new request. In
case of sendRedirect the old request and
response is lost and a new response is
created by the web server and treated as a
new request by the browser.
Servlet Collaboration
 In forward the forwarded address in not seen
by the client; its is transparent whereas in
sendRedirect the url is present in the url bar of
the web browser.
 The forward method is fast than the
sendRedirect as it does not require an extra
round trip.
CGI(Common Gateway Interface) allows the application server to
call an external program which is going to process the client http
request and respond to it. It creates a new process for each
request made by the client.
What is CGI?
Servlet Vs. CGI
 CGI(Common Gateway Interface) scripts
creates a separate process for each request
made by the client where as servlet is initialize
only once and creates threads for each
request, so CGI scripts are lead to overhead
when there is rise in user.
 CGI are more prone to attacks than servlet.
 CGI scripts are executed to native to operating
system where as servlet are compiled to java
byte code which can run anywhere.
Servlet Vs. CGI
 CGI are platform dependent whereas servlet
are platform independent.
 Servlet handling of request us much faster
than CGI.
Thank you

More Related Content

PPTX
Introduction to Spring Framework
Serhat Can
 
PPT
JavaScript: Events Handling
Yuriy Bezgachnyuk
 
PPTX
Bootstrap 4 ppt
EPAM Systems
 
PPTX
Enterprise java unit-2_chapter-3
sandeep54552
 
PPT
Java And Multithreading
Shraddha
 
PPT
Jsp ppt
Vikas Jagtap
 
PPT
JavaScript & Dom Manipulation
Mohammed Arif
 
PPTX
Bootstrap PPT by Mukesh
Mukesh Kumar
 
Introduction to Spring Framework
Serhat Can
 
JavaScript: Events Handling
Yuriy Bezgachnyuk
 
Bootstrap 4 ppt
EPAM Systems
 
Enterprise java unit-2_chapter-3
sandeep54552
 
Java And Multithreading
Shraddha
 
Jsp ppt
Vikas Jagtap
 
JavaScript & Dom Manipulation
Mohammed Arif
 
Bootstrap PPT by Mukesh
Mukesh Kumar
 

What's hot (20)

PPT
Introduction to java beans
Hitesh Parmar
 
PPTX
Enterprise java unit-1_chapter-3
sandeep54552
 
PPT
Method overriding
Azaz Maverick
 
PDF
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
PDF
JavaScript - Chapter 11 - Events
WebStackAcademy
 
PPTX
Servlets
ZainabNoorGul
 
PPT
Java-java virtual machine
Surbhi Panhalkar
 
PPTX
Spring Framework
tola99
 
PPTX
JAVA AWT
shanmuga rajan
 
PPT
Java: GUI
Tareq Hasan
 
PPT
Java Servlets
BG Java EE Course
 
PDF
Java I/o streams
Hamid Ghorbani
 
PPTX
servlet in java
sowfi
 
PDF
Asp .net web form fundamentals
Gopal Ji Singh
 
PPTX
Hibernate ppt
Aneega
 
PDF
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Edureka!
 
PDF
JPA and Hibernate
elliando dias
 
PDF
jQuery for beginners
Arulmurugan Rajaraman
 
PDF
Javabeans .pdf
Rajkiran Mummadi
 
PPTX
JSON: The Basics
Jeff Fox
 
Introduction to java beans
Hitesh Parmar
 
Enterprise java unit-1_chapter-3
sandeep54552
 
Method overriding
Azaz Maverick
 
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
JavaScript - Chapter 11 - Events
WebStackAcademy
 
Servlets
ZainabNoorGul
 
Java-java virtual machine
Surbhi Panhalkar
 
Spring Framework
tola99
 
JAVA AWT
shanmuga rajan
 
Java: GUI
Tareq Hasan
 
Java Servlets
BG Java EE Course
 
Java I/o streams
Hamid Ghorbani
 
servlet in java
sowfi
 
Asp .net web form fundamentals
Gopal Ji Singh
 
Hibernate ppt
Aneega
 
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Edureka!
 
JPA and Hibernate
elliando dias
 
jQuery for beginners
Arulmurugan Rajaraman
 
Javabeans .pdf
Rajkiran Mummadi
 
JSON: The Basics
Jeff Fox
 
Ad

Similar to java Servlet technology (20)

PPTX
UNIT-3 Servlet
ssbd6985
 
PPTX
JAVA SERVLETS acts as a middle layer between a request coming from a web brow...
ssuser4f7d71
 
ODP
servlet 2.5 & JSP 2.0
megrhi haikel
 
ODP
Servlets
ramesh kumar
 
PPTX
Servlets-UNIT3and introduction to servlet
RadhikaP41
 
PPT
Servlets
Manav Prasad
 
PPT
Servlets
Sasidhar Kothuru
 
PPT
Module 4.pptModule 4.pptModule 4.pptModule 4.ppt
tahirnaquash2
 
PPT
Servlet.ppt
MouDhara1
 
PPT
Servlet.ppt
kstalin2
 
PPT
Servlet1.ppt
KhushalChoudhary14
 
PPTX
servlets sessions and cookies, jdbc connectivity
snehalatha790700
 
PPTX
21CS642 Module 4_1 Servlets PPT.pptx VI SEM CSE Students
VENKATESHBHAT25
 
PPT
Servlet123jkhuiyhkjkljioyudfrtsdrestfhgb
shubhangimalas1
 
PPT
Servlet (1) also contains code to create it.ppt
juhishrivastava25
 
PDF
Java Servlets.pdf
Arumugam90
 
PPTX
CS8651 IP Unit 3.pptx
Vigneshkumar Ponnusamy
 
PDF
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
Tushar B Kute
 
PPTX
Servlets
Akshay Ballarpure
 
PPT
JAVA Servlets
deepak kumar
 
UNIT-3 Servlet
ssbd6985
 
JAVA SERVLETS acts as a middle layer between a request coming from a web brow...
ssuser4f7d71
 
servlet 2.5 & JSP 2.0
megrhi haikel
 
Servlets
ramesh kumar
 
Servlets-UNIT3and introduction to servlet
RadhikaP41
 
Servlets
Manav Prasad
 
Module 4.pptModule 4.pptModule 4.pptModule 4.ppt
tahirnaquash2
 
Servlet.ppt
MouDhara1
 
Servlet.ppt
kstalin2
 
Servlet1.ppt
KhushalChoudhary14
 
servlets sessions and cookies, jdbc connectivity
snehalatha790700
 
21CS642 Module 4_1 Servlets PPT.pptx VI SEM CSE Students
VENKATESHBHAT25
 
Servlet123jkhuiyhkjkljioyudfrtsdrestfhgb
shubhangimalas1
 
Servlet (1) also contains code to create it.ppt
juhishrivastava25
 
Java Servlets.pdf
Arumugam90
 
CS8651 IP Unit 3.pptx
Vigneshkumar Ponnusamy
 
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
Tushar B Kute
 
JAVA Servlets
deepak kumar
 
Ad

More from Tanmoy Barman (7)

PPSX
Java rmi
Tanmoy Barman
 
PPSX
Jini
Tanmoy Barman
 
PPSX
JDBC: java DataBase connectivity
Tanmoy Barman
 
PPSX
Java server pages
Tanmoy Barman
 
PPTX
Web apps architecture
Tanmoy Barman
 
DOCX
introduction to channel borrowing scheme in cellular networks
Tanmoy Barman
 
PPT
INTRODUCTION TO CLOUD COMPUTING
Tanmoy Barman
 
Java rmi
Tanmoy Barman
 
JDBC: java DataBase connectivity
Tanmoy Barman
 
Java server pages
Tanmoy Barman
 
Web apps architecture
Tanmoy Barman
 
introduction to channel borrowing scheme in cellular networks
Tanmoy Barman
 
INTRODUCTION TO CLOUD COMPUTING
Tanmoy Barman
 

Recently uploaded (20)

PDF
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PDF
Doc9.....................................
SofiaCollazos
 
PPTX
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
 
PDF
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
PDF
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
PDF
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
PPT
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
DevOps & Developer Experience Summer BBQ
AUGNYC
 
PDF
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
PPTX
Coupa-Overview _Assumptions presentation
annapureddyn
 
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
PDF
Software Development Company | KodekX
KodekX
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
Software Development Methodologies in 2025
KodekX
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
Doc9.....................................
SofiaCollazos
 
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
 
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
DevOps & Developer Experience Summer BBQ
AUGNYC
 
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
Coupa-Overview _Assumptions presentation
annapureddyn
 
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
Software Development Company | KodekX
KodekX
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 

java Servlet technology

  • 2. Discussions  Client-Server Architecture.  HTTP protocol.  Introduction to Servlet.  J2ee container and architecture.  Servlet Life Cycle.  Functions of servlet container.  Servlet API.  Example of servlet.  Session Management Techniques.  Servlet Collaboration.  Comparing Servlet with CGI.
  • 4. Client server architecture  Server: typically a application for large computers which can handle multiple request simultaneously, can process much faster. The application of server is written in such a way that it cannot be access by the user directly but it waits for another program to connect and then it process the request as need by the user.
  • 5. Client server architecture  Client: an application that runs on simple desktop computers. It has an attractive user interface but itself does not have the data to manipulate instead it takes input from the user, connects the server and the server responds to the request as need by the client using this application.
  • 6. HTTP protocol  HTTP(Hyper Text Transfer Protocol ) is a communication protocol for displaying HTML pages inside web browser.  HTTP are stateless protocol because it treats each request independently and have no knowledge of where the request have came from.  A stateless protocol treats each request and response pair as an independent transaction.  Default port no of HTTP is 80.
  • 7. HTTP protocol  The communication between the web browser and the server are usually done using two methods:- 1. GET. 2. POST.  GET method is used to request data from a specified resource.  POST method is used to submit data from a specified resource.
  • 8. HTTP protocol  GET methods are cached but POST method cannot be cached.  GET methods can be bookmarked but POST methods cannot be bookmarked.  GET methods have length restrictions where as POST methods have no length restrictions.  GET methods remains in history of the web browser whereas the POST methods are not present in the history of the web browser.  GET methods are idempotent but POST methods are non-idempotent.
  • 9. HTTP protocol  The other HTTP methods are:- 1. Trace 2. Put 3. Options 4. Head 5. Delete
  • 10. Introduction to Servlet  Servlet are Java class files which resides on the web server which produces dynamic output web pages when a client request for the particular Servlet.  So to execute a Servlet we need a compiler and a JVM machine inside a web server same as when we need to compile and run a simple java program inside our desktop.
  • 11. Introduction to Servlet  To deploy our servlet inside the web server we need a servlet container.  Servlet container is a java virtual machine inside web server which provides compilation and run time execution of servlet.  “Apache tomcat” is an example of popular servlet container.
  • 12. J2ee container and architecture
  • 13. J2ee container and architecture  Containers are used for deploying and execution service provided by the component.  The various types of container in J2EE:- A. Applet container B. Application client container C. EJB container D. Servlet container
  • 14. Servlet Life Cycle  When the client request for the servlet through the web browser the Servlet container loads the servlet for the first time, it calls the init() method. Before the servlet can respond to any request the init() method need to be finished or the servlet need to be initialize. After the init() method the servlet container marked the servlet as available.
  • 15. Servlet Life Cycle  For each request form the client the servlet container calls the service method on the servlet which is going to process the request. The service method can use all the resources which are marked as available in the init() method.  The destroy() method is called to clean up the resources when the server shuts down or there is a lack of resources. It also calls the save the current state information of the servlet which will be used when the servlet again called for the next time.
  • 16. Functions of servlet container  Manage servlet life cycles.  Register a servlet for one or more URLS.  Encode MIME base response.  Decode MIME base request.  Handles HTTP protocols along with other protocols(ftp,pop,smtp,telnet,etc).  Provide network services with request and responses cycles.
  • 17. Servlet API  The java servlet API consist of two Packages which helps in implementation of the servlet:- 1. javax.servlet 2. javax.servlet.http
  • 18. Servlet API  javax.servlet:- It provides the core functionality of the servlet, this API consist of classes and interfaces which is generic and protocol independent.  javax.servlet.http:- This API consist of classes and interfaces which are HTTP protocol specific.
  • 20. SERVLET API  There are two types of servlet available by default which has been defined in servlet API 1. GenericServlet 2. HttpServlet  GenericServlet : it defines the classes and interfaces which are protocol independent and generic which is define on the javax.servlet package. It defines simple life cycle methods such as init, service, destroy. To write generic servlet we simply have to override the abstract service() method.
  • 21. SERVLET API  Signature of GenericServlet:-  public abstract class GenericServlet extends java.io.lang implements Servlet, ServletConfig, java.io.seriliziable
  • 22. SERVLET API  HttpServlet :- it defines classes and interfaces which are http protocol specific. It extends the GenericServlet class therfore it inherits the GenericServlet methods.  Signature of HttpServlet:-  Public abstract class HttpServlet extends GenericServlet implements java.io.sereliziable
  • 23. Example of servlet  To write our own servlet we have to extend the HttpServlet class.  Public class our_servlet extends HttpServlet { Public void doGet(HttpServletRequest req,HttpServletResponse resp)throws IOException,ServletException{ //code for servlet //the logic of the application } }
  • 24. Example of servlet  teddy_test.java Public void display extends HttpServlet{ Public void doGet(HttpServlet response, HttpSrvlet request)throws IOException,ServletException{ out.println(“<HTML>”); out.println(“< BODY>”); response.setContentType(“text/html”); PrintWriter out=resp.getWriter(); out.println(“<p>Hello World</p>”); out.println(“</ BODY>”); out.println(“< /HTML>”); } }
  • 25. Example of servlet  Reading Form Data using Servlet:-  Servlets handles form data parsing automatically using the following methods depending on the situation:  getParameter(): -You call request.getParameter() method to get the value of a form parameter.  getParameterValues(): -Call this method if the parameter appears more than once and returns multiple values, for example checkbox.  getParameterNames(): -Call this method if you want a complete list of all parameters in the current request.
  • 26. Example of servlet  To get information from an html page  Let assume there are two text box present in html page name “teddy_test.html” <form ACTION=“teddy_servlet.java”> Name:<input type=“text” name=“nme”> Age:<input type=“text” name=“age”> <input type=“submit”>
  • 27. Example of servlet  teddy_servlet.java Public void display extends HttpServlet{ Public void doGet(HttpServlet response, HttpSrvlet request)throws IOException,ServletException{ out.println(“<HTML>”); out.println(“< BODY>”); response.setContentType(“text/html”); Printwriter out=resp.getWriter(); String name=req.getParameter(“name”); String age=req.getParameter(“age”); out.println(“<p>name:-”+name+”age:-”+age+”</p>”); out.println(“</ BODY>”); out.println(“< /HTML>”); } }
  • 28. Session Management Techniques  As HTTP is a stateless protocol it cannot remember the same user over multiple page request and treats the same user as different user.  So, if a user request for a page the server responds when the same user request for the another page the server thinks as another user.  The value or the state of the page need to be retained when we purchasing goods from merchant sites or keep us out of daily login to a particular sites.
  • 29. Session Management Techniques  The are methods to retain this value over many pages:- 1. Cookies. 2. Hidden from fields 3. Url rewriting 4. Http Session
  • 30. Session Management Techniques  Cookies are textual information which are stored on the client file system.  Cookies are send by the web server to the web browser.  Cookies contain cookie name and cookie value.  Advantages:- 1. They are simple to maintain 2. They are maintained by the client no over head of the server.
  • 31. Session Management Techniques  Disadvantage:- 1. It will not work if it is disabled by the web browser. 2. Easy to tamper and one person can impersonate as another user.  Syntax:- Cookie ck_object=new Cookie(“cookie_name”,cookie_value);
  • 32. Session Management Techniques  Hidden from fields, in HTML we have to type an extra field called “hidden” which will not be shown to the user by the web browser but it will pass information to the web server. It contains value from the previous request which will going to process the future request.
  • 33. Session Management Techniques  Advantage:- 1. To process it we do not require any special type of server. 2. It will work where cookies will be disable by the web browser.  Disadvantage:- 1. If an error occurred before the values are saved then the values are lost.
  • 34. Session Management Techniques  Syntax:- <input type=“hidden” name=“” values=“”> This field will not be shown to the user but it will pass information to the app server.
  • 35. Session Management Techniques  Url rewriting, it a technique of appending values to the end of the url, so that when the user clicks on the link the values of the current page will be passed through the url.  Advantage:- 1. No extra form submission is required as in hidden from fields.
  • 36. Session Management Techniques  Disadvantage:- 1. It can only pass textual information. 2. Work with link only.  Syntax:- http://url? name1=value1&name2=value2  Appends value on the end of the url where parameters are separated by „&‟ and name, value pairs are separated by „=„.
  • 37. Session Management Techniques  HttpSession object is completely maintained and created by the Web server. To achieve this there is HttpRequest.getSession() method. This method returns true if there the session is created or creates a session before returning. It is used to identify the user across multiple pages and they can live up to specific time bound by the server.
  • 38. Session Management Techniques  Syntax:- HttpSession ses_object=request.getSession(); ses_object.setAttribute(“ses_name”,ses_value); //to set the session on the web server. ses_object.getAttribute(“ses_name”); // to get the value of the session from the web server.
  • 39. Servlet Collaboration  Servlet collaboration is a technique of sharing information between the servlets.  In servlet collaboration on servlet pass the information directly to the another servlet.  To pass the information directly one servlet need to know about the other.
  • 40. Servlet Collaboration  The servlet collaboration can be achieved by following methods :- 1. forward 2. include 3. sendRedirect
  • 41. Servlet Collaboration  Forward and include is the method of RequestDispatcher interface.  Syntax:- RequestDispatcher rd=response.getRequestDispatcher(“url”); rd.forward(request,reponse);  sendRedirect is the method of HttpServletResponse interface  Syntax:- response.sendRedirect(“url of the redirect servlet”);
  • 42. Servlet Collaboration  Difference between sendRedirect and forward:-  The sendRedirect method is used to send the forwarded address to resources in a new domain or server where as the forward method is used to send the forwarded address to resources inside the server.  In case of forward method the servlet container takes cares of everything, the client and browser is not involved, whereas in case of sendRedirect method the forwarded address is sent to the browser of the client.
  • 43. Servlet Collaboration  In forward, the old request and response is send along with the forwarded address, the old request is going to process the new request. In case of sendRedirect the old request and response is lost and a new response is created by the web server and treated as a new request by the browser.
  • 44. Servlet Collaboration  In forward the forwarded address in not seen by the client; its is transparent whereas in sendRedirect the url is present in the url bar of the web browser.  The forward method is fast than the sendRedirect as it does not require an extra round trip.
  • 45. CGI(Common Gateway Interface) allows the application server to call an external program which is going to process the client http request and respond to it. It creates a new process for each request made by the client. What is CGI?
  • 46. Servlet Vs. CGI  CGI(Common Gateway Interface) scripts creates a separate process for each request made by the client where as servlet is initialize only once and creates threads for each request, so CGI scripts are lead to overhead when there is rise in user.  CGI are more prone to attacks than servlet.  CGI scripts are executed to native to operating system where as servlet are compiled to java byte code which can run anywhere.
  • 47. Servlet Vs. CGI  CGI are platform dependent whereas servlet are platform independent.  Servlet handling of request us much faster than CGI.