【问题标题】:Getting Exception-java.lang.IllegalStateException: getOutputStream() has already been called for this response获取 Exception-java.lang.IllegalStateException: getOutputStream() 已经为此响应调用
【发布时间】:2013-02-19 06:15:59
【问题描述】:

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

【问题讨论】:

    标签: java jsp


    【解决方案1】:

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

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

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          猜你喜欢
          • 2023-03-08
          • 2020-01-08
          • 2012-12-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-14
          • 1970-01-01
          • 2016-03-03
          相关资源
          最近更新 更多