ServletContext的揭秘
----------------------------- ServletContext的揭秘------------------------------
ServletContext
- WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用。
- 由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现通讯。ServletContext对象通常也被称之为context域对象。
- 查看ServletContext API文档,了解ServletContext对象的功能。
应用:
获取WEB应用的全局初始化参数
通过ServletContext对象实现数据共享
-
- 案例--- 统计站点访问次数
利用ServletContext对象读取资源文件
getAttribute |
|
getContextPath |
|
getInitParameter |
|
getRealPath |
|
|
setAttribute |
getResourceAsStream |
全局参数获取代码示例:
package com.rl.servlet;
import java.io.IOException;
import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
public class ServletDemo1 extends HttpServlet {
@Override public void init(ServletConfig config) throws ServletException { //获得ServletContext对象 ServletContext sc = config.getServletContext(); //获得ServletContext级别的全局参数 String contextValue = sc.getInitParameter("context_key"); System.out.println(contextValue);
} } |
<?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"> <!-- 全局的容器参数ServletContext --> <context-param> <param-name>context_key</param-name> <param-value>context_value</param-value> </context-param>
<servlet> <servlet-name>ServletDemo1</servlet-name> <servlet-class>com.rl.servlet.ServletDemo1</servlet-class> <load-on-startup>1</load-on-startup> </servlet>
<servlet-mapping> <servlet-name>ServletDemo1</servlet-name> <url-pattern>/servletDemo1</url-pattern> </servlet-mapping>
</web-app> |
全局属性设置代码示例:
package com.rl.servlet;
import java.io.IOException;
import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
public class ServletDemo1 extends HttpServlet {
@Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获得Servlet容器对象 ServletContext sc = getServletContext(); //获得全局属性 Integer pvcount = (Integer)sc.getAttribute("pvcount"); if(pvcount == null){ sc.setAttribute("pvcount", 1); pvcount = (Integer)sc.getAttribute("pvcount"); }else{ sc.setAttribute("pvcount", ++pvcount); }
response.getOutputStream().write(("<font color='red' size='20'> 当前站点被点击了" + pvcount +"次</font>").getBytes()); }
} |
通过ServletContext获得项目根目录下的文件:
package com.rl.servlet;
import java.io.IOException; import java.io.InputStream; import java.util.Properties;
import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet;
public class ServletDemo3 extends HttpServlet {
@Override public void init(ServletConfig config) throws ServletException { //获得ServletContext对象 ServletContext sc = config.getServletContext(); //获得工程目录webroot下文件的路径 //D:\tomcat-test\apache-tomcat-7.0.62\webapps\servlet_demo4\test.properties //getRealPath的参数内容不会被校验,只有真正要用这个路径的时候才知道路径对不对 String path = sc.getRealPath("test.properties"); System.out.println(path);
//获得工程目录webroot下文件的输入流对象 //第一个/代表项目的根目录 InputStream in = sc.getResourceAsStream("/WEB-INF/test.properties"); Properties prop = new Properties(); try { prop.load(in); System.out.println(prop.get("key")); } catch (IOException e) { e.printStackTrace(); } }
} |
读取类路径下文件的两种方式:
package com.rl.servlet;
import java.io.IOException; import java.io.InputStream; import java.util.Properties;
import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet;
public class ServletDemo4 extends HttpServlet {
@Override public void init(ServletConfig config) throws ServletException { //获得ServletContext对象 ServletContext sc = config.getServletContext(); //获得classpath下的资源的文件的流 //由于classpath下的文件发布之后是在web项目/WEB-INF/classes下 //所以去指定/WEB-INF/classes/test1.properties //InputStream in = sc.getResourceAsStream("/WEB-INF/classes/test1.properties");
//另一种方法获取类文件 //通过当前类获取类对象 //通过类对象获得类加载器 //通过类加载器获得资源流,资源流指定的根目录是classpath,也就是java项目的src文件夹 //使用类加载器的方式来读取classpath下的资源文件 //好处不依赖于ServletContext,任何类都可以获得classpath下的文件 //不需要再自己指定/WEB-INF/classes InputStream in = this.getClass().getClassLoader().getResourceAsStream("test1.properties");
//读取test1.properties中的值 Properties prop = new Properties(); try { prop.load(in); System.out.println(prop.get("key1")); } catch (IOException e) { e.printStackTrace(); }
} } |