当前位置: 首页 > 知识库问答 >
问题:

获取exception-java.lang.IllegalStateException:已经为此响应调用了getOutputStream()

鄂育
2023-03-14
<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>

共有1个答案

鲁鸿朗
2023-03-14
  • 如果您查看getOutputStream()方法的文档:它提到

throws:IllegalStateException-如果对此响应调用了GetWriter方法。

>

  • 这意味着可以调用GetWriter()GetOutputStream()方法

  •  类似资料: