Servlet技术中的对象-ServletConfig与ServletContext
来源:互联网 发布:matlab中矩阵运算 编辑:程序博客网 时间:2024/06/05 12:00
ServletConfig
this.getServletConfig();
在配置Servlet时,可以使用一个或多个标签为Servlet配置一些初始化参数,而这些参数,在Web容器创建Servlet实例化对象时,会自动封装到ServletCofig对象中,并且在调用servlet的init方法时传递给servlet。
即,通过这个对象可以得到配置信息
ServletContext
this.getServletConfig().getServletContext(); this.getSetvletContext();
Web容器在启动时会为每个Web应用创建一个ServletContext对象,这个对象就代表当前Web应用。
应用:
1. 全局域对象
所有Servlet共享同一个ServletContext,因此可以把它看作当前Web应用的全局对象,来保存一些全局共享的数据。 this.getServletContext().setAttribute("xx", Object);
2. 实现Servlet的转发 this.getServletContext().getRequestDispatcher("/1.jsp").forward(request, response);
3. 读取资源文件 InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/db.properties");
如果使用传统方法读取资源文件,一定要注意路径问题,一般这样做: String absolutePath = this.getServletContext().getRealPath("/WEB-INF/db.properties");
使用类加载器去读取资源文件
UserDao.class.getClassLoader().getResourceAsStream("db.properties");
应注意:以类路径为依照(eclipse中是src目录)
并且类装载器只会对文件进行一次读取,并将文件装载进内存(多次读取、太大文件,不建议用这种读取方式)转发与重定向的区别:
转发时并不会进行第二次请求,而是直接带着request对象就去转发到的资源
重定向会重新进行请求,即浏览器会发出两次请求。
转发是地址栏不会变化,重定向时会变化
- Servlet技术中的对象-ServletConfig与ServletContext
- Servlet之ServletConfig与ServletContext对象
- ServletConfig对象与ServletContext
- ServletConfig与ServletContext对象
- Servlet--ServletConfig与ServletContext应用
- javaweb-servlet- ServletConfig与ServletContext
- JavaWeb(一)Servlet中的ServletConfig与ServletContext
- ServletConfig对象与ServletContext区别
- ServletConfig对象与ServletContext区别
- ServletConfig与ServletContext对象详解
- ServletConfig与ServletContext对象详解
- ServletConfig对象与ServletContext区别
- ServletConfig与ServletContext对象详解
- ServletConfig与ServletContext对象详解
- ServletConfig与ServletContext对象详解
- ServletConfig与ServletContext对象详解
- ServletConfig与ServletContext对象详解
- ServletConfig与ServletContext对象详解
- android连接wifi相关知识
- 字符串及其操作函数
- zoj3471 Most Powerful 状压dp
- 最新版SDWebImage的使用
- hdoj 2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活【背包问题】
- Servlet技术中的对象-ServletConfig与ServletContext
- Wiki Formatting help table
- leetcode Binary Tree Paths
- 【工业串口和网络软件通讯平台(SuperIO)教程】八.SuperIO通讯机制与设备驱动对接的说明
- Unity游戏开发中的对象池
- 指针和地址的区别
- Shell脚本中遇到的一些细节问题
- 【JavaSE】day04_Collection_Iterator_新循环_泛型
- Android周报