request.getAttribute() 和 request.getParameter() 有何区别?

1.getParameter 得到的都是 String 类型的。或者是 http://a.jsp?id=123 中的 123,或者是某个表单提交过去的数据。

getAttribute 则可以是对象。

 

2.getParameter()是获取 POST/GET 传递的参数值;

getAttribute()是获取对象容器中的数据值;

 

3.getParameter:用于客户端重定向时,即点击了链接或提交按钮时传值用,即用于在用表单

或 url 重定向传值时接收数据用。

getAttribute:用于服务器端重定向时,即在 sevlet 中使用了 forward 函数,或 struts 中使用了mapping.findForward。 getAttribute 只能收到程序用 setAttribute 传过来的值。

 

4.getParameter()是获取 POST/GET 传递的参数值;

getAttribute()是获取 SESSION 的值;

另外,可以用 setAttribute,getAttribute 发送接收对象.而 getParameter 显然只能传字符串。

setAttribute 是应用服务器把这个对象放在该页面所对应的一块内存中去,当你的页面服务器重定向到另一个页面时,应用服务器会把这块内存拷贝另一个页面所对应的内存中。这样

getAttribute 就能取得你所设下的值,当然这种方法可以传对象。 session 也一样,只是对象在内存中的生命周期不一样而已。 getParameter 只是应用服务器在分析你送上来的 request页面的文本时,取得你设在表单或 url 重定向时的值。

 

5.getParameter 返回的是 String, 用于读取提交的表单中的值;

getAttribute 返回的是 Object,需进行转换,可用 setAttribute 设置成任意对象,使用很灵活,

可随时用;

猜你喜欢

转载自blog.csdn.net/cao2219600/article/details/82909542