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. node 上传文件 http client to post file

    node做http client 发送post数据是很容易的事情,但要上传文件就不是太容易了主要是因为上传文件的报文和普通post是不太一样的 要了解http post可以看下这个 https://i ...

  2. 具体图解 Flume介绍、安装配置

    写在前面一: 本文总结"Hadoop生态系统"中的当中一员--Apache Flume 写在前面二: 所用软件说明: 一.什么是Apache Flume 官网:Flume is a ...

  3. 2015.04.29,外语,读书笔记-《Word Power Made Easy》 14 “如何谈论日常现象” SESSION 39

    HOW TO TALK ABOUT COMMON PHENOMENA AND OCCURRENCES TEASER PREVIEW dire(['daiә(r)] adj. 可怕的,悲惨的,灾难警告的 ...

  4. Caffe C++API 提取任意一张图片的特征系列二----MemoryData

    介绍一种更加灵活的方法,用MemoryData层输入数据,可以直接用opencv接口读入我们的图片再添加的网络中.  第一个问题:仍然是工程建立问题,提示卷积层或其他层没有注册,解决方法与上一篇博客一 ...

  5. 关于volatile的一些思考C++

    在c++中,volatile用与修饰容易变动的变量,通常用于多线程的标志,编译器会存在代码优化,假如在同一个大括号中没有修改这么一个参数,那么编译器很可能在读取这个值的时候使用的是快取的方法,即将这个 ...

  6. c#中集成Swagger

    Swagger是什么? 官方说法:Swagger是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文 ...

  7. BZOJ 1786 DP

    思路: 肯定从小往大填合适了 f[i][j]表示第i个数是j的最少逆序对数 f[i][j]=min(f[i-1][k]+cost,f[i][j]) 优化一下成O(nk)就好啦~ (不优化也可以过的-) ...

  8. 解决:[WARNING] fpm_children_bury(), line 215: child 2736 (pool default) exited on signal 15 SIGTERM after 59.588363 seconds from start

    试用Nginx + PHP FastCGI 做WEB服务器,运行了几个月的时间,烦恼的是经常碰到Nginx 502 Bad Gateway 这个问题. 参考了很多修改办法,这个502的问题一直存在,今 ...

  9. php链接memcache操作

    设置值 set key 压缩标识 有效期 长度 set name 0 60 5 hello 压缩标识:用于告诉memcached服务器是否压所后存储数据,目的是为了节省磁盘空间,压所和解压缩会消耗时间 ...

  10. Vue模拟酷狗APP问题总结

    一.NewSongs.vue中的38行  this.$http.get('/proxy/?json=true')   里面这个路径的获取 二.router文件夹中的index.js  中的  comp ...