如何将servlet翻译成JSP?

发布于 2024-10-19 19:46:19 字数 2094 浏览 2 评论 0原文

你能帮我将 Servlet 转换为 JSP

这里是代码:

package Inventory;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DisplayData extends HttpServlet {

public void doGet(HttpServletRequest request,
        HttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    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("/SearchPage.html");
        rd.include(request, response);
        rd = request.getRequestDispatcher("/AddData.html");
        rd.include(request, response);
    }

}

}

我正在尝试使用 scriplet,但仍然想知道如何转换:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

到 JSP。我尝试这样做:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Display Data</title>
</head>
<jsp:useBean id="inventory" class="Inventory.AddData" />
<jsp:directive.page import="java.io.*" />
<jsp:directive.page import="javax.servlet.*" />
<jsp:directive.page import="javax.servlet.http.*" />
<body>

    <%-- But I don't know how to convert this:
         public class DisplayData extends HttpServlet
         in JSP
    --%>

</body>

请帮助...提前致谢

Can you help me translate a Servlet to JSP

here's the code:

package Inventory;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DisplayData extends HttpServlet {

public void doGet(HttpServletRequest request,
        HttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    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("/SearchPage.html");
        rd.include(request, response);
        rd = request.getRequestDispatcher("/AddData.html");
        rd.include(request, response);
    }

}

}

I am trying to use the scriplets, but still want to know how to convert:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

to JSP. I try to do this:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Display Data</title>
</head>
<jsp:useBean id="inventory" class="Inventory.AddData" />
<jsp:directive.page import="java.io.*" />
<jsp:directive.page import="javax.servlet.*" />
<jsp:directive.page import="javax.servlet.http.*" />
<body>

    <%-- But I don't know how to convert this:
         public class DisplayData extends HttpServlet
         in JSP
    --%>

</body>

please Help... thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

意中人 2024-10-26 19:46:19

最干净的方法是使用 JSTL 而不是 scriptlet(查看 好的入门指南在这里)。简而言之,您需要安装 JSTL JAR(在您的应用程序服务器中或在您的特定 Web 应用程序中)。然后,您可以执行以下操作:

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
...
<c:out value="${invenItem.stockID}"/>

第一行导入 JSTL“核心”标签库,这使您可以访问基本标签。然后,标签用于输出数据 - 它将为您转义特殊字符(例如,假设 stockID 具有字符“<”)。最后,${} 是一个 EL 表达式,在本例中它只是访问 invenItem 请求属性并提取 stockID 值(通过调用 getStockID())。

JSP 视图中干净、简单,没有丑陋的 Java scriptlet。

The cleanest way would be to use JSTL instead of scriptlets (check out a good primer here). In a nutshell, you need to have JSTL JARs installed (either in your app server or in your specific webapp). Then, you can do the following:

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
...
<c:out value="${invenItem.stockID}"/>

The first line imports the JSTL "core" tag library, which gives you access to the basic tags. Then, the tag is used to output data - it will escape special characters for you (for example, suppose stockID has the character "<"). Finally, the ${} is an EL expression, which in this case simply accesses the invenItem request attribute and extracts the stockID value (by calling getStockID()).

Clean, simple, no ugly Java scriptlets in your JSP view.

夜无邪 2024-10-26 19:46:19

JSP 页面已经编译成servlet。无需手动扩展。

The JSP page is already compiled into a servlet. No need to extend one manually.

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