This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Service, SevletConfig and ServletContext

 
Sriram Sharma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why shouldn't we override service, getServletContext and getServletConfig methods?
Any reason for this? What will hapen if we override?

Regards,
Sriram
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. service method is common to all HTTP methods like GET, POST, DELETE etc. It gets called whatever the HTTP method of the request is. The default implementation of service method delegates the request to the correct handler for that HTTP method like doGet for GET, doPost for POST etc. If you override the service method, you'll handle all type of HTTP methods with the same code. This might not be correct behavior. Suppose the user sends a DELETE request, then also you'll service that request in a normal form like sending some information etc. But the intention of DELETE requests is to delete the resource.

2. If you want to override the getServletContext and getServletConfig method, you'll also have to override the init(ServletConfig conf) method. The default implementation in the GenericServlet class handles all these details nicely. So normally you only override the init() method without any parameters. This is what the GenericServlet class usually does for you



Implementing this again manually is just a waste of effort...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic