欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ServletContext对象

程序员文章站 2022-10-03 20:59:40
ServletContext概述全局对象,也拥有作用域,对应一个Tomcat中的web应用当web服务器启动时,会为每一个web应用程序创建一块共享的存储区域(ServletContext)ServletContext在web服务器启动时创建,服务器关闭时销毁获取ServletContext对象GenericServlet提供了getServletContext()方法,(推荐使用) this.getServletContext();HttpServletRequest提供了g...

ServletContext概述

  • 全局对象,也拥有作用域,对应一个Tomcat中的web应用
  • 当web服务器启动时,会为每一个web应用程序创建一块共享的存储区域(ServletContext)
  • ServletContext在web服务器启动时创建,服务器关闭时销毁

获取ServletContext对象

  • GenericServlet提供了getServletContext()方法,(推荐使用) this.getServletContext();
  • HttpServletRequest提供了getServletContext()方法(推荐)
  • HttpSession提供了getServletContext()方法 

ServletContextController.java

package com.newer.servletProject.servlet.controller;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "ServletContextController",value = "/ctxController")
public class ServletContextController extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.通过 this.getServletContext()
        ServletContext servletContext=this.getServletContext();
        //2.通过request对象获取
        ServletContext servletContext1=request.getServletContext();
        //3.通过session获取
       /* HttpSession session=request.getSession();
        ServletContext servletContext2=session.getServletContext();*/

        //获取当前项目路径
        System.out.println(servletContext.getRealPath("/"));
        //获取当前项目上下文路径(应用程序名称)
        System.out.println(servletContext.getContextPath());
        //存值
        servletContext.setAttribute("context","info");


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

 ServletContext作用

获取项目真实路径

获取当前项目在服务器发布的真实路径 

String realpath=servletContext.getRealPath("/");

获取项目上下文路径

获取当前项目上下文路径(应用程序名称)

 servletContext.getContextPath();

request.getContextPath();

全局容器

servletContext拥有作用域,可以存储数据到全局容器中

  • 存储数据:servletContext .setAttribute("name",value);
  • 获取数据:servletContext .getAttribute("name);
  • 移除数据:servletContext .removeAttribute("name");

 ServletContextController.java

package com.newer.servletProject.servlet.controller;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "ServletContextController",value = "/ctxController")
public class ServletContextController extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.通过 this.getServletContext()
        ServletContext servletContext=this.getServletContext();
        //2.通过request对象获取
        ServletContext servletContext1=request.getServletContext();
        //3.通过session获取
       /* HttpSession session=request.getSession();
        ServletContext servletContext2=session.getServletContext();*/

        //获取当前项目路径
        System.out.println(servletContext.getRealPath("/"));
        //获取当前项目上下文路径(应用程序名称)
        System.out.println(servletContext.getContextPath());
        //存值
        servletContext.setAttribute("context","info");


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

ShowContextController.java

package com.newer.servletProject.servlet.controller;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "ShowContextController",value = "/showController")
public class ShowContextController extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //通过 this.getServletContext获取servletContext
        ServletContext servletContext=this.getServletContext();
        //取值
       String s= (String)servletContext.getAttribute("context");
       System.out.println(s);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

ServletContext特点

  • 唯一性:一个应用对应一个Servlet上下文
  • 生命周期:只要容器不关闭或者应用不卸载,servlet上下文就一直存在

 ServletContext应用场景

ServletContext统计当前项目访问次数

 CountController.java

package com.newer.counter;
/**
 * 统计项目访问次数
 */

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "CountController",value = "/counterController")
public class CountController extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //获取ervletContext对象
        ServletContext servletContext=request.getServletContext();
        //获取计数器
        Integer counter=(Integer)servletContext.getAttribute("counter");
        //判断是否是第一次访问
        if(counter==null){
            counter=1;
            servletContext.setAttribute("counter",counter);
        }else {
            counter++;
            servletContext.setAttribute("counter",counter);
        }
        System.out.println("counter:"+counter);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

ShowCountController.java

package com.newer.counter;
/**
 * 作用域:
 * HttpServletRequest:一次请求,请求响应之前有效
 * HttpSession:一次会话,浏览器不关闭或者不超时之前有效---登录验证码
 * ServletContext:服务器启动开始,服务器停止之前有效
 */

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "ShowCounterController",value = "/showCounterController")
public class ShowCounterController extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取ervletContext对象
        ServletContext servletContext=request.getServletContext();
        //获取计数器
        Integer counter=(Integer)servletContext.getAttribute("counter");

        //判断是否是第一次访问
        if(counter==null){
            counter=1;
            servletContext.setAttribute("counter",counter);
        }else {
            counter++;
            servletContext.setAttribute("counter",counter);
        }
        System.out.println("show:"+counter);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

作用域总结

  • HttpServletRequest:一次请求,请求响应之前有效(数据传递)
  • HttpSession:一次会话开始,浏览器不关闭或者不超时之前有效(登录权限认证)
  • ServletContext:服务器启动开始,服务器停止之前有效(上升到全局容器,如计数器)

本文地址:https://blog.csdn.net/weixin_44364444/article/details/109555819

相关标签: Servlet