zoukankan      html  css  js  c++  java
  • ServletContext中常用方法

    、.获取Tomcat的Context的初始化参数。

    1.获取Tomcat的server.xml中设置Context的初始化参数。

    例如:

    1. <Context path="/testcontext" docBase="/context"  
    2.          privileged="true" antiResourceLocking="false" antiJARLocking="false"  
    3.          debug="0" reloadable="true">  
    4.     <Parameter name="name" value="yangqisheng" />  
    5. </Context>  

    方式:getServletContext().getInitParameter(String name)

    2.获取在项目下的web.xml中设置Context的初始化参数。

    例如:

    1. <context-param>  
    2.     <param-name>age</param-name>  
    3.     <param-value>24</param-value>  
    4. </context-param>  

    方式:getServletContext().getInitParameter(String name)

    二、记录Tomcat日志

    1.设置日志文件

    在server.xml文件中,使用logger元素来设置日志文件。

    1. <Logger className="org.apache.catalina.logger.FileLogger"   
    2.         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

    1. this.getServletContext().getResourceAsStream("/WEB-INF/me.properties");  
    2. Properties me = new Properties();  
    3. me.load(is);  
    4. out.write(me.getProperty("name"));  
    5. out.write(me.getProperty("age"));  

    然后在Servlet中执行:

    将会打印出 yangqisheng25


    本文地址:http://blog.csdn.net/yakson/article/details/9203267

    转载请保留出处~!

  • 相关阅读:
    MotionEvent的getX(),getY()与getRawX(),getRawY()区别
    ProgressBar
    Android UI-SlidingMenu侧滑菜单效果
    CentOS采用grub进 single状态
    C++外观设计模式模式(三)
    01背包和背包完全
    Android开展Exception:ActivityNotFoundException: Unable to find explicit activity class
    圆通数据库泄露
    学生有自己的, 其他生活
    串行卧重建14:我们是等自己测试的主动性
  • 原文地址:https://www.cnblogs.com/langtianya/p/5062729.html
Copyright © 2011-2022 走看看