1

Good day!

I encountered the following error upon running my JSP program.

java.lang.IllegalStateException: PWC3991: getOutputStream() has already been called for this response

It seems like the html file inside my JSP doesn't work. My code is as follows:

<%@page import  = "java.util.*"%>
<%@page import  = "javax.servlet.*"%>
<%@page import  = "javax.servlet.http.*"%>
<%@page import= "session.*" %>


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
            <%
            Item item = (Item) request.getAttribute("invenItem");

            if (item != null) {
                out.println("<html><title>Inventory Item</title>");
                out.println("<body><h1>Inventory Item Details:</h1>");
                out.println("Stock ID  : " + item.getStockID() + "<br/>");
                out.println("Name      : " + item.getItemName() + "<br/>");
                out.println("Unit Price: " + item.getUnitPrice() + "<br/>");
                out.println("On Stock  : " + item.getOnStock() + "<br/>");
                out.println("</body>");
                out.println("</html>");
            } else {
                RequestDispatcher rd = request.getRequestDispatcher("DataForm.html"); //NOT WORKING
                rd.include(request, response);

                out.println("<br>Item not found...<br>");

                rd = request.getRequestDispatcher("ItemEntry.html"); //NOT WORKING
                rd.include(request, response);
            }
            %>
        </body>
    </html>

My html Files are located inside the folder WEB-INF. How can I make it work? DO i need to import it also? Thank you.

newbie
  • 14,582
  • 31
  • 104
  • 146

4 Answers4

5

Don't use scriptlets (those <% %> things). JSP is a template technology for HTML. You don't need all those nasty out.println() things for HTML. Just write HTML plain in JSP.

So, instead of

<%
    out.println("<html><title>Inventory Item</title>");
%>

just do

<html><title>Inventory Item</title>

(note that this results in invalid HTML, there should be only one <html> tag in a HTML page and only one <title> in the <head>, but that's a different problem, the w3 HTML validator should give a lot of hints and answers, also get yourself through some HTML tutorials)


JSP offers EL (Expression Language, those ${ } things) to access backend data, i.e. the data which is present as attribute in page, request, session and application scopes. It can be accessed using the attribute name.

So, instead of

<%
    Item item = (Item) request.getAttribute("invenItem");
%>

use

${invenItem}

and instead of

<%
    out.println("Stock ID  : " + item.getStockID() + "<br/>");
%>

use

Stock ID: ${invenItem.stockID}<br/>

JSP also offers taglibs like JSTL to control the page flow and output.

So, instead of

<%
    if (item != null) {

    } else {

    }
%>

use

<c:choose>
    <c:when test="${invenItem != null}">

    </c:when>
    <c:otherwise>

    </c:otherwise>
</c:choose>

JSP also offers <jsp:include> tag to include page fragments.

So, instead of

<%
    RequestDispatcher rd = request.getRequestDispatcher("DataForm.html"); //NOT WORKING
    rd.include(request, response);
%>

use

<jsp:include page="/WEB-INF/DataForm.jsp" />

(and rename it to .jsp)

And the exception will disappear.


See also:


Unrelated to the concrete problem, almost all of the links in this answer was already (in)directly given to you in your previous questions. Take them serious. To become a great programmer (as you ever stated in a question/comment), take some time to get yourself through those links (and the links in the links).

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • okay. Thank you once again Mr BalusC! i'm studying jsp (EL, Javabeans etc) as you've suggested... :) – newbie Mar 03 '11 at 05:18
2

The error indicates that the error lines of code cannot be called once something has been printed out to the output stream in jsp (including even the doctype declaration)

So you can try to put those pieces of code at the top of your page.

Quincy
  • 4,393
  • 3
  • 26
  • 40
2

Firstly, try to avoid putting code onto your JSP page - it violates the MVC/separation of concerns paradigm that is a central part of JSP.

Second, plain old JSP's getting a bit old - using JSF/facelets/etc is recommended these days.

As for your actual problem, I'm not totally familiar with the technique you're employing, but the exception basically means that you've tried to send content after the latest point at which your able to (generally, after sending headers). In this case, I think what's happening is that you've already started sending the current page when you ask it to send a different page.

Simplest fix I can think of: rather than trying a conditional include based on results, just redirect to a different page.

Mac
  • 14,615
  • 9
  • 62
  • 80
  • how? can you give me example. Thanks – newbie Mar 03 '11 at 02:47
  • That makes no sense. There are no redirects in that code - only an include(). – Mike Baranczak Mar 03 '11 at 02:47
  • "tried to send content after the latest point at which your able to (generally, after sending headers)" - you've got that backwards. First the server sends the HTTP headers, then the content. – Mike Baranczak Mar 03 '11 at 02:50
  • @Mike: my wording seems to have been a little unclear. Read again: I never said that there was a redirect in the code posted, only that there was the attempt to send a page other than originally specified. I merely suggested that displaying a different page for the outcome might be a better approach. And, obviously headers are sent first - otherwise they'd be called "footers". What I said is that once headers for one page are sent you can't then decide to start sending a different page. – Mac Mar 03 '11 at 04:35
1

You can not use

out.print() and Requestdispatcher simultaneously....

It means after execution of out.print() there should not be any execution of statement with requestdispatcher.forward()....

So remove out.println() form else block.

Nitul
  • 997
  • 12
  • 35