cant flush servlet response? - Weblogic

This is a discussion on cant flush servlet response? - Weblogic ; i am trying to have a servlet read and write a response, close the http connection, and then do something else after the http connection has been closed. the servlet writes the output and closes the output stream, but the ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: cant flush servlet response?

  1. cant flush servlet response?

    i am trying to have a servlet read and write a response, close the http
    connection, and then do something else after the http connection has
    been closed.

    the servlet writes the output and closes the output stream, but the
    client still hangs waiting to read until the servlet returns from the
    doPost method. the input is only read by the client when the servlet
    returns, rather than when the output has been written and flushed by the
    servlet.

    is there a way to flush or close the connection before the servlet returns?

    i am in weblogic 7 sp2 on windows 2000.


    ------the servlet code is like:

    StringBuffer input = new StringBuffer();
    InputStream is = req.getInputStream();
    for(int i = in.read(); i != -1; i = in.read()) input.append((char)i);
    is.close();

    OutputStream outStream = null;
    outStream = res.getOutputStream();
    outStream.write(respText.getBytes());
    outStream.flush();
    outStream.close();

    /// so some other stuff....


    ----the client code is like:

    URL url = new URL("http://localhost:7501/ysjmx/ysservice");
    HttpURLConnection con = (HttpURLConnection)url.openConnection();
    con.setDoInput(true);
    con.setDoOutput(true);

    java.io.OutputStream out = null;
    java.io.InputStream in = null;
    try {
    out = con.getOutputStream();
    out.write("test".getBytes());
    out.close();

    in = con.getInputStream();
    StringBuffer sb = new StringBuffer();

    // it hangs here until the servlet returns even though the
    // response has been written by the servlet
    for(int i = in.read(); i != -1; i = in.read()) sb.append((char)i);
    in.close();

    } finally {
    con.disconnect();
    }


  2. Re: cant flush servlet response?

    Try setting a contentLength and then call flush after the said amount
    has been written to the outputstream..

    ---

    I would suggest another foolproof approach for doing such long-running
    tasks if you are really interested:

    Use weblogic.servlet.FutureResponseServlet paired with
    weblogic.servlet.FutureServletResponse..


    Heres a brief overview :

    public LongRunningServlet extends FutureResponseServlet {

    public void service(HttpServletRequest req, FutureServletResponse res)
    throws IOException, ServletException {

    // write response ...
    OutputStream out = res.getOutputStream();
    out.write(....);
    ....
    res.send(); // This sends your response to the client immediately

    -----------
    // some other task..... Also note that this part
    // would be blocking an execute thread , so make
    // sure that the task completes in a short while.
    ------------
    }

    Hope that helps
    --
    Nagesh

    Simon Evans wrote:
    > i am trying to have a servlet read and write a response, close the http
    > connection, and then do something else after the http connection has
    > been closed.
    >
    > the servlet writes the output and closes the output stream, but the
    > client still hangs waiting to read until the servlet returns from the
    > doPost method. the input is only read by the client when the servlet
    > returns, rather than when the output has been written and flushed by the
    > servlet.
    >
    > is there a way to flush or close the connection before the servlet returns?
    >
    > i am in weblogic 7 sp2 on windows 2000.
    >
    >
    > ------the servlet code is like:
    >
    > StringBuffer input = new StringBuffer();
    > InputStream is = req.getInputStream();
    > for(int i = in.read(); i != -1; i = in.read()) input.append((char)i);
    > is.close();
    >
    > OutputStream outStream = null;
    > outStream = res.getOutputStream();
    > outStream.write(respText.getBytes());
    > outStream.flush();
    > outStream.close();
    >
    > /// so some other stuff....
    >
    >
    > ----the client code is like:
    >
    > URL url = new URL("http://localhost:7501/ysjmx/ysservice");
    > HttpURLConnection con = (HttpURLConnection)url.openConnection();
    > con.setDoInput(true);
    > con.setDoOutput(true);
    >
    > java.io.OutputStream out = null;
    > java.io.InputStream in = null;
    > try {
    > out = con.getOutputStream();
    > out.write("test".getBytes());
    > out.close();
    >
    > in = con.getInputStream();
    > StringBuffer sb = new StringBuffer();
    >
    > // it hangs here until the servlet returns even though the
    > // response has been written by the servlet
    > for(int i = in.read(); i != -1; i = in.read()) sb.append((char)i);
    > in.close();
    >
    > } finally {
    > con.disconnect();
    > }
    >



  3. Re: cant flush servlet response?

    thanks, i have tried everything like setting the contentLength, calling
    res.flushBuffer() etc. i thought that it may be because of a keep-alive?.

    its seems wierd that weblogic does this, i think the spec says the
    connection should be closed. i might have to use JMS unless you have any
    other ideas.



    Nagesh Susarla wrote:
    > Try setting a contentLength and then call flush after the said amount
    > has been written to the outputstream..
    >
    > ---
    >
    > I would suggest another foolproof approach for doing such long-running
    > tasks if you are really interested:
    >
    > Use weblogic.servlet.FutureResponseServlet paired with
    > weblogic.servlet.FutureServletResponse..
    >
    >
    > Heres a brief overview :
    >
    > public LongRunningServlet extends FutureResponseServlet {
    >
    > public void service(HttpServletRequest req, FutureServletResponse res)
    > throws IOException, ServletException {
    >
    > // write response ...
    > OutputStream out = res.getOutputStream();
    > out.write(....);
    > ....
    > res.send(); // This sends your response to the client immediately
    >
    > -----------
    > // some other task..... Also note that this part
    > // would be blocking an execute thread , so make
    > // sure that the task completes in a short while.
    > ------------
    > }
    >
    > Hope that helps
    > --
    > Nagesh
    >
    > Simon Evans wrote:
    >
    >> i am trying to have a servlet read and write a response, close the
    >> http connection, and then do something else after the http connection
    >> has been closed.
    >>
    >> the servlet writes the output and closes the output stream, but the
    >> client still hangs waiting to read until the servlet returns from the
    >> doPost method. the input is only read by the client when the servlet
    >> returns, rather than when the output has been written and flushed by
    >> the servlet.
    >>
    >> is there a way to flush or close the connection before the servlet
    >> returns?
    >>
    >> i am in weblogic 7 sp2 on windows 2000.
    >>
    >>
    >> ------the servlet code is like:
    >>
    >> StringBuffer input = new StringBuffer();
    >> InputStream is = req.getInputStream();
    >> for(int i = in.read(); i != -1; i = in.read()) input.append((char)i);
    >> is.close();
    >>
    >> OutputStream outStream = null;
    >> outStream = res.getOutputStream();
    >> outStream.write(respText.getBytes());
    >> outStream.flush();
    >> outStream.close();
    >>
    >> /// so some other stuff....
    >>
    >>
    >> ----the client code is like:
    >>
    >> URL url = new URL("http://localhost:7501/ysjmx/ysservice");
    >> HttpURLConnection con = (HttpURLConnection)url.openConnection();
    >> con.setDoInput(true);
    >> con.setDoOutput(true);
    >>
    >> java.io.OutputStream out = null;
    >> java.io.InputStream in = null;
    >> try {
    >> out = con.getOutputStream();
    >> out.write("test".getBytes());
    >> out.close();
    >>
    >> in = con.getInputStream();
    >> StringBuffer sb = new StringBuffer();
    >>
    >> // it hangs here until the servlet returns even though the
    >> // response has been written by the servlet
    >> for(int i = in.read(); i != -1; i = in.read()) sb.append((char)i);
    >> in.close();
    >>
    >> } finally {
    >> con.disconnect();
    >> }
    >>

    >



  4. Re: cant flush servlet response?

    i guess i need to keep the code app server independant, so i cant use
    the future servlet.



    Nagesh Susarla wrote:

    > Try setting a contentLength and then call flush after the said amount
    > has been written to the outputstream..
    >
    > ---
    >
    > I would suggest another foolproof approach for doing such long-running
    > tasks if you are really interested:
    >
    > Use weblogic.servlet.FutureResponseServlet paired with
    > weblogic.servlet.FutureServletResponse..
    >
    >
    > Heres a brief overview :
    >
    > public LongRunningServlet extends FutureResponseServlet {
    >
    > public void service(HttpServletRequest req, FutureServletResponse res)
    > throws IOException, ServletException {
    >
    > // write response ...
    > OutputStream out = res.getOutputStream();
    > out.write(....);
    > ....
    > res.send(); // This sends your response to the client immediately
    >
    > -----------
    > // some other task..... Also note that this part
    > // would be blocking an execute thread , so make
    > // sure that the task completes in a short while.
    > ------------
    > }
    >
    > Hope that helps
    > --
    > Nagesh
    >
    > Simon Evans wrote:
    >
    >> i am trying to have a servlet read and write a response, close the
    >> http connection, and then do something else after the http connection
    >> has been closed.
    >>
    >> the servlet writes the output and closes the output stream, but the
    >> client still hangs waiting to read until the servlet returns from the
    >> doPost method. the input is only read by the client when the servlet
    >> returns, rather than when the output has been written and flushed by
    >> the servlet.
    >>
    >> is there a way to flush or close the connection before the servlet
    >> returns?
    >>
    >> i am in weblogic 7 sp2 on windows 2000.
    >>
    >>
    >> ------the servlet code is like:
    >>
    >> StringBuffer input = new StringBuffer();
    >> InputStream is = req.getInputStream();
    >> for(int i = in.read(); i != -1; i = in.read()) input.append((char)i);
    >> is.close();
    >>
    >> OutputStream outStream = null;
    >> outStream = res.getOutputStream();
    >> outStream.write(respText.getBytes());
    >> outStream.flush();
    >> outStream.close();
    >>
    >> /// so some other stuff....
    >>
    >>
    >> ----the client code is like:
    >>
    >> URL url = new URL("http://localhost:7501/ysjmx/ysservice");
    >> HttpURLConnection con = (HttpURLConnection)url.openConnection();
    >> con.setDoInput(true);
    >> con.setDoOutput(true);
    >>
    >> java.io.OutputStream out = null;
    >> java.io.InputStream in = null;
    >> try {
    >> out = con.getOutputStream();
    >> out.write("test".getBytes());
    >> out.close();
    >>
    >> in = con.getInputStream();
    >> StringBuffer sb = new StringBuffer();
    >>
    >> // it hangs here until the servlet returns even though the
    >> // response has been written by the servlet
    >> for(int i = in.read(); i != -1; i = in.read()) sb.append((char)i);
    >> in.close();
    >>
    >> } finally {
    >> con.disconnect();
    >> }
    >>

    >



  5. Re: cant flush servlet response?

    Simon Evans wrote:
    > thanks, i have tried everything like setting the contentLength, calling
    > res.flushBuffer() etc. i thought that it may be because of a keep-alive?.


    Seems like this is because of the keepalive req from the client. If you
    dont care about keep-alive and want to really have the client terminate
    the connection, you could add the following header to your request :

    urlConnection.setRequestProperty("Connection", "close");

    --
    Nagesh
    >
    > its seems wierd that weblogic does this, i think the spec says the
    > connection should be closed. i might have to use JMS unless you have any
    > other ideas.
    >
    >
    >
    > Nagesh Susarla wrote:
    >
    >> Try setting a contentLength and then call flush after the said amount
    >> has been written to the outputstream..
    >>
    >> ---
    >>
    >> I would suggest another foolproof approach for doing such long-running
    >> tasks if you are really interested:
    >>
    >> Use weblogic.servlet.FutureResponseServlet paired with
    >> weblogic.servlet.FutureServletResponse..
    >>
    >>
    >> Heres a brief overview :
    >>
    >> public LongRunningServlet extends FutureResponseServlet {
    >>
    >> public void service(HttpServletRequest req, FutureServletResponse res)
    >> throws IOException, ServletException {
    >>
    >> // write response ...
    >> OutputStream out = res.getOutputStream();
    >> out.write(....);
    >> ....
    >> res.send(); // This sends your response to the client immediately
    >>
    >> -----------
    >> // some other task..... Also note that this part
    >> // would be blocking an execute thread , so make
    >> // sure that the task completes in a short while.
    >> ------------ }
    >>
    >> Hope that helps
    >> --
    >> Nagesh
    >>
    >> Simon Evans wrote:
    >>
    >>> i am trying to have a servlet read and write a response, close the
    >>> http connection, and then do something else after the http connection
    >>> has been closed.
    >>>
    >>> the servlet writes the output and closes the output stream, but the
    >>> client still hangs waiting to read until the servlet returns from the
    >>> doPost method. the input is only read by the client when the servlet
    >>> returns, rather than when the output has been written and flushed by
    >>> the servlet.
    >>>
    >>> is there a way to flush or close the connection before the servlet
    >>> returns?
    >>>
    >>> i am in weblogic 7 sp2 on windows 2000.
    >>>
    >>>
    >>> ------the servlet code is like:
    >>>
    >>> StringBuffer input = new StringBuffer();
    >>> InputStream is = req.getInputStream();
    >>> for(int i = in.read(); i != -1; i = in.read()) input.append((char)i);
    >>> is.close();
    >>>
    >>> OutputStream outStream = null;
    >>> outStream = res.getOutputStream();
    >>> outStream.write(respText.getBytes());
    >>> outStream.flush();
    >>> outStream.close();
    >>>
    >>> /// so some other stuff....
    >>>
    >>>
    >>> ----the client code is like:
    >>>
    >>> URL url = new URL("http://localhost:7501/ysjmx/ysservice");
    >>> HttpURLConnection con = (HttpURLConnection)url.openConnection();
    >>> con.setDoInput(true);
    >>> con.setDoOutput(true);
    >>>
    >>> java.io.OutputStream out = null;
    >>> java.io.InputStream in = null;
    >>> try {
    >>> out = con.getOutputStream();
    >>> out.write("test".getBytes());
    >>> out.close();
    >>>
    >>> in = con.getInputStream();
    >>> StringBuffer sb = new StringBuffer();
    >>>
    >>> // it hangs here until the servlet returns even though the
    >>> // response has been written by the servlet
    >>> for(int i = in.read(); i != -1; i = in.read()) sb.append((char)i);
    >>> in.close();
    >>>
    >>> } finally {
    >>> con.disconnect();
    >>> }
    >>>

    >>

    >



+ Reply to Thread