ServletContext和ServletConfig使用说明

        通过前面知识点的学习,我们对于请求的处理已经可以说比较灵活了,但是还不够。我们再介绍两个重要的对象 ServletContext 对象和 ServletConfig 对象

ServletContext 对象:

问题:Request 解决了一次请求内的数据共享问题,session 解决了用户不同请求的数据共享问题,那么不同的用户的数据共享该怎么办呢?

解决:使用 ServletContext 对象

作用:解决了不同用户的数据共享问题原理:ServletContext 对象由服务器进行创建,一个项目只有一个对 象。不管在项目的任意位置进行获取得到的都是同一个对象,那么不同用户发起的请求获取到的也就是同一个对象了,该对象由用户共同拥有。

特点:服务器进行创建用户共享一个项目只有一个

生命周期:服务器启动到服务器关闭

作用域:项目内

使用:

1.获取 ServletContext 对象

2.使用作用域进行共享数据流转

3.获取 web.xml 中的全局配置

4.获取 webroot 下项目资源流对象

5.获取 webroot 下资源绝对路径

TestServlectContextA:

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * ServletContext对象:
 * 	作用:
 * 		解决了不同用户之间的数据共享问题
 * 	使用:
 * 		创建ServletContext对象
 * 				ServletContext sc1 = this.getServletContext();
				ServletContext sc2 =this.getServletConfig().getServletContext();
				ServletContext sc3=req.getSession().getServletContext();
		存储用户共享数据
			sc.setAttribute(String name,Object value);
		获取用户共享数据
			sc.getAttribute(String name);
		删除共享数据
			sc.removeAttribute(String name);
		获取web.xml中的全局配置属性,作用:将部分动作和源文件进行解耦,我们只需要在xml配置文件中进行相关配置就会改变代码的执行效果。
			sc.getInitParameter(String name);注意返回的是String类型数据
		获取webRoot下的资源流对象
			sc.getResourceAsStream(String path);
			注意:
				path为相对路径,写从webRoot开始查找资源的路径即可。
		获取webRoot下的资源绝对路径
			sc.getRealPath(String path);
			注意:
				path为相对路径,写从webRoot开始查找资源的路径即可。
 * 	特点:
 * 		服务器创建
 * 		所有用户共享
 * 	生命周期:
 * 		服务器开启到服务器关闭
 * @author MyPC
 */
public class TestServlectContextA extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//设置请求编码格式
		req.setCharacterEncoding("utf-8");
		//设置响应编码格式
		resp.setContentType("text/html;charset=utf-8");
		//获取请求信息
		//处理请求信息
			String str="我是用户共享数据";
			//获取ServletContext对象
				ServletContext sc1 = this.getServletContext();
				ServletContext sc2 =this.getServletConfig().getServletContext();
				ServletContext sc3=req.getSession().getServletContext();
				System.out.println(sc1==sc2);
				System.out.println(sc2==sc3);
			//存储用户共享数据到ServletContext中
				sc1.setAttribute("str", str);
			//获取web.xml中的全局配置属性
				String f=sc1.getInitParameter("flag");
				if("false".equals(f)){
					System.out.println("TestServlectContextA.service(关闭***资源)");
				}else{
					System.out.println("TestServlectContextA.service(打开***资源)");
				}
			//获取webRoot下资源的流对象
				//File f2=new File("D:\\apache-tomcat-7.0.56\\webapps\\sc\\image");
				InputStream resourceAsStream = sc1.getResourceAsStream("image/s.png");
			//获取WebRoot下资源的绝对路径
				String path=sc1.getRealPath("image/s.png");
				System.out.println(path);
		//响应处理结果
			//直接响应
				resp.getWriter().write("数据已经在ServletContext中存储完毕");
			//请求转发
			//重定向
	}
}

TestServletContextB:

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 TestServletContextB extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//设置请求编码格式
		req.setCharacterEncoding("utf-8");
		//设置响应编码格式
		resp.setContentType("text/html;charset=utf-8");
		//获取请求信息
			//获取ServletContext对象
			ServletContext sc = this.getServletContext();
			//获取共享数据
			String s=(String) sc.getAttribute("str");
		//处理请求信息
			System.out.println("TestServletContextB.service()"+s);
		//响应处理结果
			//直接响应
			//请求转发
			//重定向
	}
}

 

案例:网页浏览器次数统计

ServletConfig 对象

问题:使用 ServletContext 对象可以获取 web.xml 中的全局配置文件,在 web.xml 中每个 Servlet 也可以进行单独的配置,那么该怎么获取配置信息呢?

解决:使用 ServletConfig对象

作用:ServletConfig 对象是 Servlet 的专属配置对象,每个 Servlet 都单独拥有一个 ServletConfig 对象,用来获取 web.xml 中的配置信息。

使用:

1.获取 ServletConfig 对象

2.获取 web.xml servlet 的配置信息

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServletConfig extends HttpServlet {
	@Override	
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//获取ServletConfig对象
			ServletConfig sg = this.getServletConfig();
		//设置请求编码格式
			req.setCharacterEncoding(sg.getInitParameter("code"));
		//设置响应编码格式
			resp.setContentType("text/html;charset=utf-8");
		//获取请求信息
			//获取web.xml中的属性配置
			String flag = sg.getInitParameter("flag");
			System.out.println(flag);
			//获取用户请求信息
			String uname=req.getParameter("uname");
		//处理请求信息
			System.out.println(uname);
		//响应处理结果
	}
}
posted @ 2020-02-24 11:08  赵广陆  阅读(22)  评论(0编辑  收藏  举报