Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enthuware Mock Exam Question

 
Sai Surya
Ranch Hand
Posts: 463
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Here is the question I had answered wrongly from one of the mock exams fron Enthuware.

Following is the code for TestServlet (See exhibit). Assuming that it is not preloaded or preinitialized, which of the given statements about it are correct?



And here are the options.

A. For any and every HTTP request, at the most 2 of its methods will be called.
B. For any and every request, atleast 1 of its methods will be called.
C. For any and every request, service() will be called.
D. For an HTTP PUT request, none of its methods will be called
E. For an HTTP PUT request, it'll throw an exception.

I choose CE as answers, however Enthuware says correct answers are BC.

I would like to know why option E is not correct since the code doesn't override doPut() a run time exception will occur mentioning that doPut is not supported. In the explanation Enthuware says, "HttpServlet also provides default implementation for all the doXXX methods. So even if you do not implement these methods there is no compliation error."

But I remember, when we don't override doPost and post a request the servlet throws run time exception saying that doPost not supported.

Please correct me if I am wrong.
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sai Surya wrote:

But I remember, when we don't override doPost and post a request the servlet throws run time exception saying that doPost not supported.

Please correct me if I am wrong.


well as per i know HTTP status message 405 will be returned in this case. its a message not an exception.

avi sinha
 
Chinmaya Chowdary
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Sai.

Even though we mention PUT request method in form, most of the web browsers will not supports PUT requests.
 
Sai Surya
Ranch Hand
Posts: 463
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
avi sinha wrote:
well as per i know HTTP status message 405 will be returned in this case. its a message not an exception.

avi sinha


Ok I understand. 405 message returns which means method not allowed. Thanks
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that all of the methods for POST, GET, PUT... are inherited form HttpServlet class. However they are protected.
Moreover I have tested that case.

It is not important if I use:

<form action="ELServlet" method="GET" >

or

<form action="ELServlet" method="PUT" >

or

<form action="ELServlet" method="DELETE" >

Put and delete are mapped to GET method. Anybody knows something about that?
There is the distinction between POST and GET only.



 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lucas Smith wrote:
Put and delete are mapped to GET method.



What do you mean by they are mapped to GET method?
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mapped to GET - DELETE, PUT - those methods will invoke GET.
Just try it
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah i tried it. Quite Interesting.

But how is is A correct.

. For any and every HTTP request, at the most 2 of its methods will be called.

In the regular case its three methods
-init()
-service()
-doGet()/doPost()


How can it be at the most 2
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nabila Mohammad wrote:yeah i tried it. Quite Interesting.

But how is is A correct.

. For any and every HTTP request, at the most 2 of its methods will be called.

In the regular case its three methods
-init()
-service()
-doGet()/doPost()


How can it be at the most 2


init() - this method will be invoked only once - after object of the servlet's class creating.
service() and do...() are those two.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay...
Thanks for the clarification!
 
Hasnain Javed Khan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lucas.
Lucas Smith wrote:Mapped to GET - DELETE, PUT - those methods will invoke GET.
Just try it


This is not true. I decompiled the HttpServlet class and found that only doHead calls doGet. Its not mentioned in the API Docs either !

Servlet API

Hope that clears up any doubts/misunderstanding.

Kind Regards.
Hasnain.
 
Hasnain Javed Khan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Nabila,

Nabila Mohammad wrote:yeah i tried it. Quite Interesting.

But how is is A correct.

. For any and every HTTP request, at the most 2 of its methods will be called.

In the regular case its three methods
-init()
-service()
-doGet()/doPost()


How can it be at the most 2


A is not the correct option listed in the original post. A is not the correct option anyways.

Kind Regards.
Hasnain.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hasnain Javed Khan wrote:Hello Nabila,

A is not the correct option listed in the original post. A is not the correct option anyways.


oh! I didn't realize. Thanks for pointing it out.

For any and every request, atleast 1 of its methods will be called


which method are the talking about ?
init()? or Service() ?

Thought it should be at least 2.
 
Hasnain Javed Khan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nabila Mohammad wrote:
Hasnain Javed Khan wrote:Hello Nabila,

A is not the correct option listed in the original post. A is not the correct option anyways.


oh! I didn't realize. Thanks for pointing it out.

For any and every request, atleast 1 of its methods will be called


which method are the talking about ?
init()? or Service() ?

Thought it should be at least 2.


The question says that
Assuming that it is not preloaded or preinitialized


If it is not preloaded or preinitialized, the init method will be called (so -> at least 1 of its method will be called)

For the very first request , 3 methods will be called
init()
service()
doXXX()

for subsequent requests, 2 methods will be called,

service()
doXXX().


Hope that helps.

Kind Regards.
Hasnain.

 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it. Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic