如何将servlet翻译成JSP?
你能帮我将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最干净的方法是使用 JSTL 而不是 scriptlet(查看 好的入门指南在这里)。简而言之,您需要安装 JSTL JAR(在您的应用程序服务器中或在您的特定 Web 应用程序中)。然后,您可以执行以下操作:
第一行导入 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:
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.
JSP 页面已经编译成servlet。无需手动扩展。
The JSP page is already compiled into a servlet. No need to extend one manually.