What is the purpose of the Servlet protected void service method and why can’t you override it?

Asked

Viewed 86 times

0

Would you like to know why you can’t overwrite the service method? In the book he says you shouldn’t overwrite it, but why? But if I use the super.service(request, sponse) I won’t be calling the method above what I am overwriting and with that I will perform beyond my superscript service I will perform the root method? I say this, because I want to instantiate my classes within the service, because every user request to that service is invoked and instates my objects, having no problem of a value of a user x intervene in the value of user y. These instaciated classes are responsible for communicating with DAO and Servlet(It is an intermediate class).

1 answer

2

You are in contradiction because first you say that you cannot overwrite the method service() and then says he calls it superscript.

It may be overwritten, but that’s not necessary.

As says the documentation:

Receives standard HTTP requests from the public service method and dispatches them to the doXXX methods defined in this class. This method is an HTTP-specific version of the Servlet.service(javax.servlet.Servletrequest, javax.servlet.Servletresponse) method. There’s no need to override this method.

In other words, the service() is the method that is effectively called to meet the requests that customers make for Servlet. However he is not ultimately responsible for the service: he also forwards the calls to methods such as doGet() or doPost(), these yes can be overridden by you (to meet GET or POST requests respectively).

The architecture is basically this, there is not much to explain. Remembering that 99.9% of the time you will be working with subclasses of HttpServlet and not of Servlet.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.