1.概念
代表整个web应用,可以和程序的容器(服务器)来通信
2.获取
1.通过request对象获取 req.getServletContext();
2.通过HttpServlet获取,this.getServletContext();
package cn.red.web.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 {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext sc = req.getServletContext();
ServletContext sc1 = this.getServletContext();
System.out.println(sc);
System.out.println(sc1);
System.out.println(sc==sc1);
}
}
org.apache.catalina.core.ApplicationContextFacade@560e82c
org.apache.catalina.core.ApplicationContextFacade@560e82c
true
3.功能
1.获取MIME类型
*MIME类型:在互联网通信过程中定义的一种文件数据类型(在tomcat的conf/web.xml中对应)
*格式: 大类型/小类型 text/html image/jpeg
*获取:String getMimeType(String file)
package cn.red.web.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 {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext sc = req.getServletContext();
//定义一个文件名称
String filename = "a.jpg";
String mimeType = sc.getMimeType(filename);
System.out.println(mimeType);
}
}
image/jpeg
2.域对象(对象范围:所有用户所有请求的数据)
2.1. setAttribute(String name,Object value)
2.2. getAttribute(String name)
2.3. removeAttribute(String name)
package cn.red.web.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("/servletContextDemoA")
public class ServletContextDemoA extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext sc = req.getServletContext();
sc.setAttribute("msg","this is a message from servletContext");
}
}
package cn.red.web.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("/servletContextDemoB")
public class ServletContextDemoB extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext sc = req.getServletContext();
System.out.println(sc.getAttribute("msg"));
}
}
3.获取文件的真实(服务器)路径
加载文件进服务器内存,时候需要用到
用ClassLoader也能访问到src下的文件,局限性比较大
package cn.red.web.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("/servletContextDemo5")
public class ServletContextDemo5 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext sc = req.getServletContext();
String realPath = sc.getRealPath("/WEB-INF/classes/a.txt");//src下的资源访问
//D:\workspace\Servlet_training\out\artifacts\Serlvet_Response_war_exploded\a.txt
System.out.println(realPath);
String realPath1 = sc.getRealPath("/b.txt");//web目录下的资源访问
System.out.println(realPath1);
String realPath2 = sc.getRealPath("/WEB-INF/c.txt");//WEB-INFO下的资源访问
System.out.println(realPath2);
/**
* 输出结果:
* D:\workspace\Servlet_training\out\artifacts\Serlvet_Response_war_exploded\WEB-INF\classes\a.txt
* D:\workspace\Servlet_training\out\artifacts\Serlvet_Response_war_exploded\b.txt
* D:\workspace\Servlet_training\out\artifacts\Serlvet_Response_war_exploded\WEB-INF\c.txt
*/
}
}