首页 > 其他分享 >729~730 ServletContext_概述,获取

729~730 ServletContext_概述,获取

时间:2023-02-10 15:22:19浏览次数:51  
标签:获取 request getServletContext 730 ServletContext import javax 729

ServletContext对象

  1.概念:代表整个web应用,可以和程序的容器(服务器)来通信

  2.获取:

    1. 通过request对象获取
      request.getServletContext();
    2. 通过HttpServlet获取
      this.getServletContext();

  3.功能:

    1.获取MIME类型:

    2.域对象:共享数据

    3.获取文件的真实(服务器)路径

package com.example.ServletContxt;

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("/servletContextDemo1")
public class ServletContextDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*

            ServletContext对象获取:
                1. 通过request对象获取
                    request.getServletContext();
                2. 通过HttpServlet获取
                    this.getServletContext();
         */
        
        //1. 通过request对象获取
        ServletContext context1 = request.getServletContext();
        //2. 通过HttpServlet获取
        ServletContext context2 = this.getServletContext();

        System.out.println(context1);
        System.out.println(context2);

        System.out.println(context1 == context2);//true


    }

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

 

标签:获取,request,getServletContext,730,ServletContext,import,javax,729
From: https://www.cnblogs.com/agzq/p/17109041.html

相关文章