Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Please explain why overriding service() method in Servlet is not good idea?

 
Greenhorn
Posts: 8
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain why overriding service() method in Servlet is not good idea? Then why & how the following code prints 'Hello world'

public class HelloWorld extends HttpServlet {
private static final long serialVersionUID = 1L;

public HelloWorld() {
super();
}

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<html>");
pw.println("<head><title>Hello world</title></title>");
pw.println("<body>");
pw.println("<h1>Hello World</h1>");
pw.println("</body></html>"); }

}
 
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, please explain why you think it is a good idea.
 
Nishant Kumar Singh
Greenhorn
Posts: 8
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:First, please explain why you think it is a good idea.



Hello Sir,

I haven't asked this question thinking that overriding Service() method of servlet is good idea.But after going through some books and interviews , i came to know that overriding service() method would take away the functionality provided by HttpServlet class and doXXX() methods will not be called automatically. So is it the only reason that we do not override service() method ??? And which service() method is called by container ,if i override the service() method??

 
Bear Bibeault
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if you override service() then the override would be called. That's the way that Java works.

There is rarely any good reason to override service(). The base class does a good job of determining the HTTP method (GET, POST, DELETE, etc) and calling the appropriate do method. Why would you want to override that?

And if the answer is: "so that GET and POST (and others) can all do the same job", then it's a bad answer. It's very rare for it to be a good idea to have a GET and a POST do the same thing.
 
Ranch Hand
Posts: 30
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you do so, whatever type(GET, POST etc) request the servlet receives, it would send the same response that you wrote in service method. This gives us low productivity. Do you agree?
 
Ranch Hand
Posts: 172
Redhat Ruby C++
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to point what people are saying that you are overriding:
http://grepcode.com/file/repo1.maven.org/maven2/javax.servlet/servlet-api/2.5/javax/servlet/http/HttpServlet.java#HttpServlet.service%28javax.servlet.http.HttpServletRequest%2Cjavax.servlet.http.HttpServletResponse%29

And I agree with the previously comments.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic