Issue
I'm using GlassFish as Server and Netbeans IDE 8.0 Here is my project structure.
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
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:
Solution
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
Answered By - Ramesh PVK
Answer Checked By - David Goodson (JavaFixing Volunteer)