JSP导入文件 [英] JSP importing a file

查看:115
本文介绍了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 中.我该如何运作?我还需要导入吗?谢谢.

解决方案

不要使用 scriptlets (那些<% %>东西). JSP是HTML的模板技术.对于HTML,您不需要所有这些讨厌的out.println()东西.只需在JSP中编写纯HTML.

所以,而不是

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

只是做

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

(请注意,这会导致HTML无效,HTML页面中应该只有一个<html>标签,而<head>中应该只有一个<title>,但这是一个不同的问题, w3 HTML验证器应该给出很多提示和答案,并让您自己学习一些HTML教程)


JSP提供 EL (表达语言,那些${ }东西)来访问后端数据,即在pagerequestsessionapplication范围中作为属性出现.可以使用属性名称进行访问.

所以,而不是

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

使用

${invenItem}

而不是

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

使用

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


JSP还提供了诸如 JSTL 之类的标签库来控制页面流和输出.

所以,而不是

<%
    if (item != null) {

    } else {

    }
%>

使用

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

    </c:when>
    <c:otherwise>

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


JSP还提供了<jsp:include>标记以包含页面片段.

所以,而不是

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

使用

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

(并将其重命名为.jsp)

该异常将消失.


另请参见:


与具体问题无关,此答案中的几乎所有链接都已经(间接)在您先前的问题中直接提供给您.认真对待他们.要成为一名优秀的程序员(如您在问题/评论中曾经说过的),请花一些时间来使自己熟悉那些链接(以及链接中的链接).

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

这篇关于JSP导入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆