我是jsp的新手,当我尝试通过名为cId和passWord的某些参数调用jsp页面时,出现此错误,下面给出了我一直在尝试的代码,我已经遇到了相同的错误,谷歌搜索,但仍然出现相同的问题。代码是:
<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。
当您调用response.getOutputStream时,它与JSP(将被编译为servlet的)已经获得输出流的事实相冲突。这就是为什么它导致IllegalStateException的原因。