我使用GlassFish作爲服務器和Netbeans IDE 8.0 這是我的項目結構。Servlet - java.lang.IllegalStateException:getWriter()已被調用此響應
我的程序是如何工作的:
- 客戶端打開本地主機:8080 /啤酒
- 他/她選擇啤酒(index.html中)
- 將POST到BeerSelect .java(簡稱BS)
- BS將調用BeerExpert.java,然後調用result.jsp,最終將Test.jar發送給客戶端
這是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();
的錯誤: