IllegalStateException : Writer being already used by this servlet

Discussion in 'Java' started by Kim Ray, Feb 19, 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 19, 2004
    #1
    1. Advertising

  2. Kim Ray

    Tony Morris 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


    Read the API documentation
    You can't call HttpServletResponse#getWriter() after a call to
    HttpServletResponse#getOutputStream()
    and there's a good reason for that (more in-depth thought required on your
    part).

    It is very unclear what you are trying to achieve, so I can't offer a
    correct solution.

    PS:
    catch(java.lang.Exception e) // this piece of code *should* result in
    castration (unless the Exception is rethrown) - it's poor form

    --
    Tony Morris
    (BInfTech, Cert 3 I.T.)
    Software Engineer
    (2003 VTR1000F)
    Sun Certified Programmer for the Java 2 Platform (1.4)
    Sun Certified Developer for the Java 2 Platform
     
    Tony Morris, Feb 19, 2004
    #2
    1. Advertising

  3. Kim Ray

    Kim Ray Guest

    Hi Tony
    Thanks for the response , here is what I am doing,
    1) I have an Applet which takes user input as text.
    2) From applet it passes simple parameters to the servlet using the
    showDocument method showDocument("http://server/webapp/ServletName?text1=text1&text2=text....")
    3) The servlet gets the parameters and constructs images out of those
    text using customer defined fonts (from Font.createFont() methods)
    4)Using PixelGrabber the image is serialized as int[] to the response
    stream

    Now....
    A) After serialization I want to reload the applet so that it
    de-serializes the int[] and reconstruct the image (The reconstruction
    of image and displaying it on applet is working fine , when i run the
    servlet as a standalone (with no user input)), but when I take the
    user input ...here is what the problem comes :(

    Regards
    Ray
    "Tony Morris" <> wrote in message news:<c11ef7$kq0$>...
    > "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

    >
    > Read the API documentation
    > You can't call HttpServletResponse#getWriter() after a call to
    > HttpServletResponse#getOutputStream()
    > and there's a good reason for that (more in-depth thought required on your
    > part).
    >
    > It is very unclear what you are trying to achieve, so I can't offer a
    > correct solution.
    >
    > PS:
    > catch(java.lang.Exception e) // this piece of code *should* result in
    > castration (unless the Exception is rethrown) - it's poor form
     
    Kim Ray, Feb 19, 2004
    #3
  4. Kim Ray

    Tony Morris Guest

    "Kim Ray" <> wrote in message
    news:...
    > Hi Tony
    > Thanks for the response , here is what I am doing,
    > 1) I have an Applet which takes user input as text.
    > 2) From applet it passes simple parameters to the servlet using the
    > showDocument method

    showDocument("http://server/webapp/ServletName?text1=text1&text2=text....")
    > 3) The servlet gets the parameters and constructs images out of those
    > text using customer defined fonts (from Font.createFont() methods)
    > 4)Using PixelGrabber the image is serialized as int[] to the response
    > stream
    >
    > Now....
    > A) After serialization I want to reload the applet so that it
    > de-serializes the int[] and reconstruct the image (The reconstruction
    > of image and displaying it on applet is working fine , when i run the
    > servlet as a standalone (with no user input)), but when I take the
    > user input ...here is what the problem comes :(
    >
    > Regards
    > Ray
    > "Tony Morris" <> wrote in message

    news:<c11ef7$kq0$>...
    > > "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

    > >
    > > Read the API documentation
    > > You can't call HttpServletResponse#getWriter() after a call to
    > > HttpServletResponse#getOutputStream()
    > > and there's a good reason for that (more in-depth thought required on

    your
    > > part).
    > >
    > > It is very unclear what you are trying to achieve, so I can't offer a
    > > correct solution.
    > >
    > > PS:
    > > catch(java.lang.Exception e) // this piece of code *should* result in
    > > castration (unless the Exception is rethrown) - it's poor form


    If that is the case, simply use two servlets to perform each task - the
    applet should request from each individually.

    --
    Tony Morris
    (BInfTech, Cert 3 I.T.)
    Software Engineer
    (2003 VTR1000F)
    Sun Certified Programmer for the Java 2 Platform (1.4)
    Sun Certified Developer for the Java 2 Platform
     
    Tony Morris, Feb 19, 2004
    #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. Duane Morin
    Replies:
    2
    Views:
    927
    John C. Bollinger
    Jul 24, 2003
  2. Kim Ray
    Replies:
    1
    Views:
    1,947
    Ryan Stewart
    Feb 18, 2004
  3. Alexandre Touret
    Replies:
    1
    Views:
    2,274
    Dal Mon
    Jun 8, 2005
  4. vsingr
    Replies:
    0
    Views:
    5,627
    vsingr
    Jun 26, 2006
  5. javadev
    Replies:
    5
    Views:
    13,086
    javadev
    Nov 16, 2006
Loading...

Share This Page