获取异常-java.lang.IllegalStateException:已为此响应调用了getOutputStream() [英] Getting Exception-java.lang.IllegalStateException: getOutputStream() has already been called for this response
问题描述
我是jsp的新手,当我尝试通过一些名为cId和passWord的参数调用jsp页面时,我收到此错误,我一直在尝试的代码如下,我已经经历了同样的错误通过谷歌搜索看到了,但我仍然得到同样的问题。
代码为:
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>
推荐答案
你不应该在JSP中尝试这样做。 JSP已经获得了输出流来写入它的输出。您需要使用servlet来返回XML。
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.
当你调用response.getOutputStream时,它与JSP(将被编译成servlet)已经获得输出流的事实相冲突。这就是它导致IllegalStateException的原因。
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.
这篇关于获取异常-java.lang.IllegalStateException:已为此响应调用了getOutputStream()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!