• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt in http methods

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These i got from enthuware

Q1

Question 19 of 60
Following is the code for TestServlet . Assuming that it is not preloaded or preinitialized, which of the given statements about it are correct?
(The statements are regarding the methods defined in this class.)


select any 2

a For any and every HTTP request, at the most 2 of it's methods will be called.
b For any and every request, atleast 1 of it's methods will be called.
c For any and every request, service() will be called.
d For an HTTP PUT request, none of it's methods will be called.
e For an HTTP PUT request, it'll throw an exception.

answer is bc.

but if we send any request for which doXXX method is not defined then it throws some exception.
am I correct in this?

if above two statements are true then does that mean we can avoid exception if we add the following two lines in every class?

public void service(HttpServletRequest req, HttpServletResponse res)
{
super.service();
}


thanks
geeta
 
Ranch Hand
Posts: 437
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Geeta.

super.service();

I think there is no service()(with no arguments)
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This won't even compile.
1. There's no service() method in HttpServlet. It should be service(req,res) instead.
2. ServletException and IOException have to be caught or thrown

And as you said, if doXXX method is not overriden, ServletException should be thrown.
 
Ranch Hand
Posts: 689
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

First of all you can not override service method....
 
Chinmaya Chowdary
Ranch Hand
Posts: 437
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nishan.

First of all you can not override service method....

I think we can override service() method.
See HttpServlet api, it is not declared final.
Inorder to get default service method implementation behavior, we need to call super.service(request,response) in the overridden method.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

First of all you can not override service method....



Obama wrote:Yes, we can

 
Nishan Patel
Ranch Hand
Posts: 689
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

In other words, your servlet class that extends HttpServlet should not override the service(...) method because:

You can add support for different behaviors for different request types by overriding the appropriate doXXX(...) method. This makes sense since a servlet should not return a "successful" response (i.e. status code in the 200 range) for a DELETE request unless it actually did delete the requested resource.



The default service(...) method supports modification dates (i.e.conditional GET requests) by using the getLastModified(HttpServletRequest) method (which you can override to improve performance).



You get automatic support for TRACE, OPTIONS, and HEAD requests (although you can override the doHead(...) method (in servlets 2.3+) to improve performance).



you could override service directly (to "improve" performance by reducing the number of method calls) and include the appropriate logic to replace the aforementioned functionality, but why re-invent the wheel?

And if performance is really your primary concern, never "micro-optimize" before profiling. Chances are, the code you've written has some bottlenecks that make the performance impact of these extra method calls negligible. As a matter of fact, the code you write to replace the functionality you are losing might just contain some of these bottlenecks.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic