Writer being already used by this servlet : IllegalStateException

Discussion in 'Java' started by Kim Ray, Feb 18, 2004.

  1. Kim Ray

    Kim Ray Guest

    Hi All,
    I am faceing a problem (ofcourse thats what makes all of us come
    here)..what I am trying to do is , create a image offscreen in a
    servlet , then using PixelGrabber serialize the int[] and then the
    applet reads it, ..so far fairly straight, now here...
    ....
    ....
    ObjectOutputStream out = new
    ObjectOutputStream(res.getOutputStream());
    System.out.println("Inside isReq after out obj");
    out.writeInt(w);
    out.writeInt(h);
    out.writeObject(pixels);
    }
    /**
    try{
    res.setContentType("text/html");
    printer=res.getWriter();
    printer.println("Object Writing done");
    printer.close();
    }catch(Exception ex){}
    }
    else{
    printer.println("Object Writing <b>Not</b> done");
    printer.close();
    }**/

    After Object writing is being done , I wish to display a html page
    (with applet embedded) but using the above code it is giving me a
    error "IllegalStateException: Writer being already used by the
    servlet..."
    Can anyone help me out...

    Regards
    Ray
     
    Kim Ray, Feb 18, 2004
    #1
    1. Advertising

  2. Kim Ray

    Ryan Stewart Guest

    "Kim Ray" <> wrote in message
    news:...
    > Hi All,
    > I am faceing a problem (ofcourse thats what makes all of us come
    > here)..what I am trying to do is , create a image offscreen in a
    > servlet , then using PixelGrabber serialize the int[] and then the
    > applet reads it, ..so far fairly straight, now here...
    > ...
    > ...
    > ObjectOutputStream out = new
    > ObjectOutputStream(res.getOutputStream());
    > System.out.println("Inside isReq after out obj");
    > out.writeInt(w);
    > out.writeInt(h);
    > out.writeObject(pixels);
    > }
    > /**
    > try{
    > res.setContentType("text/html");
    > printer=res.getWriter();
    > printer.println("Object Writing done");
    > printer.close();
    > }catch(Exception ex){}
    > }
    > else{
    > printer.println("Object Writing <b>Not</b> done");
    > printer.close();
    > }**/
    >
    > After Object writing is being done , I wish to display a html page
    > (with applet embedded) but using the above code it is giving me a
    > error "IllegalStateException: Writer being already used by the
    > servlet..."
    > Can anyone help me out...
    >
    > Regards
    > Ray


    Have a look at the JavaDocs for ServletResponse. Under getOutputStream():
    Either this method or getWriter() may be called to write the body, not both.

    And under getWriter():
    Either this method or getOutputStream() may be called to write the body, not
    both.

    Unless I'm mistaken, you'd also get this error if you tried to call either
    of those methods twice (without closing the stream/writer first, that is).
    If you already have somewhere to send output, there's no need to try to get
    another. Just wrap the first:
    OuputStream os = res.getOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(os);
    PrintWriter printer = new PrintWriter(os);
     
    Ryan Stewart, Feb 18, 2004
    #2
    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. Duane Morin
    Replies:
    2
    Views:
    927
    John C. Bollinger
    Jul 24, 2003
  2. Kim Ray
    Replies:
    3
    Views:
    2,393
    Tony Morris
    Feb 19, 2004
  3. Alexandre Touret
    Replies:
    1
    Views:
    2,274
    Dal Mon
    Jun 8, 2005
  4. vsingr
    Replies:
    0
    Views:
    5,628
    vsingr
    Jun 26, 2006
  5. javadev
    Replies:
    5
    Views:
    13,086
    javadev
    Nov 16, 2006
Loading...

Share This Page