下面整理一下getParameter和getAttribute的区别和各自的使用范围。
      (1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法
      (2)当两个Web组件之间为链接关系时,被链接的组件通过getParameter()方法来获得请求参数,例如假定welcome.jsp和authenticate.jsp之间为链接关系,welcome.jsp中有以下代码:
      <a  href="authenticate.jsp?username=wolf">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提交过来的数据。

Request的getParameter和getAttribute方法的区别的更多相关文章

  1. Request的getParameter和getAttribute方法的差别

    HttpServletRequest.getParameter("modelName");能取到想要的modelObject吗?经过測试之后.发现是不能的. 后来想想.其它道理挺简 ...

  2. request.getAttribute()和request.getParameter()两个方法的区别

    request.getAttribute()获得的数据必须曾经有过setAttibute()过: 而request.getParameter()获得是客户端POST或者GET请求时所携带的参数的值 g ...

  3. request中getParameter和getAttribute的区别

    整理一下getParameter和getAttribute的区别和各自的使用范围. (1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方 ...

  4. getParameter和getAttribute有什么区别

    1.getAttribute是取得jsp中 用setAttribute設定的attribute 2.parameter得到的是string:attribute得到的是object 3.request. ...

  5. 【转】HttpServletRequest.getParameter() &HttpServletRequest.getAttribute() 区别

    Ref: HttpServletRequest的getParameter和getAttribute方法有什么区别 具体如下几点: (1)HttpServletRequest类有setAttribute ...

  6. request:getParameter和getAttribute区别

    getParameter 是用来接受用post个get方法传递过来的参数的.getAttribute 必须先setAttribute.(1)request.getParameter() 取得是通过容器 ...

  7. request:getParameter getAttribute

    转载自:http://www.cnblogs.com/shaohz2014/p/3804656.html 在浏览器地址输入,表示传入一个参数test,值为123 http://localhost:88 ...

  8. JSP中getParameter和getAttribute区别

    (1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法 (2)当两个Web组件之间为链接关系时,被链接的组件通过getParameter ...

  9. getParameter和getAttribute区别

    (1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法 (2)当两个Web组件之间为链接关系时,被链接的组件通过getParameter ...

随机推荐

  1. POJ 3349 Snowflake Snow Snowflakes Hash

    题目链接: http://poj.org/problem?id=3349 #include <stdio.h> #include <string.h> #include < ...

  2. 启动Tomcat出现Using CATALINA_BASE

    有一次命令行启动Tomcat的时候,出现: Using CATALINA_BASE: "D:\apache-tomcat-6.0.35"Using CATALINA_HOME: & ...

  3. Socket实现简单的聊天通信

    最近学习了Socket后,感觉Socket挺好玩的,在博客中看到socket在实时聊天功能的很强大,于是乎就做了一个简单的聊天功能,今天贴出来,能够与大家一起共享,有不对之处,能够给予指出,谢谢! 服 ...

  4. 如何使用Github仓库创建网站

    官方文档:https://help.github.com/categories/github-pages-basics/ 1.创建一个仓库 2.额外建立一个gh-pages分支 3.添加CNAME文件 ...

  5. c++ 函数的函数声明

    c++ 函数的函数声明 只要在被调用函数的首部的末尾加一个分号,就成为对该函数的函数声明.函数声明的位置应当在函数调用之前. #include <iostream> using names ...

  6. 【技术贴】解决QQ空间发表文章手机不显示换行

    采用HTML模式,在需要换行的地方加入如下代码. <div><span style="font-family:微软雅黑;font-size:16px"> & ...

  7. 使用 .NET 平台,如何玩转 Universal Windows 应用?

    2015年7月30日 本文作者是 Managed Languages 团队项目经理 Lucian Wischik. 不久前,Visual Studio 2015上新增 Windows 10 应用的开发 ...

  8. 【BZOJ1901】 Zju2112 Dynamic Rankings(树套树)

    [题意] 给定一个含有n个数的序列a[1],a[2],a[3]--a[n], 程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]--a[j]中第k小的数是多少(1≤k ...

  9. "NetworkError: 500 Internal Server Error - http://develop.console.aliyun.sinopec.com/ots/ots_queryOtsList.action?state=0"

    项目明明开始好好的,报的这个错,错误提示也很少,啥信息都没有,只是明白是服务器报了500,知道是服务器内部错误,但是却没法找不到问题所在.后来突然想到把下面报错的action直接在浏览器运行: htt ...

  10. [转贴] 从零开始学C++之异常(二):程序错误、异常(语法、抛出、捕获、传播)、栈展开

    一.程序错误 编译错误,即语法错误.程序就无法被生成运行代码. 运行时错误 不可预料的逻辑错误 可以预料的运行异常 例如: 动态分配空间时可能不会成功 打开文件可能会失败 除法运算时分母可能为0 整数 ...