Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Servlet - java.lang.IllegalStateException: getWriter() has already been called for this response

I'm using GlassFish as Server and Netbeans IDE 8.0 Here is my project structure.

enter image description here

How my program works:

  1. client open localhost:8080/Beer
  2. she/he selects a beer (in index.html)
  3. it will POST to BeerSelect.java (BS for short)
  4. BS will call BeerExpert.java and then call result.jsp for finally send Test.jar to client

Here is the important code in BS.

    /* Result.jsp */
    String c = request.getParameter("color");
    BeerExpert be = new BeerExpert();
    List result = be.getBrands(c);

    request.setAttribute("styles", result);
    RequestDispatcher view = request.getRequestDispatcher("result.jsp");
    view.forward(request, response);

    /* Test Client Download */
    response.setContentType("application/jar");

    ServletContext ctx = getServletContext();
    InputStream is = ctx.getResourceAsStream("/Test.jar");

    int read = 0;
    byte[] bytes = new byte[1024];

    OutputStream os = response.getOutputStream();
    while ((read = is.read(bytes)) != -1){
        os.write(bytes, 0, read);
    }
    os.flush();

The Error: enter image description here

like image 963
Altiano Gerung Avatar asked Jul 05 '15 05:07

Altiano Gerung


People also ask

What is the use of response getWriter ()?

getWriter. Returns a PrintWriter object that can send character text to the client. The PrintWriter uses the character encoding returned by getCharacterEncoding() .

What is getWriter in Servlet?

getWriter() method for the response obj that gets us the stream on which we can write our output. response. getWriter() returns a PrintWriter object that can send character text to the client. Calling flush() on the PrintWriter commits the response.

What is response setContentType text HTML?

setContentType. Sets the content type of the response being sent to the client, if the response has not been committed yet. The given content type may include a character encoding specification, for example, text/html;charset=UTF-8 .

Which method sets the locale of the response?

setLocale. Sets the locale of the response, setting the headers (including the Content-Type's charset) as appropriate. This method should be called before a call to getWriter() . By default, the response locale is the default locale for the server.


2 Answers

It is illegal to use both ServletRequest.getOutputStream() and ServletRequest.getWriter(). This has been answered here in detail here.

java.lang.IllegalStateException: Already using output stream

like image 51
Ramesh PVK Avatar answered Nov 06 '22 13:11

Ramesh PVK


It is explicit in ServletResponse javadoc for method getOutputStream() :

Either this method or getWriter() may be called to write the body, not both, except when reset() has been called.

But I think you did not show the relevant code because according to the stacktrace, the error occurs in controller.BeerSelect.processRequest, in BeerSelect.java line 83.

With what you show, I cannot guess where getOutputStream was called, but the error says that it was, so you can :

  • either find where it was called and use getWriter instead
  • or replace getWriter with getOutputStream in BeerSelect.java.
like image 36
Serge Ballesta Avatar answered Nov 06 '22 12:11

Serge Ballesta