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.
Don’t use scriptlets (those
<% %>things). JSP is a template technology for HTML. You don’t need all those nastyout.println()things for HTML. Just write HTML plain in JSP.So, instead of
just do
(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 inpage,request,sessionandapplicationscopes. It can be accessed using the attribute name.So, instead of
use
and instead of
use
JSP also offers taglibs like JSTL to control the page flow and output.
So, instead of
use
JSP also offers
<jsp:include>tag to include page fragments.So, instead of
use
(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).