ServletContext对象 (域对象)
定义:WEB容器在启动时,它会为每个WEB运用程序都创建一个对应的ServletContext对象,它代表当前WEB运用。
一个WEB运用对应一个ServletContext对象
一个WEB运用下有多个Servlet程序
所有的Servlet程序都共享同一个ServletContext对象
作用
1、获取WEB运用的全局初始化参数 案例和ServletConfig类似
String getInitParameter(String name)
Enumeration getInitParameters()
配置全局初始化参数 在<Servlet>外面
<Context-param>
<param-name>assasasa</param-name>
<param-value>fdsfdsfsd<param-value>
</Context-param>
2、实现数据共享
void setAttribute(String name,Object object) 存入数据
void removeAttribute(String name) 删除数据
- object getAttribute(String name) 获取数据
统计网站访问量案例 用2 方法
package cn.idcast.Servlet;
import java.io.IOException;
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 ServletCount extends HttpServlet {
/**
* 实例被创建,调用init进行初始化 在域对象存入一个变量,初始化为0
*/
public void init() throws ServletException {
getServletContext().setAttribute("count", 0);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/**
* 每一次访问,都会执行该方法 获得count的变量,每次访问count都会自增,再存入到域对象中
*/
ServletContext sc = getServletContext();
// 因为count 是数字,所以用integer
Integer count = (Integer) sc.getAttribute("count");
// ++i是先处理完加法,再做其它运算, i++是处理完相关运算(执行完一条语句后)后自加
sc.setAttribute("count", ++count);
// 向页面输出内容
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write("<h3>下次再来</h3>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
package cn.idcast.Servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletShow extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Integer count = (Integer) getServletContext().getAttribute("count");
// 向页面输出内容
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write("<h3>一共访问了" + count + "次</h3>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
3、读取资源文件
InputStream getResourceAsStream(String path) (服务器端的绝对路径,没有项目名称) 通过文件地址获取输入流
String getRealPath(String path) (服务器端的绝对路径,没有项目名称) 通过文件地址获取文件的绝对磁盘路径
properties配置文件
username=root
password=134
desc=haha
package cn.idcast.Servlet;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
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 ReadServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* 获取到资源文件,因为用了相对路径,所以前面的项目名可以不用写, 只需要写后面的路径即可
*/
InputStream is = getServletContext().getResourceAsStream(
"WEB-INF/classes/db.properties");
// 实例化配置文件
Properties pro = new Properties();
pro.load(is);
String name = pro.getProperty("username");
String pw = pro.getProperty("password");
String desc = pro.getProperty("desc");
System.out.println("用户名:" + name);
System.out.println("密码:" + pw);
System.out.println("描述:" + desc);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}