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. 【学习总结】快速上手Linux玩转典型应用-第6章-linux常用命令讲解

    课程目录链接 快速上手Linux玩转典型应用-目录 目录 1. 软件操作命令 2. 服务器硬件资源信息 3. 文件操作命令 4. Linux文本编辑神器vim与其他常用命令 5. 系统用户操作命令 6 ...

  2. DRF之三大认证

    一.用户认证Authorticatons 1.源码解析 第一步. 找入口 def dispatch(self, request, *args, **kwargs): # 1.首先我们进入的是APIVi ...

  3. Qt和其它GUI库的对比

    http://c.biancheng.net/view/3876.html 世界上的 GUI 库多如牛毛,有的跨平台,有的专属于某个操作系统:有的只有 UI 功能,有的还融合了网络通信.多媒体处理.数 ...

  4. Django中利用type动态操作数据库表

    场景分析: 后台MySql数据库保存了一大批按股票代码命名的数据表,每张表保存的是每只股票的日线数据. stock_000002 stock_600030 stock_600020 ...一共3000 ...

  5. Red Hat Enterprise Linux 8.0 安装

    Red Hat Enterprise Linux 8.0 安装 本次安装通过使用VMware Workstation 15 pro 进行. 1.新建虚拟机 2.点击首页的创建新的虚拟机,或者点击标签栏 ...

  6. 牛客练习赛53E 老瞎眼 pk 小鲜肉(线段树)

    链接:https://ac.nowcoder.com/acm/contest/1114/E来源:牛客网题目:老瞎眼有一个长度为 n 的数组 a,为了为难小鲜肉,他准备了 Q 次询问,每次给出 一个区间 ...

  7. Comet OJ - Contest #0 A题 解方程 (数学)

    题目描述 小象同学在初等教育时期遇到了一个复杂的数学题,题目是这样的: 给定自然数 nn,确定关于 x, y, zx,y,z 的不定方程 \displaystyle \sqrt{x - \sqrt{n ...

  8. 1129. Recommendation System (25)

    Recommendation system predicts the preference that a user would give to an item. Now you are asked t ...

  9. [每日一学]apache camel|IBMWebsphere MQ header issue|MQRFH2 |MQSTR

    最近工作中,遇到一个很奇怪的问题: 现象:在camel开发中,通过 IBM Websphere MQ 给assasin 发送xml的message时,会多出<mcd>等这样的header出 ...

  10. Maven高级

    第一章 Maven解决冲突的方式 1.1 第一声明者优先原则 那个jar包的坐标在pom.xml文件上属于靠上的位置,这个jar包就是先声明的.先声明的jar包坐标下的依赖包,可以优先进入项目中. 示 ...