You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Tennessee Leeuwenburg <t....@bom.gov.au> on 2005/01/07 01:48:48 UTC

Handling HEAD request in servlet

Hi guys,

I have a problem where the default implementation of HttpServlet doesn't 
seem to be handling doHead(request, response) properly. I over-rode the 
method, and found the damndest thing happening. The line 
"response.setContentLength(length)" is just getting completely ignored. 
I can't work it out - it's like the response object is deliberately 
preventing me from setting the content-length. I tried 
setHeader("Content-Length", length) just in case but still no joy.

Note - this is a repost - the tomcat-dev people said I should post here, 
despite this relating to development issues rather than install or 
deployment problems, so please don't shoot me! I worked around the 
problem by using out.println("Content-Length: length") instead, but it's 
still bad that setContentLength() doesn't work, I think.

Help!? Please!
-------------------------------------------------

Here's the code snippet :

               FileInputStream inStream = null;
                     inStream = new FileInputStream(ncFile);
               int length = (int)ncFile.length();
               response.setStatus(response.SC_PARTIAL_CONTENT); 
               response.setHeader("Accept-Ranges", "bytes");
               response.setContentLength(length);
               response.setHeader("Content-Length-Mimic", 
""+(int)ncFile.length());
               response.setHeader("Impossible", "" + length);
               response.setContentType(contentType);
                             out.flush();

Here's what I get back via telnet :

HTTP/1.1 206 Partial Content
Date: Thu, 06 Jan 2005 05:07:22 GMT
Server: Apache/2.0.50 (Ubuntu) mod_jk2/2.0.4
Set-Cookie: JSESSIONID=3D6C4C6EBE1AB1672E40C2933243BA3B; Path=/marslet
Accept-Ranges: bytes
Content-Length-Mimic: 36903060
Impossible: 36903060
Content-Type: application/x-netcdf
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: Handling HEAD request in servlet

Posted by Wade Chandler <wc...@redesetgrow.com>.
Tennessee Leeuwenburg wrote:
> Glad to find another human! :)
> 
>  From the spec, it looked like there should be a content-length, but 
> maybe you're right. I'm writing a server for a client that expects the 
> content-length to be there. As I said, I did find a workaround by just 
> printing to the output stream, but it had me baffled for a while. Lucky 
> everyone is still on holidays, there was some swearing going on :)...
> 
> Cheers,
> -T
> 
>> Yeah, you should have seen another message between me and another guy. 
>> lol.  Anyways, head responses can't have a length as they are only a 
>> header.  So basically all you can send back in a head request is the 
>> header and the header won't have a length....read the headers until 
>> you get an empty line....if that's what you are needing to do (client 
>> stuff)...server side...you can't really do anything with the head 
>> request but send headers.  I guess you can send a header for a 
>> redirect to a different page if needed.  So I'm sure tomcat is wiping 
>> it all out for you ... as it should be technically per the specification.
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 
> 
> 
No I think you should be able to set the header......Tomcat does it for 
me on my home directory index.html file.  Here is the output from the 
head operation...

HTTP/1.1 200 OK
ETag: W/"697-1078786629000"
Last-Modified: Mon, 08 Mar 2004 22:57:09 GMT
Content-Type: text/html
Content-Length: 697
Date: Fri, 07 Jan 2005 06:14:26 GMT
Server: Apache-Coyote/1.1
Connection: close

That from my Tomcat 5.0.28 ~useraccount index.html file.  I used w3m to 
perform a head.

and this from a zip file I had in there....


HTTP/1.1 200 OK
ETag: W/"1169293-1084811760000"
Last-Modified: Mon, 17 May 2004 16:36:00 GMT
Content-Type: application/zip
Content-Length: 1169293
Date: Fri, 07 Jan 2005 06:18:19 GMT
Server: Apache-Coyote/1.1
Connection: close


Which makes sense...why else would one want HEAD in the first place if 
not to examine the data a tad before trying to grab it.

Maybe don't try to set the content and just try to set the header itself 
without calling the method to set the content?  Have you already tried 
that....setting the header and leaving the content property alone? 
Maybe you are calling something else after setting the header that is 
clearing it out....?

Wade


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: Handling HEAD request in servlet

Posted by Tennessee Leeuwenburg <t....@bom.gov.au>.
Glad to find another human! :)

 From the spec, it looked like there should be a content-length, but 
maybe you're right. I'm writing a server for a client that expects the 
content-length to be there. As I said, I did find a workaround by just 
printing to the output stream, but it had me baffled for a while. Lucky 
everyone is still on holidays, there was some swearing going on :)...

Cheers,
-T

> Yeah, you should have seen another message between me and another guy. 
> lol.  Anyways, head responses can't have a length as they are only a 
> header.  So basically all you can send back in a head request is the 
> header and the header won't have a length....read the headers until 
> you get an empty line....if that's what you are needing to do (client 
> stuff)...server side...you can't really do anything with the head 
> request but send headers.  I guess you can send a header for a 
> redirect to a different page if needed.  So I'm sure tomcat is wiping 
> it all out for you ... as it should be technically per the specification.



---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: Handling HEAD request in servlet

Posted by Wade Chandler <wc...@redesetgrow.com>.
Tennessee Leeuwenburg wrote:
> Hi guys,
> 
> I have a problem where the default implementation of HttpServlet doesn't 
> seem to be handling doHead(request, response) properly. I over-rode the 
> method, and found the damndest thing happening. The line 
> "response.setContentLength(length)" is just getting completely ignored. 
> I can't work it out - it's like the response object is deliberately 
> preventing me from setting the content-length. I tried 
> setHeader("Content-Length", length) just in case but still no joy.
> 
> Note - this is a repost - the tomcat-dev people said I should post here, 
> despite this relating to development issues rather than install or 
> deployment problems, so please don't shoot me! I worked around the 
> problem by using out.println("Content-Length: length") instead, but it's 
> still bad that setContentLength() doesn't work, I think.
> 
> Help!? Please!
> -------------------------------------------------
> 
> Here's the code snippet :
> 
>               FileInputStream inStream = null;
>                     inStream = new FileInputStream(ncFile);
>               int length = (int)ncFile.length();
>               response.setStatus(response.SC_PARTIAL_CONTENT); 
>               response.setHeader("Accept-Ranges", "bytes");
>               response.setContentLength(length);
>               response.setHeader("Content-Length-Mimic", 
> ""+(int)ncFile.length());
>               response.setHeader("Impossible", "" + length);
>               response.setContentType(contentType);
>                             out.flush();
> 
> Here's what I get back via telnet :
> 
> HTTP/1.1 206 Partial Content
> Date: Thu, 06 Jan 2005 05:07:22 GMT
> Server: Apache/2.0.50 (Ubuntu) mod_jk2/2.0.4
> Set-Cookie: JSESSIONID=3D6C4C6EBE1AB1672E40C2933243BA3B; Path=/marslet
> Accept-Ranges: bytes
> Content-Length-Mimic: 36903060
> Impossible: 36903060
> Content-Type: application/x-netcdf
> Keep-Alive: timeout=15, max=100
> Connection: Keep-Alive
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 
> 
> 
Yeah, you should have seen another message between me and another guy. 
lol.  Anyways, head responses can't have a length as they are only a 
header.  So basically all you can send back in a head request is the 
header and the header won't have a length....read the headers until you 
get an empty line....if that's what you are needing to do (client 
stuff)...server side...you can't really do anything with the head 
request but send headers.  I guess you can send a header for a redirect 
to a different page if needed.  So I'm sure tomcat is wiping it all out 
for you ... as it should be technically per the specification.

Wade


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org