Tomcat中使用servletContext

Tomcat中使用servletContext

  • 运行在JVM上的每一个web应用程序都有一个与之对应的Servlet上下文(Servlet运行环境)
  • Servlet API提供ServletContext接口用来标识Servlet上下文,ServletContext对象可以被web应用程序中的所有Servlet访问
  • ServletContext对象是web服务器中的一个一直路径的根
  • ServletContext用来解决不同用户之间的数据共享

ServletContext的特点

  • 由服务器创建
  • 所有用户共享同一个ServletContext对象
  • 所有的Servlet都可以访问到同一个ServletContext中的属性
  • 每一个web项目对象的是一个ServletContext

ServletContext的使用

  • 获取ServletContext

    • 方式1ServletContext servletContext = this.getServletContext()
    • 方式2ServletContext servletContext1 = this.getServletConfig().getServletContext();
    • 方式3ServletContext servletContext2 = request.getSession().getServletContext();
  • servletcontext设置属性值

    • servletContext.setAttribute("name", "zhangsan");
  • 读取servletcontext的属性值

    • String name = (String)servletContext.getAttribute("name");
  • 获取web,xml中配置的公共属性

    • <context-param>
      	<param-name>chengdu</param-name>
      	<param-value>beautiful</param-value>
      </context-param>
      <!--  在xml配置中添加公共属性  -->
      
    • String chengdu = servletContext.getInitParameter("chengdu");

  • 获取项目下某个文件的绝对路径

    • String realPath = servletContext.getRealPath("web.xml");
  • 获取web项目的上下文路径(虚拟目录路径)

    • String contextPath = servletContext.getContextPath();
public class ServletContextServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置编码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html:charset=utf-8");

        // 获取ServletContext
        // 方式1
        ServletContext servletContext = this.getServletContext();
        // 方式2
        ServletContext servletContext1 = this.getServletConfig().getServletContext();
        // 方式3
        ServletContext servletContext2 = request.getSession().getServletContext();

        // servletcontext设置属性值
        servletContext.setAttribute("name", "zhangsan");

        // 读取servletcontext的属性值
        String name = (String)servletContext.getAttribute("name");

        //  从web和xml中获取参数值
        String chengdu = servletContext.getInitParameter("chengdu");
        System.out.println(chengdu);

        // 获取项目下某个文件的绝对路径
        String realPath = servletContext.getRealPath("web.xml");
        System.out.println(realPath);

        // 获取web项目的上下文路径
        String contextPath = servletContext.getContextPath();
        System.out.println(contextPath);
    }
}
  • 网页访问计数器
public class NumServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置编码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("gbk");

        // 获取servletcontext对象
        ServletContext servletContext = this.getServletContext();
        // 获取属性值
        Integer num = (Integer)servletContext.getAttribute("num");
        if(num==null){
            servletContext.setAttribute("num",1);
        }else{
            // 实现每次访问加1,并将num设置到ServletContext中
            servletContext.setAttribute("num", ++num);
        }

        // 获取输出对象
        PrintWriter out = response.getWriter();
        // 给客户端返回数据
        out.write("<html>");
        out.write("<head>");
        out.write("</head>");
        out.write("<body>");
        out.write("<h1>用户的访问次数为:"+ num +"次</h1>");
        out.write("</body>");
        out.write("</html>");
    }
}
posted @ 2021-03-08 20:30  殃奕  阅读(257)  评论(0编辑  收藏  举报