• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Servlet » introduction » How many ways we can develop a servlet?
Next →
← Prev

How many ways we can develop a servlet?

By Dinesh Thakur

The three important resources of servlet API.

1. javax.servlet.Servlet

2. javax.servlet.GenericServlet      (Abstract class)

3. javax.servlet.http.httpServlet    (Abstract class)

• In Java an abstract class can contain only abstract methods or only concrete methods or mix of both.

• The above give predefined HttpServlet class is an abstract class containing no abstract methods.

• If you want to make methods / logics of one java class accessible only through subclass the make that class as abstract class even though that class contains only concrete methods.

              servlet method inheritance

• Every servlet program is a java class that is developed based on servlet api. There are three ways to develop servlet program.

1. Take a java class implementing javax.servlet.Servlet interface and provide implementation for all the five methods of that interface.

2. Take java class extending from javax.servlet.GenericServlet class and provide implementation for service() method.

3. Take java class extending from javax.servlet.http.httpServlet class and override one of the seven doxxx() methods or one of the two service(- -) methods.

• In the above said three approaches the overridden / implemented service (-,-) / doxxx(-) programmer places request processing logic that generates web pages by processing the request. But programmer never calls these methods manually but servlet container calls these methods automatically for every request given by client to servlet program.

• Programmer just supplies his servlet program related java class to servlet container then onwards servlet container is responsible to manage the whole life cycle of servlet program. (From object birth to death every operation will be take care by container).

• For all the request given to servlet program the servlet container creates one object but for every request given to servlet program, the servlet container create one separate request, response objects and calls service(-,-) method by keeping these requests, response objects as argument values.

                Servlet Object

• Servlet program uses request object to read details from the request.

• Servlet program uses response object to send response content to browser window through web server.

• When 10 requests are given to a servlet program from single or different browser windows (clients)

• Servlet container creates 10 threads on servlet program objects representing 10 requests.

• Servlet container creates 10 sets of request, response objects and calls service (-,-) or methods for 10 times having request, response objects as arguments values.

• To make our servlet program java class visible to servlet container the java class must be taken as public class.

 

We’ll be covering the following topics in this tutorial:

  • The javax.servlet.Servlet Interface
  • The javax.servlet.GenericServlet Class
  • The javax.servlet.http.HttpServlet Class

The javax.servlet.Servlet Interface

 

The javax.servlet.Servlet interface is the basic interface of the Java Servlet API. This provides a standard abstraction for the servlet container to understand the Servlet object created by the user. That means the interface is designed to describe the user-defined Java object to the Servlet container, which encapsulates the application logic to plug into the servlet container for processing the client request. The servlet object must be a subtype of the interface to be managed by the servlet container. The following are the five methods declared in this interface:

 

1. public abstract void init(ServletConfig)

 

2. public abstract void service (Service Request, Service Response) throws servlet Exception, IO Exception

 

3. public abstract void destroy()

 

4. public abstract void ServletConfiggetServletConfig()

 

5. public abstract String getServletlnfo()

 

The javax.servlet.GenericServlet Class

 

The GenericServlet class implements the servlet interface and, for convenience, the ServletConfig interface. Servlet develops typically subclass GenericServlet, or its descendent HttpServlet, unless the servlet needs another class as a parent. (If a servlet does need to subclass another class, the servlet must implement the Servlet interface directly. This would be necessary when, for example, RMI or CORBA objects act as servlets.)

 

The GenericServlet class was created to make writing servlets easier. It provides simple versions of the life-cycle and ini t destroy methods, and of the methods in the ServletConfig interface. It also provides a log method, from the ServletContext interface. The servlet writer must override only the service method, which is abstract. Though not required, the servlet implementer should also override the getServletInfo method, and will want to specialize the ini t and destroy methods if expensive servlet-wide resources are to be managed. The following are the methods residing in GenericServlet class.

 

• public void destroy()

 

• public String getlnitParameter(String name)

 

• public Enumeration getlnitParameterNames()

 

• public ServletConfiggetServletConfig()

 

• public ServletContextgetServletContext()

 

• public String getServletlnfo()

 

• public void init(ServletConfigconfig) throws ServletException

 

• public void init() throws ServletException

 

• public void log (Exception e,Stringmsg)

 

• public void log (String message,Throwable t)

 

• public abstract· void service (ServletRequest req, ServletResponse res)

 

throw Servlet Exception, IOException

 

The javax.servlet.http.HttpServlet Class

 

It is an abstract class that simplifies writing HTTP servlets. It extends the GenericServlet base class and provides a framework for handling the HTTP protocol. Because it is an abstract class, servlet writers must subclass it and override at least one method. The methods normally overridden are;

 

• doGet, if HTTP GET requests are supported. Overriding the doGet method automatically also provides support for the HEAD and conditional GET operations. Where practical, the getLastModified method should also be overridden, to facilitate caching the HTTP response data. This improves performance by enabling smarter conditional GET support.

 

• doPost, if HTTP POST requests are supported.

 

• doPut, if HTTP PUT requests are supported.

 

• doDelete, if HTTP DELETE requests are supported.

 

• life cycle methods-in it and destroy, if the servlet writer needs to manage resources that are held for the lifetime of the servlet. Servlets that do not manage resources do not need to specialize these methods.

 

• getServletInfo, To provide descriptive information through a service’s administrative interfaces.

 

Notice that the service method is not typically overridden. The service method, as provided, supports standard HTTP requests by dispatching them to appropriate methods, such as the methods listed above that have the prefix “do”, In addition, the service method also supports the HTTP1.l protocol’s TRACE and OPTIONS methods by dispatching to the doTrace and doOptions methods. The doTrace and doOptions methods are not typically overridden.

 

Servlets typically run inside multi-threaded servers; servlets must be written to handle multiple service requests simultaneously. It is the servlet writer’s responsibility to synchronize access to any shared resources. Such resources include in-memory data such as instance or class variables of the servlet, as well as external components such as files, database and network connections.

You’ll also like:

  1. What is the Servlet container raises the life cycle events in servlet program?
  2. Servlet Chaining Between Two Servlet Programs
  3. What is Servlet?
  4. Java Servlet Life Cycle
  5. Html to Servlet Communication
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Servlet Tutorials

Servlet Tutorials

  • Servlet - Home
  • Servlet - Types
  • Servlet - Advantages
  • Servlet - Container
  • Servlet - API
  • Servlet - Chaining
  • Servlet - Life Cycle
  • Servlet - Developement Way
  • Servlet - Servlet Vs CGI
  • Servlet - Server Side
  • Servlet - HttpServletRequest
  • Servlet - Cookies Advantages
  • Servlet - JDBC Architecture
  • Servlet - HttpServlet
  • Servlet - HttpServletResponse
  • Servlet - Web Technology
  • Servlet - Applet Communication
  • Servlet - Html Communication
  • Servlet - HTTP POST Request
  • Servlet - HTTP GET Request

Other Links

  • Servlet - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW