程序员社区

【Servlet-0】ServletContext对象

文章目录

    • ServletContext对象
      • 1.ServletContext对象的获取
      • 2.获取MIME类型
      • 3.ServletContext为域对象可以共享数据
      • 4.获取文件的真实(服务器)路径

ServletContext对象

1.概念:代表整个Web应用,可以和程序的容器(服务器)来通信
2.ServletContext对象的获取
 通过request对象获取:
  request.getServletContext();
 通过HTTPServlet获取:
  this.getServletContext();
3.ServletContext对象的功能:
 1.获取MIME类型
  MIME类型:在互联网通信过程中定义的一种文件数据类型
   格式:大类型/小类型 text/html image/jpeg
  获取:String getMimeType(String file)
 2.域对象:共享数据
  1.setAttribute(String name,Object value)
  2.getAttribute(String name)
  3.removeAttribute(String name)
  ServletContext对象的范围:所有用户所有请求的数据
 3.获取文件的真实(服务器)路径
  方法:String getRealPath(String path)

1.ServletContext对象的获取

package 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("/ServletContextDemo")
public class ServletContextDemo extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //ServletContext对象的获取
      	ServletContext context1 =  request.getServletContext();
        System.out.println(context1);
      	ServletContext context2 = this.getServletContext();
        System.out.println(context2);
        System.out.println(context1==context2);//true
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

2.获取MIME类型

package 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("/ServletContextDemo1")
public class ServletContextDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取ServletContext对象
        ServletContext context = this.getServletContext();
        //定义一个文件名称
        String filename = "a.jpg";
        //获取MIME类型
        String mimeType = context.getMimeType(filename);
        System.out.println(filename);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

3.ServletContext为域对象可以共享数据

 package 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("/ServletContextDemo2")
public class ServletContextDemo2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取ServletContext对象
        ServletContext context = this.getServletContext();
        //设置数据
        context.setAttribute("msg","haha");
        /*
        先运行ServletDemo2,再运行ServletDemo3,发现3可以访问2中的数据
         */
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
package 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("/ServletContextDemo3")
public class ServletContextDemo3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取ServletContext对象
        ServletContext context = this.getServletContext();
        //获取数据
        Object msg =context.getAttribute("msg");
        System.out.println(msg);
    }

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

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

package 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.File;
import java.io.IOException;

@WebServlet("/ServletContextDemo4")
public class ServletContextDemo4 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取ServletContext对象
        ServletContext context = this.getServletContext();

        //获取文件的服务器路径
        String realPath = context.getRealPath("/b.txt");//web目录下资源的访问
        //F:\javaWeb\13-Tomcat\out\artifacts\18_Http_Response_war_exploded\b.txt
        System.out.println(realPath);

        String realPath1 = context.getRealPath("/WEB-INF/c.txt");//WEB-INF目录下的资源访问
        //F:\javaWeb\13-Tomcat\out\artifacts\18_Http_Response_war_exploded\WEB-INF\c.txt
        System.out.println(realPath1);

        String realPath2 = context.getRealPath("/WEB-INF/classes/a.txt");//src目录下的资源
        //F:\javaWeb\13-Tomcat\out\artifacts\18_Http_Response_war_exploded\WEB-INF\classes\a.txt
        System.out.println(realPath2);
        
		/**
		src目录下的文件都放在WEB-INF下的classes下面
		如果想获取src下面的web文件可以通过这种方式
		*/
    }

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

赞(0) 打赏
未经允许不得转载:IDEA激活码 » 【Servlet-0】ServletContext对象

相关推荐

  • 暂无文章

一个分享Java & Python知识的社区