ServletContext中常用方法介绍
2013-07-02 15:56
495 查看
一、.获取Tomcat的Context的初始化参数。
1.获取Tomcat的server.xml中设置Context的初始化参数。
例如:
<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的初始化参数。
例如:
复制代码 代码如下:
<context-param>
<param-name>age</param-name>
<param-value>24</param-value>
</context-param>
方式:getServletContext().getInitParameter(String name)
二、记录Tomcat日志
1.设置日志文件
在server.xml文件中,使用logger元素来设置日志文件。
<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
this.getServletContext().getResourceAsStream("/WEB-INF/me.properties");
Properties me = new Properties();
me.load(is);
out.write(me.getProperty("name"));
out.write(me.getProperty("age"));
然后在Servlet中执行:
将会打印出 yangqisheng25
您可能感兴趣的文章:
相关文章推荐
- ServletContext中常用方法介绍
- ServletContext中常用方法介绍
- 浅析VS2010反汇编 VS 反汇编方法及常用汇编指令介绍 VS2015使用技巧 调试-反汇编 查看C语言代码对应的汇编代码
- StringUtils常用方法+StringUtils详细介绍
- jquery中常用的方法简单介绍
- 电脑中的常用密码破解方法介绍
- JQuery的常用选择器、过滤器、方法祥细介绍
- 前端JS/JQuery常用封装的方法介绍
- cocos2d-x-2.0新增加了几个UI控件,这里我介绍下常用的这几个UI控件(CCControlSlider、CCControlSwitch、CCControlColourPicker)使用方法
- 互联网开发常用调试工具介绍及使用方法
- Socket①:常用方法介绍
- ServletContext中常用方法(getRsource和getResourceAsStream)
- InetAddress类的常用方法介绍
- java 中获取ServletContext中常用的方法
- Python3中常用的处理时间和实现定时任务的方法的介绍
- VS 反汇编方法及常用汇编指令介绍
- 【书籍简评】《Python计算机视觉编程》还算可以的入门书,简单介绍了CV中的常用方法,但由于代码版本过老,有一些用3.6跑不通。
- Json数据的序列化与反序列化的三种常用方法介绍
- VC 6.0常用控件使用方法介绍
- 结合Scikit-learn介绍几种常用的特征选择方法