ServletContext对象:
ServletContext代表是一个web应用的环境(上下文)对象,ServletContext对象 内部封装是该web应用的信息,ServletContext对象一个web应用只有一个。
一个web应用有多个Servlet对象。
ServletContext对象的生命周期?
创建:该web应用被加载(服务器启动或发布web应用(前提,服务器启动状 态))
销毁:web应用被卸载(服务器关闭,移除该web应用)
获得Servlet对象,获得Servlet应用中任何资源的绝对路径。(放资源一般放在WebContent上,不会直接放在WEB项目上)
public class Servlet01 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取ServletContext对象 ServletContext context=getServletContext(); //获取资源的相对路径 String patha=context.getRealPath("WEB-INF/classes/a.txt"); System.out.println(patha); String pathb=context.getRealPath("b.txt"); System.out.println(pathb); String pathc=context.getRealPath("WEB-INF/c.txt"); System.out.println(pathc); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
ServletContext是一个域对象(存储数据的区域就是域对象),
ServletContext域对象的作用范围:整个web应用(所有的web资源都可以随意向 servletcontext域中存取数据,数据可以共享)
域对象的通用的方法:
setAtrribute(String name,Object obj);
getAttribute(String name);
removeAttribute(String name);
public class MyServlet01 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取ServletContext对象 ServletContext context=getServletContext(); //向域中存值 context.setAttribute("name","张三"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
public class MyServlet02 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取ServletContext对象 ServletContext context=getServletContext(); //取值 String name=(String)context.getAttribute("name"); System.out.println(name); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
设置计数器功能:
public class LoginServlet extends HttpServlet { private UserService userService=new UserService(); @Override public void init() throws ServletException { //初始化计数器 int count=0; //将计数器放在ServletContext域中 //存值 ServletContext context=getServletContext(); context.setAttribute("count",count); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取用户名密码 String uname=request.getParameter("username"); String pwd=request.getParameter("password"); int count=userService.login(uname, pwd); if(count>0){ //响应客户端 //登录成功 //获取ServletContext对象 ServletContext context=getServletContext(); //取值 int sum=(int)context.getAttribute("count"); sum++; //将计数器放回去 context.setAttribute("count",sum); response.getWriter().write("yuu are the"+sum+" person"); }else{ response.getWriter().write("false"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }