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. Android 获取屏幕截图 和保存到本地的sd卡路径下

    /** * 获取和保存当前屏幕的截图 */ private void GetandSaveCurrentImage() { //1.构建Bitmap WindowManager windowManag ...

  2. JSP页面动态查询添加数据与分页数据显示

    1 . <%@ page language="java" contentType="text/html; charset=UTF-8"%> < ...

  3. C#读出文本文件内容,遍历数组筛选出 含有汉字对应的拼音字符

    情景描述:由于任务需要,现有一用户表数据,用户名 字段 在新增用户时,输入中文和拼音两种,先要区分同时含有中文和拼音字母的用户名.由于数据很多,可以通过一段代码完成查询: 前提:在阅读本文之前可以先了 ...

  4. 匹配替换指定文本为html标签

    最近看了一道前端面试题,是关于正则的,用尽可能低复杂度的函数,匹配替换指定文本为html标签,题目是这样的: 特定语法匹配替换 说明:匹配字符串中形如 =g文字文字= 的语法,并将相应部分转化为对应的 ...

  5. surface 更新提示

  6. Eclipse键盘输出文字,显示到屏幕上方法

    方法1 /*标准的思路: * 分析: * 1.来源 * 键盘:System.in * 2.目的地 * 屏幕:System.out * 文件:FIle * 3.分别分析:源,目的地流的类型(字符,字节) ...

  7. QQ自动登录里的一些控件知识

    在这个程序里面有个读取计算机指定文件的知识: private void button2_Click(object sender, EventArgs e) { openFileDialog1.Show ...

  8. C#操作IIS服务

    进入正题:先从使用角度来讲解IIS操作,然后再深入到具体的IIS服务底层原理. [1]前提掌握要点: (1).IIS到目前经历了四个版本分别为 IIS4.0 IIS5.0 IIS6.0 IIS7.0, ...

  9. Dynamic programming language

    动态改变运行时结构 Dynamic programming language, in computer science, is a class of high-level programming la ...

  10. Kattis - How Many Digits?

    How Many Digits? Often times it is sufficient to know the rough size of a number, rather than its ex ...