File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Empty Servlet Class - Niko's mock exam question

 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is from "Niko's Free SCWCD Mock Exam for Servlets"

18.What happens when we compile and deploy this servlet? (1 correct answer)
public class Test extends HttpServlet {

}
a.Compilation fails because there is no init() method defined.
b.An exception is thrown at runtime because service() has no method to call!
c.Deployment succeeds but we get a message 'GET is not supported by this URL' if we access it.

The answer given is c. I was thinking it would be b, since a ServletException would be thrown. (Also, not sure if the two answers are mutually exclusive. Wouldn't the accurate answer be "Deployment succeeds, but an exception is thrown at runtime because service() has no method to call.")
 
Piotr Nowicki
Ranch Hand
Posts: 610
IntelliJ IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nidhi!

The doGet(-), doPost(-), ... methods doesn't need to be provided to compile and execute your Servlet if you extend the HttpServlet class.

In the matter of fact, what you normally do is override methods previously defined in HttpServlet itself. Therefore, the service() always knows what method to call - it just uses the very basic implementation from the super class (HttpServlet) which returns 'Operation not supported' type of information.
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. That really makes it clear.
 
Piotr Nowicki
Ranch Hand
Posts: 610
IntelliJ IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad I could help :-)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic