I am having problem with my JSP conversion (Servlet to JSP).
this is my JSP code:
<%@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">
<jsp:directive.page import="java.io.*" />
<jsp:directive.page import="javax.servlet.*" />
<jsp:directive.page import="javax.servlet.http.*" />
<jsp:directive.page import="Inventory.Item"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Display Data</title>
</head>
<body>
<jsp:useBean id="inventory" class="Inventory.AddData" />
<%!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("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);
}
}
}%>
</body>
This code was taken from a Servlet which shows an Information of the Entry searched by a user.
When I run this JSP code it opens a new page on my web server but Doesn't Display Anything Just a Blank Page
.
Supposedly this will show the Information of the entry searched by a user. Please Help...
In the firs开发者_如何学编程t Place... Am I doing JSP translation right?
Using the Scriptlets Method in JSP
import the Object item by doing this.. package.*
No need to import the java packages.
On the body place this code instead.
<% Item item = (Item) request.getAttribute("invenItem"); if (item != null) { %> Stock ID : <%= item.getStockID() %><br> Name : <%= item.getItemName() %><br> Unit Price: <%= item.getUnitPrice() %><br> On Stock : <%= item.getOnStock() %><br> <% } else { %> <%@ include file ="DataForm.html" %><br> <%@ include file ="ItemEntry.html" %> <% } %>
For the explanation, read this as per Mr BalucC's suggestion. Then try also creating JSP using javabeans and EL.
Note: There's already a javabeans in your code.. <jsp:useBean id="inventory" class="Inventory.AddData" />
view this for the javabeans and EL code
精彩评论