Serve LT
Serve LT
Java Servlet
• Java Servlet technology is used to create a web application (resides at
server side and generates a dynamic web page).
• The Java Servlet technology is robust and scalable because of java
language.
• There are many interfaces and classes in the Servlet API such as
Servlet, GenericServlet, HttpServlet, ServletRequest, ServletResponse,
etc.
Java Servlet
What is a web application?
• A web application is an application accessible from the web.
• A web application is composed of web components like Servlet, JSP,
Filter, etc. and other elements such as HTML, CSS, and JavaScript.
• The web components typically execute in Web Server and respond to
the HTTP request.
Advantages of Servlet
• The web container creates threads for
handling the multiple requests to the
Servlet.
• Threads have many benefits such as
cost of communication between the
threads are low.
Web Terminology
Servlet Interface
• Servlet interface defines methods that all servlets must implement.
• Servlet interface needs to be implemented for creating any servlet.
• It provides 3 life cycle methods that are used to initialize the servlet,
to service the requests, and to destroy the servlet and 2 non-life cycle
methods.
GenericServlet class
• GenericServlet class implements Servlet.
• It provides the implementation of all the methods of the interface
except the service method.
HttpServlet class
• The HttpServlet class extends the GenericServlet class
• It provides http specific methods such as doGet, doPost, doHead,
doTrace etc.
Life Cycle of a Servlet (Servlet Life Cycle)
• Servlet class is loaded.
• Servlet instance is created.
• init method is invoked.
• service method is invoked.
• destroy method is invoked
Ex 1
• Which method is invoked by the servlet container to allow a servlet
to release resources before it is removed from service?
• destroy()
• doDelete()
• doCleanup()
• finalize()
• During the initialization phase of a servlet, which method is called
by the servlet container to read the servlet configuration
information?
• init()
• service()
• config()
• initialize()
• In which phase of the servlet lifecycle is the service() method
repeatedly called?
• Initialization phase
• Instantiation phase
• Request handling phase
• Destruction phase
• Which method is called by the servlet container when it decides to
remove a servlet instance from the service, allowing the servlet to
perform final cleanup?
• service()
• doGet()
• init()
• destroy()
Steps to create a servlet example
• we are going to use apache tomcat server in this example.
• The steps are as follows:
• Create a directory structure
• Create a Servlet
• Compile the Servlet
• Create a deployment descriptor
• Start the server and deploy the project
• Access the servlet
1-Create a directory structures
• The directory structure defines that
where to put the different types of files so
that web container may get the
information and respond to the client.
• As you can see that the servlet class file
must be in the classes folder.
• The web.xml file must be under the WEB-
INF folder.
2-Create a Servlet
• The servlet example can be created by three ways: