本质上的区别:

request.getAttribute():
总体来说这个getAttribute() 是在页面中获取后台传递来的数据
这个getAttribute() 配套的是setAttribute() , 但是他取值得范围不仅限于setAttribute()的值。通过Model类型的addAttribute() 以及通过ModelAndView的addObject()添加的数据。在页面中都可以通过request.getAttribute() 来获得。不过一般都是使用EL直接取值的方式替代该方法。

//一下三种方式都可以在页面中通过request.getAttribute() 获得
modelMap.addAttribute("object","object"); 
request.setAttribute("aaa", "aaaa");
ModelAndView mav = new ModelAndView();
mav.addObject("msg","hahahahahaha!");

request.getParameter()

该方法主要是后台获取前台页面出传递过来的数据

  1. get方式提交时,连接后边追加的参数。
  2. 表单中提交的参数。
  3. 注: getParameter()是获取不到前台页面中setAttribute()的值的。

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

总结:

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()方法来获得请求参数.

例子: