HttpServletRequest.getParameter("modelName");能取到想要的modelObject吗?经过測试之后。发现是不能的。

后来想想。其它道理挺简单的,当两个Web组件之间为转发关系时,转发源会将要共享request范围内的数据先用setAttribute将数据放入到HttpServletRequest对象中,然后转发目标通过getAttribute方法来取得要共享的数据。而MVC中用的就是Web组件之间的转发啊!真是笨。怎么当时没有想到呢?

以下整理一下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传递当前的username字, 怎样传递这一数据呢?先在authenticate.jsp中调用setAttribute()方法:



        <%

        String username=request.getParameter("username");

        request.setAttribute("username",username);

        %>



        <jsp:forward page="hello.jsp" />



        在hello.jsp中通过getAttribute()方法获得username字:



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

        Hello: <%=username %>



        从更深的层次考虑,request.getParameter()方法传递的数据。会从Webclient传到Webserver端。代表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方法的区别

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

  2. request中getParameter和getAttribute的区别

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

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

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

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

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

  5. request:getParameter和getAttribute区别

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

  6. request:getParameter getAttribute

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

  7. JSP中request getParameter和getAttribute不同(转载)

    (1)request.getParameter()取得是通过容器的实现来取得通过类似post,get等方式传入的数据,,request.setAttribute()和getAttribute()只是在 ...

  8. JSP中getParameter和getAttribute区别

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

  9. Servlet中的request对象、重定向和转发的差别(6)

    1.这里所说的request对象指的是HttpServletRequest对象,它代表client的请求,当client通过HTTP协议訪问server时.HTTP请求头中的全部信息都封装在这个对象中 ...

随机推荐

  1. C# 跨线程调用form控件技巧及byte[]与string型相互转换

    跨线程调用form控件技巧 private delegate void MethodSocket(object obj);//使用托管 ss = "OK"; this.BeginI ...

  2. caffe-ssd使用预训练模型做目标检测

    首先参考https://www.jianshu.com/p/4eaedaeafcb4 这是一个傻瓜似的目标检测样例,目前还不清楚图片怎么转换,怎么验证,后续继续跟进 模型测试(1)图片数据集上测试 p ...

  3. WSL初体验

    Windows10 新增加了WSL子系统, 体验了一下感觉还不错...   开启对应的功能后, 在商店里选择安装Ubuntu就可以了. 迁移文件系统 WSL 的文件系统位于 C 盘,当安装的软件越来越 ...

  4. oracle 优化之组合索引

    组合索引适用场景: 1.适用在单独查询返回记录很多,组合查询后忽然返回记录很少的情况: 比如where 学历=硕士以上 返回不少的记录 比如where 职业=收银员 同样返回不少的记录 于是无论哪个条 ...

  5. Repeater 中 OnItemCommand 用法

    <table> <asp:Repeater ID="rptList" runat="server"OnItemCommand="rp ...

  6. ROS命令参考

    前言:整理一些ROS常用命令,参考自:<ROS机器人编程>. 一.ROS执行命令 二.ROS信息命令 三.ROS catkin命令 四.ROS功能包命令 -END-

  7. 基于Socket的Winform例子

    一.直接上效果图 二.Socket握手 三.服务端 Thread threadWatch = null;// 负责监听客户端的线程 Socket socketWatch = null;// 负责监听客 ...

  8. Redis的配置文件详解

    daemonize:如需要在后台运行,把该项的值改为yes pdifile:把pid文件放在/var/run/redis.pid,可以配置到其他地址 bind:指定redis只接收来自该IP的请求,如 ...

  9. js 数据类型判断

    判断type类型 isString (o) { //是否字符串 return Object.prototype.toString.call(o).slice(8, -1) === 'String' } ...

  10. Unity5.X 开发资源介绍

    Asset 资源 Category 类别 Publisher 开发商 Rating 评级 Version 版本号   Windows → Asset Store 资源商店 [Ctrl + 9]   U ...