Reputation: 428
I am new to jsp,when I try to invoke a jsp page by some parameters named cId and passWord,I getting this error,The code I have been trying is given below,I have already gone through the same error which have been seen by googling,but still I am getting the same issue. The code is:
<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>
Upvotes: 0
Views: 17180
Reputation: 17923
Throws:IllegalStateException
- if the getWriter
method has been called on this response .
This means that you can call either getWriter()
or getOutputStream()
methods.
Now in JSP (and eventually in compiled servlet), there is an implicit variable defined called out
. This is nothing but an instance of PrintWriter
class. This means that on the response object, getWriter()
is already called and hence on calling getOutputStream()
you get IllegalStateException
Now as solution for this problem, as some people have pointed out, move this code into a servlet where you have full control and use the outputstream the way you want.
Upvotes: 2
Reputation: 2136
This is a JSP with scriplet which is converted into a Servlet file. You dont need to call explicitly the response object. If you need to see how a compiled JSP looks like when its deployed , search (Google) how to look for the compiled class(Servlet generated out of JSP) on the server. Since you have already called the method on the response a second invocation is Illegal on the response object
Upvotes: 1
Reputation: 6354
You shouldn't try and do this inside a JSP. The JSP will already have obtained an output stream to write it's output. You need to use a servlet to return your XML.
When you call response.getOutputStream, it is conflicting with the fact that the JSP (which will be compiled into a servlet) already obtained an output stream. This is why it is resulting in the IllegalStateException.
Upvotes: 0