ServletContext中常用方法(getRsource和getResourceAsStream)
2014-05-10 01:47
246 查看
转自:http://blog.csdn.net/yakson/article/details/9203267
一、.获取Tomcat的Context的初始化参数。
1.获取Tomcat的server.xml中设置Context的初始化参数。
例如:
[html] view plaincopy
<Context path="/testcontext" docBase="/context"
privileged="true" antiResourceLocking="false" antiJARLocking="false"
debug="0" reloadable="true">
<Parameter name="name" value="yangqisheng" />
</Context>
方式:getServletContext().getInitParameter(String name)
2.获取在项目下的web.xml中设置Context的初始化参数。
例如:
[html] view plaincopy
<context-param>
<param-name>age</param-name>
<param-value>24</param-value>
</context-param>
方式:getServletContext().getInitParameter(String name)
二、记录Tomcat日志
1.设置日志文件
在server.xml文件中,使用logger元素来设置日志文件。
[html] view plaincopy
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_log." suffix=".txt" timestamp="true"/>
写日志:this.getServletContext().log("测试")
三、访问资源文件
3.1 getResource(String parh)方法:其中path必须是/开头,代表当前web应用程序的根目录。返回返回的一个代表某个资源的URL对象。
3.2 getResoutceAsStream(String parh),返回文件流。这个好处是可以使用相对于根目录的路径访问到web目录下的所有文件,而不必知道绝对路径。
例如在WEB-INF下新建文件me.properties,内容为:
name=yangqisheng
age=25
[java] view plaincopy
this.getServletContext().getResourceAsStream("/WEB-INF/me.properties");
Properties me = new Properties();
me.load(this.getServletContext().getResourceAsStream("/WEB-INF/me.properties"));
out.write(me.getProperty("name"));
out.write(me.getProperty("age"));
然后在Servlet中执行:
将会打印出 yangqisheng25
本文地址:http://blog.csdn.net/yakson/article/details/9203267
转载请保留出处~!
一、.获取Tomcat的Context的初始化参数。
1.获取Tomcat的server.xml中设置Context的初始化参数。
例如:
[html] view plaincopy
<Context path="/testcontext" docBase="/context"
privileged="true" antiResourceLocking="false" antiJARLocking="false"
debug="0" reloadable="true">
<Parameter name="name" value="yangqisheng" />
</Context>
方式:getServletContext().getInitParameter(String name)
2.获取在项目下的web.xml中设置Context的初始化参数。
例如:
[html] view plaincopy
<context-param>
<param-name>age</param-name>
<param-value>24</param-value>
</context-param>
方式:getServletContext().getInitParameter(String name)
二、记录Tomcat日志
1.设置日志文件
在server.xml文件中,使用logger元素来设置日志文件。
[html] view plaincopy
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_log." suffix=".txt" timestamp="true"/>
写日志:this.getServletContext().log("测试")
三、访问资源文件
3.1 getResource(String parh)方法:其中path必须是/开头,代表当前web应用程序的根目录。返回返回的一个代表某个资源的URL对象。
3.2 getResoutceAsStream(String parh),返回文件流。这个好处是可以使用相对于根目录的路径访问到web目录下的所有文件,而不必知道绝对路径。
例如在WEB-INF下新建文件me.properties,内容为:
name=yangqisheng
age=25
[java] view plaincopy
this.getServletContext().getResourceAsStream("/WEB-INF/me.properties");
Properties me = new Properties();
me.load(this.getServletContext().getResourceAsStream("/WEB-INF/me.properties"));
out.write(me.getProperty("name"));
out.write(me.getProperty("age"));
然后在Servlet中执行:
将会打印出 yangqisheng25
本文地址:http://blog.csdn.net/yakson/article/details/9203267
转载请保留出处~!
相关文章推荐
- 关于Servlet中getServletContext() 的getResourceAsStream(String path) 方法中的path路径问题
- Java绝对/相对路径获取与getResourceAsStream()方法
- Java 使用getClass().getResourceAsStream()方法获取资源
- java加载外部文件数据到代码中:外部数据文件放到jar包中,调用方法getResourceAsStream
- java加载外部文件数据到代码中:外部数据文件放到jar包中,调用方法getResourceAsStream
- java.lang.ClassLoader中方法getResourceAsStream与ClassName.class.ResourceAsStream方法两者路径的区别
- 对Class.getResourceAsStream和ClassLoader.getResourceAsStream方法所使用的资源路径的解释
- android开发 通过class.getResourceAsStream()方法 获得资源。
- Resources.class.getResourceAsStream 获取配置的方法
- Java绝对/相对路径获取与getResourceAsStream()方法
- Java中的getResourceAsStream方法
- 对Class.getResourceAsStream和ClassLoader.getResourceAsStream方法所使用的资源路径的解
- ClassLoader.getResourceAsStream(name);获取配置文件的方法
- ClassLoader.getResourceAsStream(name);获取配置文件的方法
- ClassLoader.getResourceAsStream(name);获取配置文件的方法
- ClassLoader.getResourceAsStream(name);获取配置文件的方法
- getClass().getResourceAsStream() .获取配置文件的方法 .
- (转)java getResourceAsStream的使用方法
- getResourceAsStream()方法
- 关于Object.class.getResourceAsStream方法读取文件的使用