ServletContext域对象
* ServletContext域对象(接口)
* 定义:WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用。
一个WEB应用对应一个ServletContext对象
一个WEB应用下有多个Servlet程序
一个web应用所有的servlet程序都共享同一个ServletContext对象
demo1存入内容—》ServletContext—》demo2中取出来:ServletContext相当于一个媒介,demo1向其中存入数据,demo2可以冲其中取出数据。
* 获取全局初始化参数
import java.io.IOException; import java.util.Enumeration; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 获取全局初始化参数 * @author Administrator * */ public class ContextDemo1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 先获取ServletContext对象 ServletContext context = getServletContext(); // 获取web.xml全局初始化参数 String encoding = context.getInitParameter("encoding"); System.out.println("编码:"+encoding); //获取所有的参数名 Enumeration<String> e = context.getInitParameterNames(); while(e.hasMoreElements()){ String name = e.nextElement(); // 通过name获取值 String value = context.getInitParameter(name); System.out.println(name+" : "+value); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
* 统计和显示网站的访问次数
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; /** * 统计网站的访问次数 * @author Administrator * */ public class CountServlet extends HttpServlet { /** * 实例被创建,调用init方法进行初始化 * 在域对象存入一个变量,赋值为0 */ public void init() throws ServletException { // 获取ServletContext对象 getServletContext().setAttribute("count", 0); } /** * 每一次访问,都会执行该方法。 * 拿出count的变量,值自增,存入到域对象中 */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 先获取ServletContext对象 ServletContext context = getServletContext(); // 获取count的值,自增 Integer count = (Integer) context.getAttribute("count"); // 存入到域对象中 context.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); } }
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 显示网站的访问次数 * @author Administrator * */ public class ShowServlet 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); } }
* 读取资源文件
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 读取资源文件 * @author Administrator * */ public class ReadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { read2(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } /** * 通过ServletContext对象获取文件的绝对磁盘路径 * 获取src目录下文件 * @throws IOException */ public void read5() throws IOException{ // 获取对象 String path = getServletContext().getRealPath("/WEB-INF/classes/db.properties"); // System.out.println(path); // C:\apache-tomcat-6.0.14\webapps\day09\WEB-INF\classes\db.properties // 获取输入流 InputStream in = new FileInputStream(path); print(in); } /** * 获取WebRoot目录目录下db.properties文件 * @throws IOException */ public void read4() throws IOException{ // ServletContext读取文件 InputStream in = getServletContext().getResourceAsStream("/db.properties"); // 打印方式 print(in); } /** * 获取包目录下db.properties文件 * @throws IOException */ public void read3() throws IOException{ // ServletContext读取文件 InputStream in = getServletContext().getResourceAsStream("/WEB-INF/classes/cn/itcast/context/db.properties"); // 打印方式 print(in); } /** * 获取src目录下db.properties文件 * @throws IOException 编译后,src文件不见了,全都放在class文件里 */ public void read2() throws IOException{ // ServletContext读取文件 InputStream in = getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties"); // 打印方式 print(in); } /** * 传统方式读取资源文件 * 交给服务器处理,相对的位置tomcat/bin * @throws IOException */ public void read1() throws IOException{ // 获取输入流 InputStream in = new FileInputStream("src/db.properties"); print(in); } /** * 在控制台打印内容 * @param in * @throws IOException */ public void print(InputStream in) throws IOException{ Properties pro = new Properties(); // 加载 pro.load(in); // 获取文件中的内容 String username = pro.getProperty("username"); String password = pro.getProperty("password"); String desc = pro.getProperty("desc"); System.out.println("用户名:"+username); System.out.println("密码:"+password); System.out.println("描述:"+desc); } }
配置文件db.properties
username=root33
password=12333
desc=haha33