how do you close an HTTP connection from an HttpServlet?

Discussion in 'Java' started by Jason, Feb 16, 2005.

  1. Jason

    Jason Guest

    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.
     
    Jason, Feb 16, 2005
    #1
    1. Advertising

  2. Jason

    Ryan Stewart Guest

    "Jason" <> wrote in message
    news:...
    > 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?
     
    Ryan Stewart, Feb 16, 2005
    #2
    1. Advertising

  3. Jason

    Jason Guest

    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
     
    Jason, Feb 16, 2005
    #3
  4. Jason

    Ryan Stewart Guest

    "Jason" <> wrote in message
    news:...
    > 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.
     
    Ryan Stewart, Feb 20, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.amnet.net.au
    Replies:
    1
    Views:
    924
    Sudsy
    Sep 15, 2004
  2. Paul van Rossem
    Replies:
    0
    Views:
    604
    Paul van Rossem
    Apr 7, 2005
  3. tshad
    Replies:
    1
    Views:
    467
    Scott M.
    Sep 6, 2006
  4. S_K
    Replies:
    6
    Views:
    678
    Russell
    Dec 7, 2007
  5. Iñaki Baz Castillo
    Replies:
    7
    Views:
    869
    Iñaki Baz Castillo
    Jan 12, 2010
Loading...

Share This Page