Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException: getOutputStream(

Discussion in 'Java' started by javadev, Nov 14, 2006.

  1. javadev

    javadev Guest

    I have an Apache Tomcat server on which I have deployed 2 web
    applications.

    When I deploy and run the first application everything works fine. But
    on deploying the second application, I get the following error:

    Servlet.service() for servlet jsp threw exception
    java.lang.IllegalStateException: getOutputStream() has already been
    called for this response

    I think this might have to do with both applications running of the
    same port on the server. How can I fix this?

    Thanks
    SS
    javadev, Nov 14, 2006
    #1
    1. Advertising

  2. Re: Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException: getOutputStream() has already been called for this response

    > I think this might have to do with both applications running of the
    > same port on the server. How can I fix this?


    As long as they have different contexts, you should be okay. The
    mentioned exception occurs when you try to write to the response after
    it has been committed. Can you provide more information around the two
    applications? This should happen to an application regardless of it's
    coexistance with another, as the contexts are totally different.

    -cheers,
    Manish
    Manish Pandit, Nov 14, 2006
    #2
    1. Advertising

  3. javadev

    javadev Guest

    Re: Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException: getOutputStream() has already been called for this response

    > As long as they have different contexts, you should be okay. The
    > mentioned exception occurs when you try to write to the response after
    > it has been committed. Can you provide more information around the two
    > applications? This should happen to an application regardless of it's
    > coexistance with another, as the contexts are totally different.


    The 2 applications are deployed separately through the Tomcat Manager
    which I assume should create separate contexts for them.

    I get the same error message on NetBeans (which I use to develop the
    applications) but my application runs fine through NetBeans. However
    when I deploy the 2nd application and try to run it on the server, then
    I can access the index.jsp which contains a login page. On submitting
    this page, I get the stack trace of the above error.
    javadev, Nov 14, 2006
    #3
  4. "javadev" <> writes:

    > Servlet.service() for servlet jsp threw exception
    > java.lang.IllegalStateException: getOutputStream() has already been
    > called for this response


    Uusually triggered by calling RequestDispatcher.forward() or some
    ServletResponse methods (like setting response headers) that don't
    like it if you have tried to get hold of the (body) output stream
    (i.e. you have committed the response). Since it cannot know whether
    you have written to it it just assumes you have been naughty and done
    so.

    > I think this might have to do with both applications running of the
    > same port on the server. How can I fix this?


    It doesn't have anything to do with the port: You can have as many
    distinctly named web applications you like (for practical purposes) on
    the same Tomcat.
    Tor Iver Wilhelmsen, Nov 15, 2006
    #4
  5. javadev

    Guest

    Re: Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException: getOutputStream() has already been called for this response

    There's a good change you've opened your PrintWriter, and then done a
    forward. A forward expects a clean, virginal PrintWriter.

    Check that you're not doing a Print before or after forwarding with the
    RequestDispatcher.

    Cheers!

    -Cameron McKenzie
    Author of the SCJA Certification Guide and What is WebSphere?


    Free WebSphere Tutorials: www.pulpjava.com
    Free Mock Java Certification Exams: www.scja.com
    Free J2EE and Java Multimedia Tutorials: www.mcnz.com

    Certification Resources: www.examscam.com




    Tor Iver Wilhelmsen wrote:
    > "javadev" <> writes:
    >
    > > Servlet.service() for servlet jsp threw exception
    > > java.lang.IllegalStateException: getOutputStream() has already been
    > > called for this response

    >
    > Uusually triggered by calling RequestDispatcher.forward() or some
    > ServletResponse methods (like setting response headers) that don't
    > like it if you have tried to get hold of the (body) output stream
    > (i.e. you have committed the response). Since it cannot know whether
    > you have written to it it just assumes you have been naughty and done
    > so.
    >
    > > I think this might have to do with both applications running of the
    > > same port on the server. How can I fix this?

    >
    > It doesn't have anything to do with the port: You can have as many
    > distinctly named web applications you like (for practical purposes) on
    > the same Tomcat.
    , Nov 15, 2006
    #5
  6. javadev

    javadev Guest

    Re: Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException: getOutputStream() has already been called for this response

    Hi Tor

    It turned out that it was being caused by the RequestDispatcher's
    forward() call. I've changed those now to jsp:forward and it works fine
    now.

    Thanks
    Swetha

    wrote:
    > There's a good change you've opened your PrintWriter, and then done a
    > forward. A forward expects a clean, virginal PrintWriter.
    >
    > Check that you're not doing a Print before or after forwarding with the
    > RequestDispatcher.
    >
    > Cheers!
    >
    > -Cameron McKenzie
    > Author of the SCJA Certification Guide and What is WebSphere?
    >
    >
    > Free WebSphere Tutorials: www.pulpjava.com
    > Free Mock Java Certification Exams: www.scja.com
    > Free J2EE and Java Multimedia Tutorials: www.mcnz.com
    >
    > Certification Resources: www.examscam.com
    >
    >
    >
    >
    > Tor Iver Wilhelmsen wrote:
    > > "javadev" <> writes:
    > >
    > > > Servlet.service() for servlet jsp threw exception
    > > > java.lang.IllegalStateException: getOutputStream() has already been
    > > > called for this response

    > >
    > > Uusually triggered by calling RequestDispatcher.forward() or some
    > > ServletResponse methods (like setting response headers) that don't
    > > like it if you have tried to get hold of the (body) output stream
    > > (i.e. you have committed the response). Since it cannot know whether
    > > you have written to it it just assumes you have been naughty and done
    > > so.
    > >
    > > > I think this might have to do with both applications running of the
    > > > same port on the server. How can I fix this?

    > >
    > > It doesn't have anything to do with the port: You can have as many
    > > distinctly named web applications you like (for practical purposes) on
    > > the same Tomcat.
    javadev, Nov 16, 2006
    #6
    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.

Share This Page