Image Image Image Image Image Image Image Image Image Image

Mudassir Shahzad | August 21, 2017

Scroll to top

Top

2 Comments

Two service methods in HttpServlet class

Two service methods in HttpServlet class
Mudassir Shahzad

Whenever we write a servlet, we inherit our class from HttpServlet which contains two service methods. Let’s have a look at the difference between both.

This article assumes that you have a basic knowledge of method overriding and overloading.

If you don’t, GET A LIFE!

So, getting on with the article.

Inheriting from HttpServlet enables us to reuse the methods specified in it.
1

 

which in turn inherits from GenericServlet class

2

 

So, the hierarchy becomes

3

 

Our MyServlet inherits HttpServlet which inherits GenericServlet.

For now, just keep that in mind and we will get back to it.

 

Overview of the methods

 

Now, HttpServlet is an Abstract class and its all methods are concrete.

HttpServlet is derived from GenericServlet which is also an abstract class.

HttpServlet has two service methods.

One public and one protected.

The public method is inherited from the GenericServlet class.

HttpServlet extends GenericServlet, remember ?

The public method has two parameters; ServletRequest and ServletResponse.

The protected method overloads the public method and it also has two parameters; HttpServletRequest and HttpServletResponse.

 

 

This is an excellent example of Dynamic Polymorphism (overriding) and Static Polymorphism (overloading).

 

How the container works with them

 

The typical call to a servlet is:

The container calls the public service method(the one inherited from GenericServlet)

The service method internally typecasts the ServletRequest to HttpServletRequest and ServletResponse to HttpServletResponse.

The protected service method then internally verifies if it is GET or POST and promptly calls doGet() or doPost().

Not getting it ?

Have a look at it this way

4

Container receives request and calls the public service method which in turn calls the protected service method. Now, if the method is get, it calls the doGet and if it is post, it calls the doPost.

Now, the question may arise in mind

Why doesn’t the public service method do the job that the protected method is doing?

The answer is, separation of concerns. Generic method or public method handles the generic functionality and the protected method is an HTTP-specific version of the public method. It handles the HTTP-specific functionality.

 

 

References

 

official java docs

http://www.coderanch.com/t/362259/Servlets/java/service-methods-HttpServlet

 

Help by sharing!!

Related posts:

Comments

  1. Utsav Garg

    same job could have been done in the public service method as well by typecasting those req,res parameters into http types and by providing all that if else ladder to determine which method (doGet(), doPost() etc etc) can be called.
    The question still remains the same…..WHY DIFFERENT METHOD WHEN THE SAME COULD HAVE BEEN ACHIEVED UNDER THE SINGLE METHOD?

  2. Mudassir Shahzad

    The main reason is SEPARATION OF CONCERNS (also given in the post). To separate the generic and http specific functionality.

Submit a Comment

Cancel reply

not found