Getting Exception-java.lang.IllegalStateException: getOutputStream () has already been called for this response
I am new to jsp, when I try to call a jsp page on some parameters named cId and passWord, I get this error. The code I tried is below, I have already gone through the same error that has been noticed by googling, but still I get the same problem. Code:
<body>
<%
String cidMessage = "cID";
String passEncrypted = "passWord";
System.out.println("CID ISSSSSSSSSSSS"+cId);
if ((cId.equals(cidMessage)) && (passWord.equals(passEncrypted))) {
System.out.println("Validation Correct"+cId);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String time = sdf.format(date.getTime());
String xmlOutput = "<smsreport>"
+ "<date>" + time + "</date>"
+ "<result>" + "SUCESS" + "</result>"
+ "<msgid>" + currentTimeMillis() + "</msgid>"
+ "<msgparts>" + "1" + "</msgparts>"
+ "</smsreport>";
try {
byte[] contents = xmlOutput.getBytes();
response.setContentType("text/xml");
response.setContentLength(contents.length);
response.getOutputStream().write(contents);
response.getOutputStream().flush();
} catch (Exception e) {
throw new ServletException(e);
}
} else {
System.out.println("Validation Wrong"+cId);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String time = sdf.format(date.getTime());
String xmlOutput = "<smsreport>"
+ "<date>" + time + "</date>"
+ "<result>ERROR</result>"
+ "<msgid>" + "ErrorCode" + "</msgid>"
+ "<msgparts>" + "ErrorMessage" + "</msgparts>"
+ "</smsreport>";
try {
byte[] contents = xmlOutput.getBytes();
response.setContentType("text/xml");
response.setContentLength(contents.length);
response.getOutputStream().write(contents);
response.getOutputStream().flush();
} catch (Exception e) {
throw new ServletException(e);
}
}
%>
</body>
source to share
You shouldn't try to do this inside the JSP. The JSP will already receive an output stream to write its output. You need to use a servlet to get your XML back.
When you call response.getOutputStream it is contrary to the fact that the JSP (which will be compiled into a servlet) has already received an output stream. This is why it throws an IllegalStateException.
source to share
- If you check the documentation the getOutputStream () method : it mentions
Throws : IllegalStateException
- if a method was called in this response getWriter
.
-
This means that you can call methods
getWriter()
orgetOutputStream()
. -
Now in the JSP (and eventually in the compiled servlet) there is an implicit variable called
out
. This is nothing more than a class examplePrintWriter
. This means that in the response object isgetWriter()
already being called and hence when called,getOutputStream()
you getIllegalStateException
-
Now, as a solution to this problem, as some people point out, move this code into a servlet where you have full control and use the output stream the way you want.
source to share
It is a JSP with scriplet that gets converted to Servlet file. You don't need to explicitly call the response object. If you need to see what a compiled JSP looks like when doing an expanded search (Google), how to search for a compiled class (servlet generated from JSP) on the server. Since you have already called the response method, the second call is illegal on the response object
source to share