posts - 42, comments - 7, trackbacks - 0, articles - 1
          HttpServletRequest類既有g(shù)etAttribute()方法,也由getParameter()方法,這兩個(gè)方法有以下區(qū)別:

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

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

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

          或者:

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

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

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

          (3)當(dāng)兩個(gè)Web組件之間為轉(zhuǎn)發(fā)關(guān)系時(shí),轉(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 %>

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 孝感市| 镇沅| 囊谦县| 黄平县| 博罗县| 贡山| 宝山区| 介休市| 泊头市| 浪卡子县| 祁阳县| 永顺县| 丰县| 马公市| 龙川县| 保定市| 镇坪县| 东阳市| 探索| 错那县| 黎城县| 小金县| 永年县| 永春县| 比如县| 延庆县| 东乌珠穆沁旗| 普洱| 塔河县| 滨州市| 永仁县| 轮台县| 杭锦后旗| 梨树县| 丹阳市| 文水县| 孝义市| 江达县| 信阳市| 灵璧县| 昌平区|