ServletConfig和ServletContext详解
来源:互联网 发布:java对象什么意思 编辑:程序博客网 时间:2024/06/04 01:14
ServletConfig
- ServeltConfig代表了Servlet的配置信息的对象。
- 在web.xml中配置Servelt的初始化参数
如:
<servlet> <servlet-name>Demo1</servlet-name> <servlet-class>com.demo.Demo1</servlet-class> <init-param> <param-name>data</param-name> <param-value>xxx</param-value> </init-param> </servlet>
- 在Demo1中,我们可以直接获取到配置信息
ServletConfig config = this.getServletConfig();String str = config.getParamater("data");
- ServletConfig的生命周期
他的声明周期和Servlet是一样的。
在初始化Servlet时(init方法执行),web服务器就把web.xml中Servlet的配置信息封装到Config传递给init(config)了。当Servlet对象摧毁时,ServletConfig也就没有用了,随着一起摧毁了。
ServletContext
ServletContext代表的是当前的web应用。
这个东西的作用可就大了去了。
可以当做“域”来使用,即Context域
这个域的作用范围是整个web应用。也就是说,这个容器只有一个,且给web应用下的所有的Servlet用,且共享(A用户将<”data”,”a”>存入域后,B用户再将<”data”,”b”>存入域中,A用户取出data来发现变成了b)可以获取Web应用的初始化参数
( 这个要区别于上面的初始化参数。ServletConfig是针对某一个Servlet,而ServletContext是针对的这个web应用。)
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <!-- 配置了Context的初始化参数 --> <context-param> <param-name>data1</param-name> <param-value>yyy</param-value> </context-param> <servlet> <servlet-name>Demo1</servlet-name> <servlet-class>com.demo.Demo1</servlet-class> <!-- 配置了ServletConfig的初始化参数 --> <init-param> <param-name>data</param-name> <param-value>xxx</param-value> </init-param> </servlet></web-app>
//在web应用的任一Servlet中均可以获取ServletContext的配置信息ServletContext context = this.getServletContext();String data1 = context.getInitParamater("data1");//yyy
- 可以实现请求的转发
ServletContext context=this.getServletContext();context.getRequestDispatcher("/index.jsp").forward(request, response);
读取配置文件
首先我们使用传统的方式来读取配置文件
FileInputStream in = new FileInputStream("src/1.properties");//我们直接把配置文件放在src下面 Properties prop = new Properties(); prop.load(in); prop.getProperty("name");//随便从配置文件中获取一个属性
这回抛出了500,没有找到相应的文件。
这是为什么?因为new File(url)、new FileInputStream(url)……是使用的相对路径,相对的是JVM目录。在普通的Java工程中JVM的路径就是Java工程所在的路径。但是,在web工程中就不一样了,JVM的路径是tomcat的启动目录。- 读取web应用中的资源可以使用ServletContext的getResourceAsStream()
InputStream in = this.getServletContext().getResourceAsStream(url);//资源的路径Properties prop = new Properties();prop.load(in);
这里的资源路径是很重要的:1)在src目录下面的资源文件会映射到WEB-INF/classes下面,所以他的url是`/WEB-INF/classes/资源文件名`2)在包中的资源文件会随着类一起映射,所以他的url是`/WEB-INF/classes/cn/demo/资源文件名`(这里的包是cn.demo)3)在WEB-ROOT下面的资源文件,直接就是在web应用下面,他的url是`/WEB-INF/资源文件名`
- 读取web应用中的配置文件经常可以使用ServletContext的getResource来获取资源的URL
String path = this.getServletContext().getResource("/WEB-INF/classes/1.porperties"). getPath();//获取URL的路径 FileInputStream in = new FileInputStream(path); //String filename = path.subString(path.lastIndexOf("/")+1); Properties prop = new Properties(); prop.load(in); String name = prop.getProperty("name"); System.out.println(name);
这回可以成功获取,这种方法最常用的原因就是他能够获取文件的名
- 扩展:
web工程中的普通java程序如何读取资源文件,使用类装载器。
InputStream in = this.getClass().getClassLoader().getResourceAsStream("1.properties");//配置文件放在src下,和类加载器是同一个目录Properties prop = new Properties();prop.load(in);
这种方法只适用于 只会读不会改的文件,而且我们只需要读一次,做成静态的代码跨即可。但是,若是对资源文件进行修改后,就不能读取到最新的。因为类加载器加载过资源以后,就不会再次加载了,只加载一次。
String path = this.getClass().getClassLoader().getResource("1.properties").getPath();//配置文件放在src下,和类加载器是同一个目录FileInputStream in = new FileInputStream("d1.properties");Properties prop = new Properties();prop.load(in);
0 0
- ServletConfig和ServletContext详解
- ServletConfig对象和ServletContext对象的详解
- Servlet中的ServletContext和ServletConfig详解
- ServletConfig与ServletContext详解
- ServletContext和servletconfig区别
- ServletContext和ServletConfig分析
- ServletContext,ServletConfig和Sesion
- ServletConfig和ServletContext
- ServletConfig和ServletContext
- ServletConfig和ServletContext
- ServletConfig和ServletContext
- ServletConfig和ServletContext
- (24)ServletConfig和ServletContext
- ServletConfig和ServletContext
- ServletConfig和ServletContext
- ServletConfig和ServletContext
- ServletConfig和ServletContext
- ServletConfig和ServletContext
- Android 随笔——下载图片
- 自考结束,细数点滴
- android让控件基于底部
- 3360 学生信息的添加与查询
- M.2接口固态硬盘安装WIN7系统
- ServletConfig和ServletContext详解
- 矩阵快速幂
- 自考总结
- JVM调优总结(四)-垃圾回收面临的问题
- LeetCode 题目 :455. Assign Cookies
- LeetCode-419. Battleships in a Board
- JiLi Number HihoCoder
- Linux常用操作指令
- 车牌识别