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

question about doGet not present..

 
cyril vidal
Ranch Hand
Posts: 247
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Here's the following code:

In one mock exam, they ask for the type of response when the following url is asked:
http://localhost:8080/test/servlet/com.baboon.servletmodel.TestServlet?param=12
The author says blank page witthout error.
I've tested it and i received a 405 error: http method GET not supported by this URL.
So who is right, and is there any default implementation of GET method provided by the container in the case of no one is implemented by the developer?
Thanks in advance,
Regards,
Cyril.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe the 'real world' experience is the correct answer (browser receives a 405).

Any exam (mock or not) that asks you what a browser screen should look like, is IMHO, rubbish.

If you have friendly error messages turned on in IE, you'll get a different looking screen than if you have them turned off. If you're using an old netscape version, you might get a popup dialog that says "page contained no data", while other browsers present a blank page. Sometimes when you view the source, it contains an empty <html><body></body></html> other times, it's completely blank.

Grr!
 
cyril vidal
Ranch Hand
Posts: 247
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike,
thanks for your response.
What I still not understand is why an exception is generated.
Assume the following asserts are correct:
GenericServlet has an abstract service(ServletRequest, ServletResponse) method. HttpServlet extends GenericServlet and provides a concrete implementation of the service() method. It also provides the empty implementation for the doXXX() methods.
There's anyway an empty implementation of doGet method provided by the container for the class HttpServlet, so why an exception is thrown?
What is also the utility of such a default implementation?
Thanks for your lignt on this topic,
Regards,
Cyril.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have the source in front of me, so I'm not sure, but here's how I speculate it might be so:

public abstract class HttpServlet contains a whole bunch of protected methods (which are not themselves abstract).

doGet and doPost are two such methods.

their default implementation is to throw a 405 'not supported' exception.

When you subclass HttpServlet, and then provide your own doGet or doPost, you are overriding the method and 'supporting' it.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yup, i've just decompiled the HttpServlet class found in servlet.jar in Tomcat's common/lib folder.

When your servlet provides a doGet() method, but not doPost(), the supercalss doPost() is used, which will sendError(405).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic