J2EE——GetAttribute()与GetParameter()的区别

1.获取的值不同

getAttribute表示从request范围取得设置的属性,必须要通过setAttribute设置属性,才能通过getAttribute取得。设置和取得的值都是Object类型。

getParameter表示接收页面提交的 参数,主要有表单提交的参数、URL重写传递的参数(http://item.jsp?id=1中的id值)等,所以不需要通过setParameter来设置参数,而且参数的返回类型是String类型。

2.数据传递路径不同

request.getParameter()方法传递的数据是从web客户端到web服务端,代表http的请求数据,用于表单或URL重定向时使用。

request.getAttribute()传递的数据只存在于web容器内部,在request范围内也就是转发时web组件之间的共享。

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

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

参考:

https://blog.csdn.net/yiwangxiblog/article/details/55814544

https://www.cnblogs.com/keyi/p/6125626.html

另:

关于getParameter()与getInitParameter()

https://blog.csdn.net/qq_35873847/article/details/77988619

一个web项目web.xml的配置中<context-param>配置作用

https://blog.csdn.net/qq_33267676/article/details/79070825

https://www.cnblogs.com/yqskj/articles/2233061.html

ServletConfig对象详解

https://www.cnblogs.com/lashou/p/6068775.html

猜你喜欢

转载自blog.csdn.net/qq_41596538/article/details/81116139