ServletContext_获取以及获取MIME类型
1.获取:
1.通过request对象获取
request.getServletContext();
2.通过HTTPServlet获取
this.getServletContext();
@WebServlet(name = "ServletContextDemo1", value = "/ServletContextDemo1") public class ServletContextDemo1 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override 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); } }
获取MIME类型:
MIME类型:在互联网通信过程中定义的一种文件数据类型
格式:大类型/小类型 text/html image
方法:
获取:String getMimeType(String file)
2.域对象,共享数据
3.获取文件的真实(服务器)路径
@WebServlet(name = "ServletContextDemo2", value = "/ServletContextDemo2") public class ServletContextDemo2 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* 获取MIME类型: MIME类型:在互联网通信过程中定义的一种文件数据类型 格式:大类型/小类型 text/html image 方法: 获取:String getMimeType(String file) 2.域对象,共享数据 3.获取文件的真实(服务器)路径 */ //1.通过HttpServlet对象获取 ServletContext context = this.getServletContext(); //2.定义文件名称 String filename = "a.jpg"; // image/jpeg //3.获取MIME类型 String mimeType = context.getMimeType(filename); System.out.println(mimeType); } }
标签:String,request,getServletContext,获取,MIME,ServletContext From: https://www.cnblogs.com/qihaokuan/p/16967062.html