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

query about doHead() method

 
Sudarshan Sreenivasan
Ranch Hand
Posts: 188
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is written abotut the doHead() method in the servlet api


IReceives an HTTP HEAD request from the protected service method and handles the request. The client sends a HEAD request when it wants to see only the headers of a response, such as Content-Type or Content-Length. The HTTP HEAD method counts the output bytes in the response to set the Content-Length header accurately.

If you override this method, you can avoid computing the response body and just set the response headers directly to improve performance. Make sure that the doHead method you write is both safe and idempotent (that is, protects itself from being called multiple times for one HTTP HEAD request).

If the HTTP HEAD request is incorrectly formatted, doHead returns an HTTP "Bad Request" message.




what if we dont override the doHead() method ... & keep the method in the html as method = head then will the client evaluate the response body ?? ... i just can not follow what is tryin to be said in the api ... some one please explain !!! with a example if possible




Thanks
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For starters, you can't use the HEAD method in an HTML form. Forms only recognize GET and POST. That doesn't mean HEAD is useless, though - the browser itself could send a HEAD request, or it could be sent by the JavaScript XmlHttpRequest object.

According to the HTTP spec, HEAD is identical to GET, except that the response body is not returned. What the javadocs mean is that the default implementation will -when it receives a HEAD request- execute it as a GET request, but then throw away the response body which would have been sent, and just return the response header.

But executing the GET may be unnecessary - the server-side code may have all the information needed for creating the response headers. In that case, the doHead method should be overridden to NOT execute the GET, but just return those headers.

The whole concept is a bit theoretical, because HEAD isn't used by HTML, so it's not easy to see what is going on, and why. One use for HEAD might be an intelligent browser that is being asked to retrieve via GET a large resource (maybe a 50MB file). If the browser has the file in the cache, it wouldn't need to download it again, but it would need to make sure that the file hasn't changed since the download. That's what a HEAD might be used for, because the response headers would contain the date on which the file was last modified.

Does that make things clearer?
[ July 21, 2007: Message edited by: Ulf Dittmer ]
 
Sudarshan Sreenivasan
Ranch Hand
Posts: 188
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it thanks a lot ..... was trying to use head as a method in the html ... that was getting me confused
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic