Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   how do you close an HTTP connection from an HttpServlet? (http://www.velocityreviews.com/forums/t141063-how-do-you-close-an-http-connection-from-an-httpservlet.html)

Jason 02-16-2005 12:32 AM

how do you close an HTTP connection from an HttpServlet?
 
When implementing a javax.servlet.http.HttpServlet, how do you override
the connector and close the HTTP connection? I've noticed the
following:

In Tomcat's server.xml, you can prevent any connection from being kept
open by setting maxKeepAliveRequests="1" in the Connector element.
This doesn't help me, though: in my server application, the application
knows when the user is done & when it's reasonable to close the
connection, but this isn't something you could configure in the
server.xml.

Also, I've noticed that you can manually do an
HttpServletResponse.setHeader("Connection", "close"), but this is
completely ignored by the connector. It leaves the TCP connection open
(hopefully the client will eventually close it), and in situations when
it does close the connection (such as an HTTP/1.0 req without
keep-alive) it appends a second "Connection: close".

There must be a way to do this, but I'm not seeing it.


Ryan Stewart 02-16-2005 01:47 AM

Re: how do you close an HTTP connection from an HttpServlet?
 
"Jason" <jason_c_costa@yahoo.com> wrote in message
news:1108513926.575893.139400@z14g2000cwz.googlegr oups.com...
> When implementing a javax.servlet.http.HttpServlet, how do you override
> the connector and close the HTTP connection? I've noticed the
> following:
>
> In Tomcat's server.xml, you can prevent any connection from being kept
> open by setting maxKeepAliveRequests="1" in the Connector element.
> This doesn't help me, though: in my server application, the application
> knows when the user is done & when it's reasonable to close the
> connection, but this isn't something you could configure in the
> server.xml.
>
> Also, I've noticed that you can manually do an
> HttpServletResponse.setHeader("Connection", "close"), but this is
> completely ignored by the connector. It leaves the TCP connection open
> (hopefully the client will eventually close it), and in situations when
> it does close the connection (such as an HTTP/1.0 req without
> keep-alive) it appends a second "Connection: close".
>
> There must be a way to do this, but I'm not seeing it.
>

Well first, you wouldn't just close the connection. You *have* to send a
response, so you shouldn't actually close the connection *in* the Servlet. Now
according to RFC 2616: "If either the client or the server sends the close token
in the Connection header, that request becomes the last one for the connection."
So in theory, after sending a Connection: close, that connection shouldn't be
around much longer, but it's not the server that closes it. It's the client. I
don't think the RFC says exactly when it should be closed. Have you checked how
long the connection stays open after sending Connection: close? As for a second
Connection header being appended, that seems odd and perhaps a bug. Is that
Tomcat? Which version?



Jason 02-16-2005 02:45 AM

Re: how do you close an HTTP connection from an HttpServlet?
 
When I say "close the connection", I mean close the HTTP connection by
sending a response with the "Connection: close" header and then closing
the TCP connection. This is what Tomcat does when you set
maxKeepAliveRequests="1" in server.xml, or when you send it an HTTP/1.0
request. I just want a way to do this programatically from the servlet
code.

"Have you checked how
long the connection stays open after sending Connection: close?"

You mean how long until the server forcibly closes the connection, if
it doesn't hear back from the client? That's configurable. Factory
setting is 20 seconds.

"As for a second
Connection header being appended, that seems odd and perhaps a bug. Is
that
Tomcat? Which version?"

5.0.28


Ryan Stewart 02-20-2005 08:05 PM

Re: how do you close an HTTP connection from an HttpServlet?
 
"Jason" <jason_c_costa@yahoo.com> wrote in message
news:1108521902.029358.121290@f14g2000cwb.googlegr oups.com...
> When I say "close the connection", I mean close the HTTP connection by
> sending a response with the "Connection: close" header and then closing
> the TCP connection. This is what Tomcat does when you set
> maxKeepAliveRequests="1" in server.xml, or when you send it an HTTP/1.0
> request. I just want a way to do this programatically from the servlet
> code.
>
> "Have you checked how
> long the connection stays open after sending Connection: close?"
>
> You mean how long until the server forcibly closes the connection, if
> it doesn't hear back from the client? That's configurable. Factory
> setting is 20 seconds.
>
> "As for a second
> Connection header being appended, that seems odd and perhaps a bug. Is
> that
> Tomcat? Which version?"
>
> 5.0.28
>

Using Tomcat 5.5.4, when I send a response header of "Connection: close" to
Firefox or IE, the connection being used is immediately closed. Firefox seems to
open another connection right after the old one is closed for some reason. IE
doesn't. I can't tell you at this point whether it's the server or the browser
doing the closing. That's just from some simple debugging using netstat.




All times are GMT. The time now is 02:47 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57