简体   繁体   English

Servlet - java.lang.IllegalStateException:已为此响应调用了getWriter()

[英]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. 我使用GlassFish作为服务器和Netbeans IDE 8.0这是我的项目结构。

在此输入图像描述

How my program works: 我的程序如何工作:

  1. client open localhost:8080/Beer 客户端打开localhost:8080 /啤酒
  2. she/he selects a beer (in index.html) 她/他选择了一种啤酒(在index.html中)
  3. it will POST to BeerSelect.java (BS for short) 它会POST到BeerSelect.java(简称BS)
  4. BS will call BeerExpert.java and then call result.jsp for finally send Test.jar to client BS将调用BeerExpert.java,然后调用result.jsp,最后将Test.jar发送给客户端

Here is the important code in BS. 这是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: 错误: 在此输入图像描述

It is illegal to use both ServletRequest.getOutputStream() and ServletRequest.getWriter(). 使用ServletRequest.getOutputStream()和ServletRequest.getWriter()是非法的。 This has been answered here in detail here. 这里已经详细解答了这个问题。

java.lang.IllegalStateException: Already using output stream java.lang.IllegalStateException:已经使用输出流

将Test.jar移动到WEB-INF文件夹中。

您可能必须将test.jar移动到项目的源文件夹中,以便可以访问它。

It is explicit in ServletResponse javadoc for method getOutputStream() : 对于方法getOutputStream()它在ServletResponse javadoc中是显式的:

Either this method or getWriter() may be called to write the body, not both, except when reset() has been called. 除非调用reset(),否则可以调用此方法或getWriter()来编写正文,而不是两者。

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. 但我认为您没有显示相关代码,因为根据stacktrace,错误发生在BeerSelect.java第83行的controller.BeerSelect.processRequest中。

With what you show, I cannot guess where getOutputStream was called, but the error says that it was, so you can : 根据你所展示的内容,我无法猜出调用了getOutputStream位置,但是错误说它是,所以你可以:

  • either find where it was called and use getWriter instead 要么找到它被调用的地方getWriter使用getWriter
  • or replace getWriter with getOutputStream in BeerSelect.java . 或者用BeerSelect.java getOutputStream替换getWriter

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IllegalStateException:已为此响应调用getWriter() - java.lang.IllegalStateException: getWriter() has already been called for this response 筛选器链的GlassFish问题:java.lang.IllegalStateException:PWC3990:此响应已调用getWriter() - GlassFish issue with filter chain: java.lang.IllegalStateException: PWC3990: getWriter() has already been called for this response java.lang.IllegalStateException:已经为此响应调用了 getOutputStream() - java.lang.IllegalStateException: getOutputStream() has already been called for this response java.lang.IllegalStateException:已为此响应调用了getOutputStream() - java.lang.IllegalStateException: getOutputStream() has already been called for this response java.lang.IllegalStateException:此请求已调用getInputStream() - java.lang.IllegalStateException: getInputStream() has already been called for this request java.lang.IllegalStateException: 已经为此请求调用了getReader() - java.lang.IllegalStateException: getReader() has already been called for this request Spring Boot Web应用程序:引发java.lang.IllegalStateException:此响应已调用getOutputStream() - Spring Boot Web Application: throwing java.lang.IllegalStateException: getOutputStream() has already been called for this response REST-java.lang.IllegalStateException:此响应已调用getOutputStream() - REST - java.lang.IllegalStateException: getOutputStream() has already been called for this response Spring MVC: java.lang.IllegalStateException: getOutputStream() 已经被这个响应调用 - Spring MVC : java.lang.IllegalStateException: getOutputStream() has already been called for this response 在jsp中下载图像:Illegalstateexception:此响应已调用getwriter() - Downloading images in a jsp: Illegalstateexception: getwriter() has already been called for this response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM