0% found this document useful (0 votes)
68 views9 pages

Introduction To Servlet

This document provides an introduction to servlets, including: 1) Servlets are Java programs that run on a web server to develop dynamic web applications. 2) The servlet lifecycle includes loading, instantiation, initialization via init(), repeated execution of service() for requests, and destruction via destroy() upon server shutdown. 3) Servlets receive HTTP requests, access request information, and format responses via servlet and HTTP servlet request and response objects.

Uploaded by

Ajay Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views9 pages

Introduction To Servlet

This document provides an introduction to servlets, including: 1) Servlets are Java programs that run on a web server to develop dynamic web applications. 2) The servlet lifecycle includes loading, instantiation, initialization via init(), repeated execution of service() for requests, and destruction via destroy() upon server shutdown. 3) Servlets receive HTTP requests, access request information, and format responses via servlet and HTTP servlet request and response objects.

Uploaded by

Ajay Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Introduction to Servlet

Dept. of B. Voc.(Software Development and System Administration)


St. Joseph’s College(Autonomous)
Trichy-02
By
Dr. J. Ronald Martin

Introduction to Servlet : Dept. of B. Voc. (SD&SA) 1


Servlet

 Servlet is a java program that runs inside JVM on the web


server.

 It is used for developing dynamic web applications.

Introduction to Servlet : Dept. of B.Voc SD&SA 2


Life Cycle of Servlet

Servlet life cycle contains five steps:

1. Loading of Servlet
2. Creating instance of Servlet
3. Invoke init() once
4. Invoke service() repeatedly for each client request
5. Invoke destroy()

Introduction to Servlet : Dept. of B.Voc SD&SA 3


Step 1: Loading of Servlet

When the web server (e.g. Apache Tomcat) starts up, the servlet container
deploy and loads all the servlets.

Step 2: Creating instance of Servlet

Once all the Servlet classes loaded, the servlet container creates instances
of each servlet class. Servlet container creates only once instance per
servlet class and all the requests to the servlet are executed on the same
servlet instance.

Step 3: Invoke init() method

Once all the servlet classes are instantiated, the init() method is invoked for
each instantiated servlet.
Introduction to Servlet : Dept. of B.Voc SD&SA 4
Step 4: Invoke service() method

Each time the web server receives a request for servlet, it spawns a new
thread that calls service() method.

If the servlet is GenericServlet then the request is served by the service()


method itself,

if the servlet is HttpServlet then service() method receives the request and
dispatches it to the correct handler method based on the type of request.

Step 5: Invoke destroy() method

When servlet container shuts down(this usually happens when we stop


the web server), it unloads all the servlets and calls destroy() method for
each initialized servlets.
Introduction to Servlet : Dept. of B.Voc SD&SA 5
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.
Introduction to Servlet : Dept. of B.Voc SD&SA 6
Introduction to Servlet : Dept. of B.Voc SD&SA 7
How Servlet Works?
1) When the web server (e.g. Apache Tomcat) starts up, the servlet container
deploy and loads all the servlets.

2) Once the servlet is loaded, the servlet container creates the instance of
servlet class. For each instantiated servlet, its init() method is invoked.

3) Client (user browser) sends an Http request to web server on a certain


port. Each time the web server receives a request, the servlet container
creates HttpServletRequest and HttpServletResponse objects. The
HttpServletRequest object provides the access to the request information
and the HttpServletResponse object allows us to format and change the
http response before sending it to the client.

4) When servlet container shuts down, it unloads all the servlets and calls
destroy() method for each initialized
Introduction to Servletservlets.
: Dept. of B.Voc SD&SA 8
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class DemoServlet extends HttpServlet{

public void doGet(HttpServletRequest req,HttpServletResponse res)


throws ServletException,IOException

{
res.setContentType("text/html");//setting the content type
PrintWriter pw=res.getWriter();//get the stream to write the data

//writing html in the stream


pw.println("<html><body>");
pw.println("Welcome to servlet");
pw.println("</body></html>");

pw.close();//closing the stream


}

}
Introduction to Servlet : Dept. of B.Voc SD&SA 9

You might also like