newbie
newbie

Reputation: 14950

JSP importing a file

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.

Upvotes: 1

Views: 9660

Answers (4)

Nitul
Nitul

Reputation: 1035

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.

Upvotes: 1

BalusC
BalusC

Reputation: 1108632

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

Upvotes: 5

Quincy
Quincy

Reputation: 4433

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.

Upvotes: 2

Mac
Mac

Reputation: 14791

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.

Upvotes: 2

Related Questions