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
Servlet - java.lang.IllegalStateException: getWriter() has already been called for this response
提问by Altiano Gerung
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:
我的程序如何工作:
- client open localhost:8080/Beer
- she/he selects a beer (in index.html)
- it will POST to BeerSelect.java (BS for short)
- BS will call BeerExpert.java and then call result.jsp for finally send Test.jar to client
- 客户端打开本地主机:8080/Beer
- 她/他选择啤酒(在 index.html 中)
- 它将 POST 到 BeerSelect.java(简称 BS)
- 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:
错误:
采纳答案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
回答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 ServletResponse
javadocfor method getOutputStream()
:
它在ServletResponse
javadoc 中是明确的方法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.java
line 83.
但我认为您没有显示相关代码,因为根据堆栈跟踪,错误发生controller.BeerSelect.processRequest
在BeerSelect.java
第 83 行。
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 - or replace
getWriter
withgetOutputStream
inBeerSelect.java
.
- 要么找到在那里它被称为和使用
getWriter
,而不是 - 或替换
getWriter
为getOutputStream
inBeerSelect.java
。