[英]Storing integer as attribute in jsp
我想將Integer
數值作為 JSP 中的屬性傳遞。 但是當我嘗試
int i = Integer.parseInt(request.getAttribute("count"));
返回一個錯誤。 你能告訴我一種在 JSP 中將Integer
存儲為屬性的方法嗎?
我收到一個轉換錯誤,說parseInt()
不適合處理對象。
request.getAttribute
返回一個對象。 您需要像這樣將其轉換為String
:
Integer.parseInt((String)request.getAttribute("count"));
要訪問 JSP 中的請求,請使用帶有小寫 r 的request
。 它還需要在 scriptlet 中,但不建議使用 scriptlet,因為您可以輕松使用 JSP EL。
<%
int i=Integer.parseInt((String)request.getAttribute("count"));
%>
如果您在頁面上顯示此值,則可以輕松使用表達式語言:
${count}
request.getAttribute() - 返回一個對象。
所以這個對象必須按如下方式進行類型轉換
int i = (Integer.parseInt)(String.valueOf(request.getAttribute("count")));
我有我的工作示例..看看
字符串余額 = (String.valueOf(session.getAttribute("CostOfTotalTicket")));
int i = Integer.parseInt(Balance);
我在會話中存儲的CostOfTotalTicket變量是字符串類型的
嘗試這個
<%Object object = request.getAttribute("count");
int val =Integer.parseInt(object.toString());%>
它對我有用
這取決於您需要發送的數據類型,在本例中,我發送的是integer
類型的數據。
在第一個 servlet 中:
request.setAttribute("count", count); //To send data
在第二個 servlet 中:
request.getAttribute("count"); //To receive data
這對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.