国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

JSP 中request中getParameter與getAttribute區別

2019-11-17 03:59:33
字體:
來源:轉載
供稿:網友
request.getParameter("companyName");是獲得表單(前臺頁面表單中名稱為companyName)提交的數據
request.getAttribute("cc")是獲得你setAttribute的數據(獲得是你自己存起來的參數cc的值。)

在超鏈接里帶的參數用。。request.getParameter(參數名)

HttpServletRequest類既有getAttribute()方法,也由getParameter()方法,這兩個方法有以下區別:

(1)HttpServletRequest類有setAttribute()方法,而沒有setParameter()方法

(2)當兩個Web組件之間為鏈接關系時,被鏈接的組件通過getParameter()方法來獲得請求參數,例如假定welcome.jsp和authenticate.jsp之間為鏈接關系,welcome.jsp中有以下代碼:

<a href="authenticate.jsp?username=weiqin">authenticate.jsp </a>

或者:

<form name="form1" method="post" action="authenticate.jsp">
   請輸入用戶姓名:<input type="text" name="username">
   <input type="submit" name="Submit" value="提交">
</form>

在authenticate.jsp中通過request.getParameter("username")方法來獲得請求參數username:

<% String username=request.getParameter("username"); %>

(3)當兩個Web組件之間為轉發關系時,轉發目標組件通過getAttribute()方法來和轉發源組件共享request范圍內的數據。假定authenticate.jsp和hello.jsp之間為轉發關系。authenticate.jsp希望向hello.jsp傳遞當前的用戶名字,如何傳遞這一數據呢?先在authenticate.jsp中調用setAttribute()方法:

<%
String username=request.getParameter("username");
request.setAttribute("username",username);
%>

<jsp:forward page="hello.jsp" />

在hello.jsp中通過getAttribute()方法獲得用戶名字:

<% String username=(String)request.getAttribute("username"); %>
Hello: <%=username %>

從更深的層次考慮,request.getParameter()方法傳遞的數據,會從Web客戶端傳到Web服務器端,代表HTTP請求數據。request.getParameter()方法返回String類型的數據。

request.setAttribute()和getAttribute()方法傳遞的數據只會存在于Web容器內部,在具有轉發關系的Web組件之間共享。這兩個方法能夠設置Object類型的共享數據

簡單來講request.getParamenter()方法使用的是HTTP協議來傳遞數據,只能傳遞String類型的信息,而request.setAttribtute()方法傳遞數據是在WEB容器中傳遞,可以轉發任意類型的對象信息,比如一個listAction的servlet想傳給list.jsp一個LIST集合,則必須使用setAttribute方法
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 霞浦县| 昭觉县| 浙江省| 宝山区| 张家口市| 奉贤区| 炎陵县| 资源县| 肇源县| 双牌县| 阿图什市| 天镇县| 准格尔旗| 永嘉县| 乌海市| 台东市| 静宁县| 镇沅| 额敏县| 三明市| 府谷县| 双峰县| 乡城县| 北宁市| 雅江县| 张北县| 韶关市| 台湾省| 尤溪县| 普洱| 探索| 德庆县| 松潘县| 东明县| 光山县| 岳阳市| 淄博市| 赤峰市| 晋州市| 赤峰市| 福清市|