0
我在將數據從servlet發送到JSP時遇到問題。將數據從servlet發送到JSP
我的數據來自用戶表單,我修改並嘗試將其發送到輸出頁面,但發送的值與輸入頁面上獲得的值相同。
當調試時,我看到地圖中的變量和更新的變量(在服務方法的引擎下創建的),所以我確信問題在於我試圖將數據發送到輸出頁面的方式。
我的servlet:
public class OpportunityCost extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (request.getParameter("purchasePrice") == null) {
getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
return;
}
String iPurchasePrice = request.getParameter("purchasePrice");
double purchasePrice = Double.parseDouble(iPurchasePrice);
purchasePrice = purchasePrice * 100;
request.setAttribute("purchasePrice", purchasePrice);
getServletContext().getRequestDispatcher("/output.jsp").forward(request, response);
}
}
輸入代碼:
<form action="OpportunityCost" method="get">
<table border="0">
<tr>
<td> Home Purchase Price*: <input type="text" value="1" name="purchasePrice" /></td>
</tr>
<tr>
<td colspan=2"> <input type="submit" value="submit" /> </td>
</tr>
</table>
輸出代碼:
String finalPrice = request.getParameter("purchasePrice"); %>
<%= finalPrice %>
任何想法,我做錯了嗎?
您的設置request.setAttribute,但嘗試顯示request.getParameter –
非常感謝! – Merch