ServletContext对象
来源:互联网 发布:淘宝话费代理怎么做 编辑:程序博客网 时间:2024/05/12 01:39
一句话ServletContext对象
web容器启动时,容器会为每个web应用单独创建一个ServletContext对象。ServletContext对象代表当前应用。
ServletContext context = this.getServletConfig().getServletContext();//获得ServletContext对象context.setAttribute("data", data);String data = (String) context.getAttribute("data");//从ServletContext对象中取出数据
1、多个Servlet通过ServletContext对象实现数据共享
由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现通讯2、获取WEB应用的初始化参数
<!-- 配置WEB应用的初始化参数 --> <context-param> <param-name>url</param-name> <param-value>jdbc:mysql://localhost:3306/test</param-value> </context-param>
ServletContext context = this.getServletContext(); //获取整个web站点的初始化参数String contextInitParam = context.getInitParameter("url");
3、用servletContext实现请求转发
ServletContext context = this.getServletContext();//获取ServletContext对象RequestDispatcher rd = context.getRequestDispatcher("/servlet/ServletContextDemo5");//获取请求转发对象(RequestDispatcher)rd.forward(request, response);//调用forward方法实现请求转发
4、利用ServletContext对象读取资源文件
<span style="white-space:pre"></span><strong>// src目录下的properties文件</strong><span style="white-space:pre"></span>InputStream in = this.getServletContext().getResourceAsStream(<span style="white-space:pre"></span>"/WEB-INF/classes/gacl/servlet/study/db4.properties");<span style="white-space:pre"></span><strong>// FileInputStream</strong><span style="white-space:pre"></span>InputStream in = new FileInputStream(<span style="white-space:pre"></span>"/WEB-INF/classes/db/config/db3.properties");<span style="white-space:pre"></span>// WebRoot目录下的properties配置文件 “/”代表的是项目根目录<span style="white-space:pre"></span>InputStream in = this.getServletContext().getResourceAsStream(<span style="white-space:pre"></span>"/db2.properties");<span style="white-space:pre"></span>Properties prop = new Properties();<span style="white-space:pre"></span>prop.load(in);<span style="white-space:pre"></span>String driver = prop.getProperty("driver")
5、用类装载器读取资源文件
<span style="font-size:14px;">//获取到装载当前类的类装载器 ClassLoader loader = ServletContextDemo7.class.getClassLoader(); //用类装载器读取src目录下的gacl.servlet.study包中的db4.properties配置文件 InputStream in = loader.getResourceAsStream("gacl/servlet/study/db4.properties"); Properties prop = new Properties(); prop.load(in); String driver = prop.getProperty("driver");</span>
0 0
- ServletContext对象:
- servletContext对象
- servletContext对象
- servletContext对象
- serVletContext对象
- ServletContext对象
- servletContext对象
- ServletContext对象
- ServletContext对象
- ServletContext对象
- ServletContext对象
- ServletContext对象
- servletContext对象
- ServletContext对象
- ServletContext对象
- servletconfig对象+servletcontext对象
- Servlet中的ServletContext对象
- ServletConfig对象与ServletContext
- 荣誉勋章 win7下无法正常运行解决方案
- c++ primer第五版(中文)习题答案 第三章第六节-多维数组
- 泛型通配符
- ubuntu环境下安装R
- 115.Oracle数据库SQL开发之 PLSQL编程——循环
- ServletContext对象
- Socket 通信原理机制
- Android Studio打包.so文件教程
- 几个主流的Java连接池整理 (转) (2012-01-04 23:48:02)转载▼
- 远程桌面链接命令
- CCNA学习笔记五 VTP vlan turnk protcol
- 直线生成算法——中点画线法(Middpoint)
- 学习日志(1)
- Codeforces Round #297 (Div. 2) E. Anya and Cubes (双向DFS)