html input 传值 request接到值为null,解决jsp向servlet传值为null的问题
jsp關鍵代碼:
BookInfo bi = (BookInfo)request.getAttribute("bi") ;
request.setAttribute("id", bi.getId()) ;
method="post">
value="
%>"/>
servlet關鍵代碼:
if(actionUrl.equals("/update.do")){
String id =
(String)request.getAttribute("id");;System.out.println(id) ;
String
bookname = request.getParameter("bookname")
;System.out.println(bookname) ;}
執行結果:servlet可以取得bookname的值,但是id的值為null.
分析原因:用request.setAttribute()保存的數據只能在同一個請求的前提下取到,在不同的請求之間屬性會重置。
解決方法:
(1)在form表單傳值:
在form表單里加上
value="
%>"/>,在servlet中用request.getParameter取值。
(2)使用session會話機制:
在jsp頁面設置session: session.setAttribute("id", bi.getId())
,在servlet中取值:String id =
(String)request.getSession().getAttribute("id") 。
(3)URL傳值:
在form表單提交URL中傳遞參數:
action="update.do?id=
%>"
method="post">,servlet用request.getParameter取值。
總結:jsp向servlet傳遞參數有三種方法:form表單,session和URL傳值
總結
以上是生活随笔為你收集整理的html input 传值 request接到值为null,解决jsp向servlet传值为null的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计数在html怎么添加,在Go中显示ht
- 下一篇: 计算机网络rsa算法,计算机网络安全实验