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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14951041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:59:33  来源:igfitidea点击:

Getting Exception-java.lang.IllegalStateException: getOutputStream() has already been called for this response

javajsp

提问by Prabhath kesav

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:

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

采纳答案by Mr Moose

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.

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

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.

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

回答by Santosh

Throws:IllegalStateException- if the getWritermethod has been called on this response .

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

  • This means that you can call either getWriter()or getOutputStream()methods.

  • Now in JSP (and eventually in compiled servlet), there is an implicit variable defined called out. This is nothing but an instance of PrintWriterclass. This means that on the response object, getWriter()is already called and hence on calling getOutputStream()you get IllegalStateException

  • Now as solution for this problem, as some people have pointed out, move this code into a servlet where you have full control and use the outputstream the way you want.

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

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

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

回答by user1428716

This is a JSP with scriplet which is converted into a Servlet file. You dont need to call explicitly the response object. If you need to see how a compiled JSP looks like when its deployed , search (Google) how to look for the compiled class(Servlet generated out of JSP) on the server. Since you have already called the method on the response a second invocation is Illegal on the response object

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