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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31227525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:49:07  来源:igfitidea点击:

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

javajspservletsinputstream

提问by Altiano Gerung

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

我使用 GlassFish 作为服务器和 Netbeans IDE 8.0 这是我的项目结构。

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
  1. 客户端打开本地主机:8080/Beer
  2. 她/他选择啤酒(在 index.html 中)
  3. 它将 POST 到 BeerSelect.java(简称 BS)
  4. 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: enter image description here

错误: 在此处输入图片说明

采纳答案by Ramesh PVK

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

同时使用 ServletRequest.getOutputStream() 和 ServletRequest.getWriter() 是非法的。这已在此处详细回答here。

java.lang.IllegalStateException: Already using output stream

java.lang.IllegalStateException: 已经在使用输出流

回答by Raman Shrivastava

Move your Test.jar inside WEB-INF folder.

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

回答by Garry

You may have to move your test.jar in the source folder of your project so that it can accessible.

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

回答by Serge Ballesta

It is explicit in ServletResponsejavadocfor method getOutputStream():

它在ServletResponsejavadoc 中是明确的方法getOutputStream()

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

可以调用此方法或 getWriter() 来写入主体,但不能同时调用两者,除非调用了 reset()。

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

但我认为您没有显示相关代码,因为根据堆栈跟踪,错误发生controller.BeerSelect.processRequestBeerSelect.java第 83 行。

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

根据你展示的内容,我无法猜测在哪里getOutputStream被调用,但错误表明它是,所以你可以:

  • either find where it was called and use getWriterinstead
  • or replace getWriterwith getOutputStreamin BeerSelect.java.
  • 要么找到在那里它被称为和使用getWriter,而不是
  • 或替换getWritergetOutputStreamin BeerSelect.java