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
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
相关文章推荐
- 常用排序算法PHP最精确写法
- php中self和$this的用法
- PHP数组函数
- php包管理机制-支持php5.3的方法
- PHP异步并发访问mysql简单实现
- 简单PHP性能记录工具
- SimpleFork php多进程并发框架
- PHP如何正常转换中文json格式字符串
- PHP Curl模拟登录并抓取数据
- php在windows上扩展pthreads多线程
- PHP缓存函数apc
- Yii2-条件Where使用说明
- Ajax在PHP中的使用
- FTP工具下载的文件用DW打开中文乱码记事本却不会
- CakePHP下使用paginator需要对多个字段排序的做法
- php判断来源网址地址并且限制非法来源
- php事件钩子
- 机房收费系统中dtpicker控件的使用
- php利用zendframework编程实例
- 终于解决了PHP调用SOAP过程中的种种问题