精品丰满熟女一区二区三区_五月天亚洲欧美综合网_亚洲青青青在线观看_国产一区二区精选

  • <menu id="29e66"></menu>

    <bdo id="29e66"><mark id="29e66"><legend id="29e66"></legend></mark></bdo>

  • <pre id="29e66"><tt id="29e66"><rt id="29e66"></rt></tt></pre>

      <label id="29e66"></label><address id="29e66"><mark id="29e66"><strike id="29e66"></strike></mark></address>
      學(xué)習(xí)啦 > 知識(shí)大全 > 知識(shí)百科 > 百科知識(shí) > 參數(shù)和屬性的區(qū)別是什么意思

      參數(shù)和屬性的區(qū)別是什么意思

      時(shí)間: 肖陽(yáng)1034 分享

      參數(shù)和屬性的區(qū)別是什么意思

        在JAVA WEB開(kāi)發(fā)中經(jīng)常遇到獲取請(qǐng)求參數(shù)的值和在request對(duì)象中set或者get屬性,初學(xué)者往往容易混淆,那么二者之間關(guān)系到底如何呢?下面就跟著學(xué)習(xí)啦小編一起來(lái)看看吧。

        參數(shù)(parameter)和屬性(Attribute)的區(qū)別

        區(qū)別:

        來(lái)源不同:

        參數(shù)(parameter)是從客戶端(瀏覽器)中由用戶提供的,若是GET方法是從URL中提供的,若是POST方法是從請(qǐng)求體(request body)中提供的;

        屬性(attribute)是服務(wù)器端的組件(JSP或者Servlet)利用requst.setAttribute()設(shè)置的

        操作不同:

        參數(shù)(parameter)的值只能讀取不能修改,讀取可以使用request.getParameter()讀取;

        屬性(attribute)的值既可以讀取亦可以修改,讀取可以使用request.setAttribute(),設(shè)置可使用request.getAttribute()

        數(shù)據(jù)類型不同:

        參數(shù)(parameter)不管前臺(tái)傳來(lái)的值語(yǔ)義是什么,在服務(wù)器獲取時(shí)都以String類型看待,并且客戶端的參數(shù)值只能是簡(jiǎn)單類型的值,不能是復(fù)雜類型,比如一個(gè)對(duì)象。

        屬性(attribute)的值可以是任意一個(gè)Object類型。

        共同點(diǎn)

        二者的值都被封裝在request對(duì)象中。

        jsp中屬性和參數(shù)有什么區(qū)別

        JSP中的getParameter(String name)和getAttribute(String name)有何區(qū)別?

        也就是Parameter(參數(shù)) 和Attribute(屬性)的區(qū)別?

        在Java中有屬性 和 方法之分,這個(gè)屬性和Attribute(屬性)是不是一樣的?

        參數(shù)就是方法中()里面的內(nèi)容,Parameter和這個(gè)參數(shù)一樣嗎?

        HttpServletRequest類既有g(shù)etAttribute()方法,也由getParameter()方法,這兩個(gè)方法有以下區(qū)別:

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

        (2)當(dāng)兩個(gè)Web組件之間為鏈接關(guān)系時(shí),被鏈接的組件通過(guò)getParameter()方法來(lái)獲得請(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中通過(guò)request.getParameter( "username ")方法來(lái)獲得請(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)組件通過(guò)getAttribute()方法來(lái)和轉(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中通過(guò)getAttribute()方法獲得用戶名字:

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

        Hello: <%=username %>

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

        request.setAttribute()和getAttribute()方法傳遞的數(shù)據(jù)只會(huì)存在于Web容器內(nèi)部,在具有轉(zhuǎn)發(fā)關(guān)系的Web組件之間共享。這兩個(gè)方法能夠設(shè)置Object類型的共享數(shù)據(jù)。

        ——getParameter得到的都是String類型的?;蛘呤莌ttp://a.jsp?id=123中的123,或者是某個(gè)表單提交過(guò)去的數(shù)據(jù)。

        ——getAttribute則可以是對(duì)象。

        ——getParameter()是獲取POST/GET傳遞的參數(shù)值;

        ——getAttribute()是獲取對(duì)象容器中的數(shù)據(jù)值;

        ——getParameter:用于客戶端重定向時(shí),即點(diǎn)擊了鏈接或提交按扭時(shí)傳值用,即用于在用表單或url重定向傳值時(shí)接收數(shù)據(jù)用。

        ——getAttribute:用于服務(wù)器端重定向時(shí),即在sevlet中使用了forward函數(shù),或struts中使用了mapping.findForward。getAttribute只能收到程序用setAttribute傳過(guò)來(lái)的值。

        另外,可以用setAttribute,getAttribute發(fā)送接收對(duì)象.而getParameter顯然只能傳字符串。

        setAttribute是應(yīng)用服務(wù)器把這個(gè)對(duì)象放在該頁(yè)面所對(duì)應(yīng)的一塊內(nèi)存中去,當(dāng)你的頁(yè)面服務(wù)器重定向到另一個(gè)頁(yè)面時(shí),應(yīng)用服務(wù)器會(huì)把這塊內(nèi)存拷貝另一個(gè)頁(yè)面所對(duì)應(yīng)的內(nèi)存中。這樣getAttribute就能取得你所設(shè)下的值,當(dāng)然這種方法可以傳對(duì)象。session也一樣,只是對(duì)象在內(nèi)存中的生命周期不一樣而已。

        getParameter只是應(yīng)用服務(wù)器在分析你送上來(lái)的request頁(yè)面的文本時(shí),取得你設(shè)在表單或url重定向時(shí)的值。
      猜你喜歡

      1..net屬性和特性的區(qū)別

      2.智能電視和網(wǎng)絡(luò)電視的區(qū)別

      3.最新電腦配置屬性如何修改

      3344485