3
Most read
17
Most read
Servlets
List of Topics
 What are Servlet
 Servlet Architecture
 Life cycle
 Example
 FORM
 Servlet deployment
 Filter
 Packaging
 Debugging
What are Servlets ?
Java Servlets are programs that run on a Web or Application server and act as a
middle layer between a request coming from a Web browser or other HTTP
client and databases or applications on the HTTP server.
Using Servlets, you can collect input from users through web page forms,
present records from a database or another source, and create web pages
dynamically.
Servlets advantages
 Performance is significantly better.
 Servlets execute within the address space of a Web server. It is not necessary to
create a separate process to handle each client request.
 Servlets are platform-independent because they are written in Java.
 Java security manager on the server enforces a set of restrictions to protect the
resources on a server machine. So servlets are trusted.
 The full functionality of the Java class libraries is available to a servlet. It can
communicate with applets, databases, or other software via the sockets and RMI
mechanisms that you have seen already.
Servlet Architecture
Servlet Task
 Read the explicit data sent by the clients (browsers). This includes an HTML
form on a Web page or it could also come from an applet or a custom HTTP
client program.
 Read the implicit HTTP request data sent by the clients (browsers). This
includes cookies, media types and compression schemes the browser
understands, and so forth.
 Process the data and generate the results. This process may require talking to a
database, executing an RMI or CORBA call, invoking a Web service, or
computing the response directly.
 Send the explicit data (i.e., the document) to the clients (browsers). This
document can be sent in a variety of formats, including text (HTML or XML),
binary (GIF images), Excel, etc.
 Send the implicit HTTP response to the clients (browsers). This includes
telling the browsers or other clients what type of document is being returned
(e.g., HTML), setting cookies and caching parameters, and other such tasks.
Servlets Package
 Java Servlets are Java classes run by a web server that has an interpreter
that supports the Java Servlet specification.
 Servlets can be created using
the javax.servlet and javax.servlet.http packages, which are a
standard part of the Java's enterprise edition, an expanded version of
the Java class library that supports large-scale development projects.
 Java servlets have been created and compiled just like any other Java
class. After you install the servlet packages and add them to your
computer's Class path, you can compile servlets with the JDK's Java
compiler or any other current compiler
servlet-api.jar present in <Tomcat>/lib dir
Servlet – Life cycle
 A servlet life cycle can be defined as the entire process from its creation till the
destruction. The following are the paths followed by a servlet
 The servlet is initialized by calling the init () method.
 The servlet calls service() method to process a client's request.
 The servlet is terminated by calling the destroy() method.
 Finally, servlet is garbage collected by the garbage collector of the JVM.
 The init() method :
The init method is designed to be called only once. It is called when the servlet
is first created, and not called again for each user request. So, it is used for one-
time initializations, just as with the init method of applets.
The servlet is normally created when a user first invokes a URL corresponding
to the servlet, but you can also specify that the servlet be loaded when the
server is first started.
Life cycle cont…
Life cycle cont…
Life cycle cont…
When a user invokes a servlet, a single instance of each servlet gets created, with each
user request resulting in a new thread that is handed off to doGet or doPost as
appropriate. The init() method simply creates or loads some data that will be used
throughout the life of the servlet.
The init method definition looks like this:
public void init() throws ServletException {
// Initialization code...
}
 The service() method :
The service() method is the main method to perform the actual task. The servlet
container (i.e. web server) calls the service() method to handle requests coming from the
client( browsers) and to write the formatted response back to the client.
Each time the server receives a request for a servlet, the server spawns a new thread and
calls service. The service() method checks the HTTP request type (GET, POST, PUT,
DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate.
Life cycle cont…
Here is the signature of this method:
public void service(ServletRequest request, ServletResponse response) throws
ServletException, IOException{
}
 The service () method is called by the container and service method invokes doGet,
doPost, doPut, doDelete, etc. methods as appropriate. So you have nothing to do with
service() method but you override either doGet() or doPost() depending on what type of
request you receive from the client.
 The doGet() and doPost() are most frequently used methods with in each service request.
Here is the signature of these two methods.
Life cycle cont…
 The doGet() Method
A GET request results from a normal request for a URL or from an HTML form
that has no METHOD specified and it should be handled by doGet() method.
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Servlet code
}
 The doPost() Method
A POST request results from an HTML form that specifically lists POST as the
METHOD and it should be handled by doPost() method.
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Servlet code
}
Life cycle cont…
 The destroy() method :
The destroy() method is called only once at the end of the life cycle of a servlet.
This method gives your servlet a chance to close database connections, halt
background threads, write cookie lists or hit counts to disk, and perform other
such cleanup activities.
After the destroy() method is called, the servlet object is marked for garbage
collection. The destroy method definition looks like this:
public void destroy() {
// Finalization code...
}
Servlet types
 Servlet is an object that extends either generic servlet class or http servlet
 Generic servlet class define methods for building protocol independent servlet
 Http servlet class provide methods specific to HTTP protocol
 Servlets are protocol independent. Thus, servlet can support FTP commands,
SMTP(Simple Mail Transfer Protocol), POP3 (Post Office Protocol), telnet, HTTP and
other protocols.
Architecture diagram
The following figure depicts a typical servlet life-cycle scenario.
 First the HTTP requests coming to the server are delegated to the servlet container.
 The servlet container loads the servlet before invoking the service() method.
 Then the servlet container handles multiple requests by spawning multiple threads,
each thread executing the service() method of a single instance of the servlet.
Servlet FORM data
 Consider a situation when you need to pass some information from your
browser to web server and ultimately to your backend program. The
browser uses two methods to pass this information to web server. These
methods are GET Method and POST Method
 GET method:
The GET method sends the encoded user information appended to the
page request. The page and the encoded information are separated by the ?
character as follows:
http://www.test.com/hello?key1=value1&key2=value2
The GET method is the default method to pass information from browser
to web server and it produces a long string that appears in your browser's
Location. Never use the GET method if you have password or other
sensitive information to pass to the server. The GET method has size
limitation : only 1024 characters can be in a request string.
FORM cont…
 POST method:
A generally more reliable method of passing information to a backend
program is the POST method. This packages the information in exactly
the same way as GET methods, but instead of sending it as a text string
after a ? in the URL it sends it as a separate message. This message
comes to the backend program in the form of the standard input which
you can parse and use for your processing. Servlet handles this type of
requests using doPost() method. Parameters are encrypted.
Deploying Servlet in App server
Steps
 Under the $CATALINA_HOME/webapps folder ( henceforth, just webapps ), create a new folder for
your webapp; I'll be calling it servletTestApp.
 Under servletTestApp create another folder called WEB-INF.
 Under WEB-INF, create a folder called classes.
 Also, under WEB-INF, create a file called web.xml ( just create a text file and rename the extension ).
 Next, create the servlet, testServlet.
 I've put the servlet in a package, testServletPackage, to demonstrate. So, create a folder under classes
called testServletPackage.
 Compile the servlet.
 Edit the web.xml to add the following servlet mapping:

More Related Content

PDF
Django Introduction & Tutorial
PDF
Airline Reservation System - Software Engineering
PPTX
Servlets
PDF
NFA to DFA
PPTX
Client server architecture
PPT
Basics Of Networking (Overview)
PPT
Java Servlets
PPTX
Statistics-Regression analysis
Django Introduction & Tutorial
Airline Reservation System - Software Engineering
Servlets
NFA to DFA
Client server architecture
Basics Of Networking (Overview)
Java Servlets
Statistics-Regression analysis

What's hot (20)

PPTX
java Servlet technology
PDF
Servlet and servlet life cycle
PPTX
Servlets
PPT
PPTX
Java Server Pages(jsp)
PPTX
Web forms in ASP.net
PDF
Java servlets
PDF
Hibernate Presentation
PPTX
Java servlets
PPT
Java Networking
PPTX
Chapter 3 servlet & jsp
PPS
Jsp element
PDF
MVC Architecture
ODP
Multithreading In Java
PPTX
servlet in java
PPTX
Jsf presentation
PPTX
Session tracking in servlets
PPT
Java And Multithreading
java Servlet technology
Servlet and servlet life cycle
Servlets
Java Server Pages(jsp)
Web forms in ASP.net
Java servlets
Hibernate Presentation
Java servlets
Java Networking
Chapter 3 servlet & jsp
Jsp element
MVC Architecture
Multithreading In Java
servlet in java
Jsf presentation
Session tracking in servlets
Java And Multithreading
Ad

Viewers also liked (20)

PPT
Java servlet life cycle - methods ppt
PPT
Java Servlets
DOC
Java Servlets & JSP
PDF
Dynamic content generation
PPTX
PPT
Strings v.1.1
PPTX
C# Strings
RTF
Servlet lifecycle
PDF
Java EE 01-Servlets and Containers
PPT
An Introduction To Java Web Technology
PDF
Servlets
PPT
Eo gaddis java_chapter_12_5e
PPT
Java Servlet
PDF
Tarifas Ica Bogota
PPTX
Website Design Issues
PPT
Applet Architecture - Introducing Java Applets
PPTX
Applet java
PDF
Web Usability (Slideshare Version)
PPTX
Corba concepts & corba architecture
Java servlet life cycle - methods ppt
Java Servlets
Java Servlets & JSP
Dynamic content generation
Strings v.1.1
C# Strings
Servlet lifecycle
Java EE 01-Servlets and Containers
An Introduction To Java Web Technology
Servlets
Eo gaddis java_chapter_12_5e
Java Servlet
Tarifas Ica Bogota
Website Design Issues
Applet Architecture - Introducing Java Applets
Applet java
Web Usability (Slideshare Version)
Corba concepts & corba architecture
Ad

Similar to Servlets (20)

DOCX
Major project report
PPTX
JAVA SERVLETS acts as a middle layer between a request coming from a web brow...
DOCX
Servlet
PPT
Web Tech Java Servlet Update1
DOC
Servlet basics
PPTX
Http Server Programming in JAVA - Handling http requests and responses
PPTX
Servlet in java , java servlet , servlet servlet and CGI, API
PPTX
UNIT-3 Servlet
PPT
Lecture 2
PPTX
Wt unit 3
PPTX
Servlets & jdbc
PPTX
SERVLET in web technolgy engineering.pptx
DOC
Unit5 servlets
PPT
Presentation on java servlets
PDF
Bt0083 server side programing
PPTX
J2ee servlet
PPTX
Java Servlet
PDF
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
PPT
Servlet ppt by vikas jagtap
Major project report
JAVA SERVLETS acts as a middle layer between a request coming from a web brow...
Servlet
Web Tech Java Servlet Update1
Servlet basics
Http Server Programming in JAVA - Handling http requests and responses
Servlet in java , java servlet , servlet servlet and CGI, API
UNIT-3 Servlet
Lecture 2
Wt unit 3
Servlets & jdbc
SERVLET in web technolgy engineering.pptx
Unit5 servlets
Presentation on java servlets
Bt0083 server side programing
J2ee servlet
Java Servlet
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
Servlet ppt by vikas jagtap

Recently uploaded (20)

PDF
Improvisation in detection of pomegranate leaf disease using transfer learni...
PDF
giants, standing on the shoulders of - by Daniel Stenberg
PDF
CXOs-Are-you-still-doing-manual-DevOps-in-the-age-of-AI.pdf
PDF
The influence of sentiment analysis in enhancing early warning system model f...
PDF
Transform-Your-Factory-with-AI-Driven-Quality-Engineering.pdf
PDF
NewMind AI Weekly Chronicles – August ’25 Week IV
PDF
Flame analysis and combustion estimation using large language and vision assi...
PDF
Transform-Your-Streaming-Platform-with-AI-Driven-Quality-Engineering.pdf
DOCX
search engine optimization ppt fir known well about this
PPTX
Internet of Everything -Basic concepts details
PDF
Co-training pseudo-labeling for text classification with support vector machi...
PDF
Advancing precision in air quality forecasting through machine learning integ...
PDF
The-Future-of-Automotive-Quality-is-Here-AI-Driven-Engineering.pdf
PDF
Lung cancer patients survival prediction using outlier detection and optimize...
PDF
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
PDF
Auditboard EB SOX Playbook 2023 edition.
PDF
Rapid Prototyping: A lecture on prototyping techniques for interface design
PPTX
Build Your First AI Agent with UiPath.pptx
PPTX
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
PDF
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf
Improvisation in detection of pomegranate leaf disease using transfer learni...
giants, standing on the shoulders of - by Daniel Stenberg
CXOs-Are-you-still-doing-manual-DevOps-in-the-age-of-AI.pdf
The influence of sentiment analysis in enhancing early warning system model f...
Transform-Your-Factory-with-AI-Driven-Quality-Engineering.pdf
NewMind AI Weekly Chronicles – August ’25 Week IV
Flame analysis and combustion estimation using large language and vision assi...
Transform-Your-Streaming-Platform-with-AI-Driven-Quality-Engineering.pdf
search engine optimization ppt fir known well about this
Internet of Everything -Basic concepts details
Co-training pseudo-labeling for text classification with support vector machi...
Advancing precision in air quality forecasting through machine learning integ...
The-Future-of-Automotive-Quality-is-Here-AI-Driven-Engineering.pdf
Lung cancer patients survival prediction using outlier detection and optimize...
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
Auditboard EB SOX Playbook 2023 edition.
Rapid Prototyping: A lecture on prototyping techniques for interface design
Build Your First AI Agent with UiPath.pptx
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf

Servlets

  • 2. List of Topics  What are Servlet  Servlet Architecture  Life cycle  Example  FORM  Servlet deployment  Filter  Packaging  Debugging
  • 3. What are Servlets ? Java Servlets are programs that run on a Web or Application server and act as a middle layer between a request coming from a Web browser or other HTTP client and databases or applications on the HTTP server. Using Servlets, you can collect input from users through web page forms, present records from a database or another source, and create web pages dynamically. Servlets advantages  Performance is significantly better.  Servlets execute within the address space of a Web server. It is not necessary to create a separate process to handle each client request.  Servlets are platform-independent because they are written in Java.  Java security manager on the server enforces a set of restrictions to protect the resources on a server machine. So servlets are trusted.  The full functionality of the Java class libraries is available to a servlet. It can communicate with applets, databases, or other software via the sockets and RMI mechanisms that you have seen already.
  • 5. Servlet Task  Read the explicit data sent by the clients (browsers). This includes an HTML form on a Web page or it could also come from an applet or a custom HTTP client program.  Read the implicit HTTP request data sent by the clients (browsers). This includes cookies, media types and compression schemes the browser understands, and so forth.  Process the data and generate the results. This process may require talking to a database, executing an RMI or CORBA call, invoking a Web service, or computing the response directly.  Send the explicit data (i.e., the document) to the clients (browsers). This document can be sent in a variety of formats, including text (HTML or XML), binary (GIF images), Excel, etc.  Send the implicit HTTP response to the clients (browsers). This includes telling the browsers or other clients what type of document is being returned (e.g., HTML), setting cookies and caching parameters, and other such tasks.
  • 6. Servlets Package  Java Servlets are Java classes run by a web server that has an interpreter that supports the Java Servlet specification.  Servlets can be created using the javax.servlet and javax.servlet.http packages, which are a standard part of the Java's enterprise edition, an expanded version of the Java class library that supports large-scale development projects.  Java servlets have been created and compiled just like any other Java class. After you install the servlet packages and add them to your computer's Class path, you can compile servlets with the JDK's Java compiler or any other current compiler servlet-api.jar present in <Tomcat>/lib dir
  • 7. Servlet – Life cycle  A servlet life cycle can be defined as the entire process from its creation till the destruction. The following are the paths followed by a servlet  The servlet is initialized by calling the init () method.  The servlet calls service() method to process a client's request.  The servlet is terminated by calling the destroy() method.  Finally, servlet is garbage collected by the garbage collector of the JVM.  The init() method : The init method is designed to be called only once. It is called when the servlet is first created, and not called again for each user request. So, it is used for one- time initializations, just as with the init method of applets. The servlet is normally created when a user first invokes a URL corresponding to the servlet, but you can also specify that the servlet be loaded when the server is first started.
  • 10. Life cycle cont… When a user invokes a servlet, a single instance of each servlet gets created, with each user request resulting in a new thread that is handed off to doGet or doPost as appropriate. The init() method simply creates or loads some data that will be used throughout the life of the servlet. The init method definition looks like this: public void init() throws ServletException { // Initialization code... }  The service() method : The service() method is the main method to perform the actual task. The servlet container (i.e. web server) calls the service() method to handle requests coming from the client( browsers) and to write the formatted response back to the client. Each time the server receives a request for a servlet, the server spawns a new thread and calls service. The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate.
  • 11. Life cycle cont… Here is the signature of this method: public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException{ }  The service () method is called by the container and service method invokes doGet, doPost, doPut, doDelete, etc. methods as appropriate. So you have nothing to do with service() method but you override either doGet() or doPost() depending on what type of request you receive from the client.  The doGet() and doPost() are most frequently used methods with in each service request. Here is the signature of these two methods.
  • 12. Life cycle cont…  The doGet() Method A GET request results from a normal request for a URL or from an HTML form that has no METHOD specified and it should be handled by doGet() method. public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Servlet code }  The doPost() Method A POST request results from an HTML form that specifically lists POST as the METHOD and it should be handled by doPost() method. public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Servlet code }
  • 13. Life cycle cont…  The destroy() method : The destroy() method is called only once at the end of the life cycle of a servlet. This method gives your servlet a chance to close database connections, halt background threads, write cookie lists or hit counts to disk, and perform other such cleanup activities. After the destroy() method is called, the servlet object is marked for garbage collection. The destroy method definition looks like this: public void destroy() { // Finalization code... }
  • 14. Servlet types  Servlet is an object that extends either generic servlet class or http servlet  Generic servlet class define methods for building protocol independent servlet  Http servlet class provide methods specific to HTTP protocol  Servlets are protocol independent. Thus, servlet can support FTP commands, SMTP(Simple Mail Transfer Protocol), POP3 (Post Office Protocol), telnet, HTTP and other protocols.
  • 15. Architecture diagram The following figure depicts a typical servlet life-cycle scenario.  First the HTTP requests coming to the server are delegated to the servlet container.  The servlet container loads the servlet before invoking the service() method.  Then the servlet container handles multiple requests by spawning multiple threads, each thread executing the service() method of a single instance of the servlet.
  • 16. Servlet FORM data  Consider a situation when you need to pass some information from your browser to web server and ultimately to your backend program. The browser uses two methods to pass this information to web server. These methods are GET Method and POST Method  GET method: The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the ? character as follows: http://www.test.com/hello?key1=value1&key2=value2 The GET method is the default method to pass information from browser to web server and it produces a long string that appears in your browser's Location. Never use the GET method if you have password or other sensitive information to pass to the server. The GET method has size limitation : only 1024 characters can be in a request string.
  • 17. FORM cont…  POST method: A generally more reliable method of passing information to a backend program is the POST method. This packages the information in exactly the same way as GET methods, but instead of sending it as a text string after a ? in the URL it sends it as a separate message. This message comes to the backend program in the form of the standard input which you can parse and use for your processing. Servlet handles this type of requests using doPost() method. Parameters are encrypted.
  • 18. Deploying Servlet in App server Steps  Under the $CATALINA_HOME/webapps folder ( henceforth, just webapps ), create a new folder for your webapp; I'll be calling it servletTestApp.  Under servletTestApp create another folder called WEB-INF.  Under WEB-INF, create a folder called classes.  Also, under WEB-INF, create a file called web.xml ( just create a text file and rename the extension ).  Next, create the servlet, testServlet.  I've put the servlet in a package, testServletPackage, to demonstrate. So, create a folder under classes called testServletPackage.  Compile the servlet.  Edit the web.xml to add the following servlet mapping: