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

I am new to jsp, when I try to call a jsp page on some parameters named cId and passWord, I get this error. The code I tried is below, I have already gone through the same error that has been noticed by googling, but still I get the same problem. Code:

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

      

0


source to share


3 answers


You shouldn't try to do this inside the JSP. The JSP will already receive an output stream to write its output. You need to use a servlet to get your XML back.



When you call response.getOutputStream it is contrary to the fact that the JSP (which will be compiled into a servlet) has already received an output stream. This is why it throws an IllegalStateException.

0


source


Throws : IllegalStateException

- if a method was called in this response getWriter

.



  • This means that you can call methods getWriter()

    or getOutputStream()

    .

  • Now in the JSP (and eventually in the compiled servlet) there is an implicit variable called out

    . This is nothing more than a class example PrintWriter

    . This means that in the response object is getWriter()

    already being called and hence when called, getOutputStream()

    you getIllegalStateException

  • Now, as a solution to this problem, as some people point out, move this code into a servlet where you have full control and use the output stream the way you want.

+2


source


It is a JSP with scriplet that gets converted to Servlet file. You don't need to explicitly call the response object. If you need to see what a compiled JSP looks like when doing an expanded search (Google), how to search for a compiled class (servlet generated from JSP) on the server. Since you have already called the response method, the second call is illegal on the response object

+1


source







All Articles