基于request.getAttribute與request.getParameter的區(qū)別詳解
HttpServletRequest類既有getAttribute()方法,也有g(shù)etParameter()方法,這兩個方法有以下區(qū)別:
1、HttpServletRequest類有setAttribute()方法,而沒有setParameter()方法;
2、當(dāng)兩個Web組件之間為鏈接關(guān)系時,被鏈接的組件通過getParameter()方法來獲得請求參數(shù);
例如,假定welcome.jsp和authenticate.jsp之間為鏈接關(guān)系,welcome.jsp中有以下代碼:
<a href="authenticate.jsp?username=qianyunlai.com">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”)方法來獲得請求參數(shù)username:
<% String username=request.getParameter("username"); %>
3、當(dāng)兩個Web組件之間為轉(zhuǎn)發(fā)關(guān)系時,轉(zhuǎn)發(fā)目標(biāo)組件通過getAttribute()方法來和轉(zhuǎn)發(fā)源組件共享request范圍內(nèi)的數(shù)據(jù)。
假定authenticate.jsp和hello.jsp之間為轉(zhuǎn)發(fā)關(guān)系。authenticate.jsp希望向hello.jsp傳遞當(dāng)前的用戶名字,如何傳遞這一數(shù)據(jù)呢?先在authenticate.jsp中調(diào)用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 %>
4、request.getAttribute 返回的是Object,request.getParameter 返回的是String。
相關(guān)文章
SpringApplicationRunListener監(jiān)聽器源碼詳解
這篇文章主要介紹了SpringApplicationRunListener監(jiān)聽器源碼詳解,springboot提供了兩個類SpringApplicationRunListeners、SpringApplicationRunListener(EventPublishingRunListener),spring框架還提供了一個ApplicationListener接口,需要的朋友可以參考下2023-11-11使用JVMTI實現(xiàn)SpringBoot的jar加密,防止反編譯
這篇文章主要介紹了使用JVMTI實現(xiàn)SpringBoot的jar加密,防止反編譯問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08數(shù)組實現(xiàn)Java 自定義Queue隊列及應(yīng)用操作
這篇文章主要介紹了數(shù)組實現(xiàn)Java 自定義Queue隊列及應(yīng)用操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06mybatis實現(xiàn)批量插入并返回主鍵(xml和注解兩種方法)
這篇文章主要介紹了mybatis實現(xiàn)批量插入并返回主鍵(xml和注解兩種方法),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12鴻蒙HarmonyOS App開發(fā)造輪子之自定義圓形圖片組件的實例代碼
這篇文章主要介紹了鴻蒙HarmonyOS App開發(fā)造輪子之自定義圓形圖片組件,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01