Reputation: 4994
For logging purposes, I override the service method from HttpServlet like this:
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
{
logger.debug("enter servlet");
logger.debug("Request Header: {}", MyHttpTools.requestHeaderToSting(req);
super.service(req, resp);
logger.debug("leaving servlet");
logger.debug("Response Header: {}", MyHttpTools.responseHeaderToSting(resp);
}
I found some places in the web, where authors say "don't override service".
What do you think of my approach?
Upvotes: 0
Views: 848
Reputation: 8616
The default service() method in an HTTP servlet routes the request to another method based on the HTTP transfer method (POST, GET, and so on). For example, HTTP POST requests are routed to the doPost() method, HTTP GET requests are routed to the doGet() method, and so on. This enables the servlet to perform different request data processing depending on the transfer method. Since the routing takes place in service(), there is no need to generally override service() in an HTTP servlet. Instead, override doGet(), doPost(), and so on, depending on the expected request type.
For detail plz go through this article : Sun Java System Web Server 6.1 SP6 Programmer's Guide to Web Applications
Upvotes: 0
Reputation: 2043
Nothing wrong with your approach, but you could do it in a "cleaner" (in my opinion) way, e.g. by implementing (and registering) a javax.servlet.Filter
, or by using AspectJ, or other approaches that are more commonly used.
Upvotes: 1
Reputation: 73568
Nothing inherently wrong with your approach.
The authors are saying that don't override service()
for business code, override the doPost()
, doGet()
etc. methods instead.
Upvotes: 0