<a href="http://www.cnblogs.com/keyi/p/6125626.html">java中getAttribute和getParameter的差別</a>
getAttribute表示從request範圍取得設定的屬性,必須要先setAttribute設定屬性,才能通過getAttribute來取得,設定與取得的為Object對象類型
getParameter表示接收參數,參數為頁面送出的參數,包括:表單送出的參數、URL重寫(就是xxx?id=1中的id)傳的參數等,是以這個并沒有設定參數的方法(沒有setParameter),而且接收參數傳回的不是Object,而是String類型
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.getParameter()取得是通過容器的實作來取得通過類似post,get等方式傳入的資料,, request.setAttribute()和getAttribute()隻是在web容器内部流轉,僅僅是請求處理階段,這個的确是正解.
getAttribute是傳回對象,getParameter傳回字元串
request.getAttribute()方法傳回request範圍記憶體在的對象,而request.getParameter()方法是擷取http送出過來的資料。