0

我是 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>
4

3 回答 3

2

抛出IllegalStateException- 如果getWriter在此响应上调用了该方法。

  • 这意味着您可以调用其中一个getWriter()getOutputStream()方法。

  • 现在在 JSP 中(最终在编译的 servlet 中),定义了一个名为out. 这不过是PrintWriter类的一个实例。这意味着在响应对象上,getWriter()已经被调用,因此在调用getOutputStream()你得到IllegalStateException

  • 现在作为这个问题的解决方案,正如一些人指出的那样,将此代码移动到一个 servlet 中,您可以在其中完全控制并以您想要的方式使用输出流。

于 2013-02-19T06:58:51.480 回答
1

这是一个带有脚本的 JSP,它被转换为一个 Servlet 文件。您不需要显式调用响应对象。如果您需要查看已编译的 JSP 在部署时的样子,请搜索(Google)如何在服务器上查找已编译的类(由 JSP 生成的 Servlet)。由于您已经在响应上调用了该方法,因此第二次调用在响应对象上是非法的

于 2013-02-19T06:41:04.667 回答
0

您不应该尝试在 JSP 中执行此操作。JSP 已经获得了一个输出流来写入它的输出。您需要使用 servlet 来返回您的 XML。

当您调用 response.getOutputStream 时,它与 JSP(将被编译成 servlet)已经获得输出流这一事实相冲突。这就是导致 IllegalStateException 的原因。

于 2013-02-19T06:35:24.247 回答