码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-09-01 21:11:57      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:

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

 

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

标签:

原文地址:http://www.cnblogs.com/mada0/p/4776799.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!