• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Nishant Kumar Singh
Greenhorn
Posts: 8
Java
  • Mark post as helpful
  • send pies
  • 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>"); }

}
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66151
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Posts: 66151
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
mak pandian
Ranch Hand
Posts: 30
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • 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?
 
Luan Cestari
Ranch Hand
Posts: 172
C++ Redhat Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!