JSP 导入文件

发布于 2024-10-19 18:51:23 字数 2023 浏览 3 评论 0原文

再会!

我在运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

城歌 2024-10-26 18:51:23

不要使用scriptlet(那些<% %> 的东西)。 JSP 是 HTML 的模板技术。 HTML 不需要所有那些讨厌的 out.println() 东西。只需在 JSP 中编写简单的 HTML 即可。

因此,不应

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

只执行

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

(请注意,这会导致无效的 HTML),HTML 页面中应该只有一个 标记,并且只有一个 < /code> 在 <code><head></code> 中,但这是一个不同的问题,<a href="http://validator.w3.org/" rel="nofollow noreferrer">w3 HTML 验证器</a> 应该给出很多提示和答案,还可以让自己完成一些 HTML 教程)</em>


JSP 提供 EL (表达式语言,那些 ${ } 东西)访问后端数据,即在 pagerequest 中作为属性出现的数据、sessionapplication 范围。可以使用属性名称来访问它。

因此,

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

JSP 还提供了

${invenItem}

JSTL这样

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

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

标签库来控制页面流和输出。

而不是

<%
    if (item != null) {

    } else {

    }
%>

使用 JSP

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

    </c:when>
    <c:otherwise>

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

因此, JSP 还提供 标记来包含页面片段,

。因此,不要

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

使用

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

(并将其重命名为 .jsp

并且异常将会消失。


另请参阅:


与具体问题无关,这个答案中的几乎所有链接都已经直接提供给了您你之前的问题。认真对待他们。要成为一名出色的程序员(正如您在问题/评论中所说的那样),请花一些时间来浏览这些链接(以及链接中的链接)。

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

自此以后,行同陌路 2024-10-26 18:51:23

首先,尽量避免将代码放到 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.

ペ泪落弦音 2024-10-26 18:51:23

该错误表明,一旦将某些内容打印到 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.

深海不蓝 2024-10-26 18:51:23

您不能

同时使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文