how do you close an HTTP connection from an HttpServlet?

J

Jason

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.
 
R

Ryan Stewart

Jason said:
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?
 
J

Jason

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
 
R

Ryan Stewart

Jason said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top