JSP 导入文件
再会!
我在运行 JSP 程序时遇到以下错误。
java.lang.IllegalStateException: PWC3991: getOutputStream() has already been called for this response
我的 JSP 中的 html 文件似乎不起作用。 我的代码如下:
<%@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>
我的 html 文件位于文件夹 WEB-INF 内。我怎样才能让它发挥作用?我还需要导入它吗?谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要使用scriptlet(那些
<% %>
的东西)。 JSP 是 HTML 的模板技术。 HTML 不需要所有那些讨厌的out.println()
东西。只需在 JSP 中编写简单的 HTML 即可。因此,不应
只执行
(请注意,这会导致无效的 HTML),HTML 页面中应该只有一个< /code> 在 <code><head></code> 中,但这是一个不同的问题,<a href="http://validator.w3.org/" rel="nofollow noreferrer">w3 HTML 验证器</a> 应该给出很多提示和答案,还可以让自己完成一些 HTML 教程)</em>
标记,并且只有一个
JSP 提供 EL (表达式语言,那些
${ }
东西)访问后端数据,即在page
、request
中作为属性出现的数据、session
和application
范围。可以使用属性名称来访问它。因此,
JSP 还提供了
像 JSTL这样
的
标签库来控制页面流和输出。
而不是
使用 JSP
因此, JSP 还提供
标记来包含页面片段,。因此,不要
使用
(并将其重命名为
.jsp
)并且异常将会消失。
另请参阅:
与具体问题无关,这个答案中的几乎所有链接都已经直接提供给了您你之前的问题。认真对待他们。要成为一名出色的程序员(正如您在问题/评论中所说的那样),请花一些时间来浏览这些链接(以及链接中的链接)。
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
,session
andapplication
scopes. 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).
首先,尽量避免将代码放到 JSP 页面上 - 它违反了作为 JSP 核心部分的 MVC/关注点分离范例。
其次,普通的旧式 JSP 已经有点过时了——现在建议使用 JSF/facelets/etc。
至于您的实际问题,我并不完全熟悉您所采用的技术,但例外基本上意味着您尝试在您能够发送的最新点之后发送内容(通常是在发送标头之后)。在这种情况下,我认为发生的情况是,当您要求它发送不同的页面时,您已经开始发送当前页面。
我能想到的最简单的修复:不要尝试基于结果的条件包含,只需重定向到不同的页面。
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.
该错误表明,一旦将某些内容打印到 jsp 中的输出流(甚至包括 doctype 声明),就无法调用错误代码行,
因此您可以尝试将这些代码片段放在页面的顶部。
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.
您不能
同时使用 out.print() 和 Requestdispatcher ....
这意味着执行 out.print() 后不应执行任何带有 requestdispatcher.forward() 的语句....
所以删除 out.println( ) 形成 else 块。
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.