您的位置:首页 > 编程语言 > PHP开发

Servlet中getInitParameter(),getParameter(),getAttribute()异同

2015-09-01 20:29 711 查看
1.ServletConfig和ServletContext之间getInitParameter()方法的区别:

ServletConfig#getInitParameter()作用于单个Servlet

ServletContext#getInitParameter()作用于全局Servlet

作用域不同,但都需要先在web.xml中配置,都只能返回String类型变量

HttpServletRequest#getParameter():获取网页中表单里元素的name值,客户端向服务器返回数据,只能返回String类型变量

HttpServletRequest#setAttribute()和getAttribute()在HttpServletRequest中实现数据保存,在一个HttpServletRequest生命周期中使用。在相同Servlet的doGet(),doPost()方法和jsp页面传递参数,在jsp页面要强制转换。

ServletContext通过setAttribute()和getAttribute()在同一项目下的Servlet中实现数据共享,同一项目下所有Servlet共享一个ServletContext实例,单例模式。

这两者返回值是Object。

参考阅读:
http://stackoverflow.com/questions/18490517/jsp-getattribute-returning-null http://stackoverflow.com/questions/5243754/difference-between-getattribute-and-getparameter http://blog.csdn.net/liqinghuan_blog/article/details/11486017
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: