BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Servlet Architecture: Basics of Servlets

Last Updated: September 10, 2022 by Chaitanya Singh | Filed Under: Java Servlet tutorial

A Servlet is a class, which implements the javax.servlet.Servlet interface. However instead of directly implementing the javax.servlet.Servlet interface we extend a class that has implemented the interface like javax.servlet.GenericServlet or javax.servlet.http.HttpServlet.

servlet-architecture

Servlet Exceution

This is how a servlet execution takes place when client (browser) makes a request to the webserver.

servlet architecture diagram2

Servlet architecture includes:

a) Servlet Interface
To write a servlet we need to implement Servlet interface. Servlet interface can be implemented directly or indirectly by extending GenericServlet or HttpServlet class.

b) Request handling methods
There are 3 methods defined in Servlet interface: init(), service() and destroy().

The first time a servlet is invoked, the init method is called. It is called only once during the lifetime of a servlet. So, we can put all your initialization code here.

The Service method is used for handling the client request. As the client request reaches to the container it creates a thread of the servlet object, and request and response object are also created. These request and response object are then passed as parameter to the service method, which then process the client request. The service method in turn calls the doGet or doPost methods (if the user has extended the class from HttpServlet ).

c) Number of instances

Basic Structure of a Servlet

public class firstServlet extends HttpServlet {
   public void init() {
      /* Put your initialization code in this method, 
       * as this method is called only once */
   }
   public void service() {
      // Service request for Servlet
   }
   public void destroy() {
      // For taking the servlet out of service, this method is called only once
   }
}

Top Related Articles:

  1. Servlet Interface explained with Example
  2. HttpServlet class with example
  3. Servlet Class Hierarchy
  4. Working of Servlet
  5. How to use load-on-startup tag in web.xml file with Example

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Abhinav says

    October 9, 2015 at 2:29 PM

    This is only one site and knowledge area where i got every solution with high commands thanking you and have a great wishes to your site.. ☺️☺️

    Reply
  2. swetha says

    December 3, 2015 at 12:30 PM

    what is the difference between servlets and webservices

    Reply
    • Aravind Reddy says

      April 29, 2016 at 5:32 PM

      1. Servlets provide webservices.
      2. webservices in the sense services provided by the server containing servlet container and in that servlet container we have webapplications. And webapplications may contain different servlets in order to retrive dynamic data and also process the data provided by the user.

      Reply
  3. Sonym says

    May 25, 2016 at 9:40 PM

    Thank You so much for the answer . This is the best answer i got, because it explained along with diagram and besides that the english is simple, very easy to understand ,,, It helps me alot !

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Servlet Tutorial

  • Servlet Introduction
  • Servlet API
  • Servlet Interface
  • Generic Servlet
  • HttpServlet
  • Servlet in Eclipse
  • Servlet Life cycle
  • Working of Servlet
  • Welcome-file-list
  • load-on-startup tag
  • ServletRequest
  • RequestDispatcher
  • ServletConfig
  • ServletContext
  • ServletResponse
  • HttpSession
  • Cookies
  • Servlet Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap