getParameter 是用来接受用post个get方法传递过来的参数的.
getAttribute 必须先setAttribute.

(1)request.getParameter() 取得是通过容器的实现来取得通过类似post,get等方式传入的数据,request.setAttribute()和getAttribute()只是在web容器内部流转,仅仅是请求处理阶段。

(2)request.getParameter() 方法传递的数据,会从Web客户端传到Web服务器端,代表HTTP请求数据。request.getParameter()方法返回String类型的数据。

request.setAttribute() 和 getAttribute() 方法传递的数据只会存在于Web容器内部

还有一点就是,HttpServletRequest 类有 setAttribute() 方法,而没有setParameter() 方法。

拿一个例子来说一下吧,假如两个WEB页面间为链接关系时,就是说要从1.jsp链接到2.jsp时,被链接的是2.jsp可以通过getParameter()方法来获得请求参数.

假如1.jsp里有

  1. <form name="form1" method="post" action="2.jsp">
  2. 请输入用户姓名:<input type="text" name="username">
  3. <input type="submit" name="Submit" value="提交">
  4. </form>

的话在2.jsp中通过request.getParameter("username")方法来获得请求参数username:

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

但是如果两个WEB间为转发关系时,转发目的WEB可以用getAttribute()方法来和转发源WEB共享request范围内的数据,也还是说一个例子吧。

有1.jsp和2.jsp

1.jsp希望向2.jsp传递当前的用户名字,如何传递这一数据呢?先在1.jsp中调用如下setAttribute()方法:

  1. <%
  2. String username=request.getParameter("username");
  3. request.setAttribute("username",username);
  4. %>
  5. <jsp:forward page="2.jsp" />

在2.jsp中通过getAttribute()方法获得用户名字:

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

(1)HttpServletRequest 类有setAttribute()方法,而没有setParameter()方法

(2) 当两个Web组件之间为链接关系时,被链接的组件通过getParameter()方法来获得请求参数,

(3) 当两个Web组件之间为转发关系时,转发目标组件通过getAttribute()方法来和转发源组件共享request范围内的数据。

一般通过表单和链接传递的参数使用getParameter

通过request.setAttribute("name","jerry")的方式赋值的使用request.getAttribute("name")

这个问题主要是request和session的差别,request范围较小一些,只是一个请求,简单说就是你在页面上的一个操作, request.getParameter()就是从上一个页面中的url、form中获取参数,但如果一个request涉及多个类,后面还要取参数,可以用request.setAttribute()和request.getAttribute(),但是当结果输出之后,request就结束了。

而session可以跨越很多页面,可以理解是客户端同一个IE窗口发出的多个请求。这之间都可以传递参数,比如很多网站的用户登录都用到了。

一般可以用getParameter得到页面参数。。。字符串。。。

getAttribute()可以得到对象。。。

getParameter可以得到页面传来的参数如?id=123之类的。

getAttribute()常用于servlet页面传递参数给jsp

st.getParameter() 和request.getAttribute() 区别 https://terryjs.iteye.com/blog/1317610的更多相关文章

  1. request.setAttribute和request.getAttribute还有session.setAttribute和session.getAttribute还有request.getParameter和request.getAttribute区别和联系

    1.session.setAttribute()和session.getAttribute()配对使用,作用域是整个会话期间,在所有的页面都使用这些数据的时候使用. 2.request.setAttr ...

  2. request.getParameter() 和request.getAttribute() 区别

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

  3. request.getparameter和 request.getattribute的差别

    request.getAttribute():是request时设置的变量的值,用request.setAttribute("name","您自己的值");来设 ...

  4. 〖转〗request.getparameter()和request.getAttribute()的区别

    getAttribute表示从request范围取得设置的属性,必须要先setAttribute设置属性,才能通过getAttribute来取得,设置与取得的为Object对象类型 getParame ...

  5. Servlet & JSP - getParameter 与 request.getAttribute 的区别

    getParameter 方法获取的是表单或 URL 的请求参数.参数是从 Web 客户端传递至 Web 服务端.例如有如下的 servlet: @WebServlet(name = "he ...

  6. request.getParameter() 和request.getAttribute()

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/wrs120/article/details/79287607[相同点] 1.都是HttpServle ...

  7. request.getParameter与request.getAttribute()

    这里就request为例,不去考虑session. request对象是javax.servlet.http.HttpServletRequest接口的一个实例,request表示调用JSP页面的请求 ...

  8. request.getParameter()和request.getAttribute()的区别

    request.getParameter("val_1");这是获取请求的参数,比如你在url上看到的?id=12&name=abc就是参数,如果是post请求,就看不到. ...

  9. 数据库中无数据时查询数据为空,但出现空指针异常的解决方案(转载https://blueskator.iteye.com/blog/2096026)

    异常:java.lang.NullPointerException 解决方案: try{ hotelImageList = ihotelImgManager.query(hiqc); }catch(E ...

随机推荐

  1. 流畅的Python (Fluent Python) —— 第一部分

    Python 最好的品质之一是一致性. 魔术方法(magic method)是特殊方法的昵称.特殊方法也叫双下方法. 1.1 一摞Python风格的纸牌 import collections Card ...

  2. linux MySql 主从异步复制

    [root@localhost ~]# hostname master.allentuns ###SLAVE 执行 [root@localhost ~]# sed -i 's@\(HOSTNAME=\ ...

  3. GNU ARM eclipse 安装--Linux 版本

    官方网站:https://gnu-mcu-eclipse.github.io/ 1. 工具链安装 1.1 xpm 安装 安装 nodejs: sudo apt-get install nodejs s ...

  4. 下载放在resource下面的excel文件

    1.将excel文件放项目resources目录下 2.打包的时候排除指定后缀文件,否则打包时会出现文件损坏的情况 <configuration> <encoding>UTF- ...

  5. MySQL新增用户及赋予权限

    创建用户 USE mysql; #创建用户需要操作 mysql 表 # 语法格式为 [@'host'] host 为 'localhost' 表示本地登录用户,host 为 IP地址或 IP 地址区间 ...

  6. fs.mkdir

    fs.mkdir(path[, mode], callback) 要求父目录必须存在 let fs = require('fs'); fs.mkdir('./c/b/a', res=>{ // ...

  7. Django框架架构总览

    Django框架架构总览 理解Django是如何运作的 条目创建于 2013-08-14     1464 views     服务器君一共花费 15.204 ms 进行了 4 次数据库查询,努力地为 ...

  8. Spring Boot关于layui的通用返回类

    1.关于layui的通用返回类 code.count.data.msg public class Msg { private long code = 0; private long count = 0 ...

  9. 【leetcode】Decode Ways

    题目如下: 解题思路:这个题目的本质是一个爬楼梯问题,在爬楼梯问题中,每次可以选择走一步或者两步.而本题也是一样的,解码的时候选择解码一个字符或者两个字符,但是加大了一点点难度,要考虑这些情况.1,Z ...

  10. 面试题常考&必考之--js数组排序冒泡排序和快速排序

    冒泡排序: 原理:比较相邻的元素,将值大的元素交换到右边.(如果相等不进行交换) 实例: 要排列数组:[10,1,35,61,89,36,55] 第一趟排序: 第1次排序:10和1比较,10>1 ...