[英]Confused with Java Servlets and HTML
我明天要做的部分工作是使用Java EE搜索和添加條目。 如果搜索不存在,則添加項選項將顯示如下:
假設,當庫存編號不存在時,它將被轉移到庫存編號的添加項目文本字段。 但是我不知道該怎么做。 我的代碼如下:
Servlet的:
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("/DataForm.html");
rd.include(request, response);
out.println("Sorry Item not found..");
rd = request.getRequestDispatcher("AddEntry.html");
rd.include(request, response);
}
}
}
HTML:
<html>
<head>
<title>Add Entry</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h2>Add Item:</h2>
Stock ID: <input type ="text" name ="stockId" value="???"> <br> <--how to get it?
Item Name: <input type ="text" name ="name"> <br>
Unit Price: <input type ="text" name ="unitPrice"> <br>
On Stock : <input type ="text" name ="stock"> <br><br>
<input type ="submit" value ="Add Item">
</body>
</html>
您正在以錯誤的方式處理此問題。 HTML屬於JSP文件,而不屬於Servlet類。 另外,EL ${}
根本不在純HTML文件中運行,而僅在JSP文件中運行。 將.html
文件重命名為.jsp
。 這樣,即使您仍有XSS攻擊漏洞,像${param.id}
這樣的EL也將起作用。
(請閱讀它們,其中包含您好世界示例,應該可以打開您的腦海)
您不能在純HTML文件中使用表達式語言(即${param.id}
)。 它只會在JSP(擴展名為.jsp
文件)中解釋。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.