JavaWeb15.4【response:ServletContext对象的获取方式和功能】
1 package com.haifei.servletcontext; 2 3 import javax.servlet.ServletContext; 4 import javax.servlet.ServletException; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 import java.io.IOException; 10 11 /** 12 * ServletContext对象的获取方式 13 * 1. 通过request对象获取 14 * request.getServletContext(); 15 * 2. 通过HttpServlet获取 16 * this.getServletContext(); 17 */ 18 @WebServlet("/servletContextDemo1") 19 public class ServletContextDemo1 extends HttpServlet { 20 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 21 ServletContext context1 = request.getServletContext(); 22 ServletContext context2 = this.getServletContext(); 23 System.out.println(context1); //org.apache.catalina.core.ApplicationContextFacade@501082b2 24 System.out.println(context2); //org.apache.catalina.core.ApplicationContextFacade@501082b2 25 System.out.println(context1 == context2); //true 26 } 27 28 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 29 this.doPost(request, response); 30 } 31 }
1 package com.haifei.servletcontext; 2 3 import javax.servlet.ServletContext; 4 import javax.servlet.ServletException; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 import java.io.IOException; 10 11 /** 12 * ServletContext对象的功能1:获取MIME类型 13 * * MIME类型:在互联网通信过程中定义的一种文件数据类型 14 * * 格式: 大类型/小类型 text/html image/jpeg 15 * * 查看:D:\Program Files\apache-tomcat-8.5.31\conf\web.xml 16 * * 获取:String getMimeType(String file) 17 */ 18 @WebServlet("/servletContextDemo2") 19 public class ServletContextDemo2 extends HttpServlet { 20 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 21 ServletContext context = this.getServletContext(); 22 23 //定义文件名称 24 String fileName = "a.jpg"; 25 //获取文件的MIME类型 26 String mimeType = context.getMimeType(fileName); 27 System.out.println(mimeType); //image/jpeg 28 29 } 30 31 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 32 this.doPost(request, response); 33 } 34 }
1 package com.haifei.servletcontext; 2 3 import javax.servlet.ServletContext; 4 import javax.servlet.ServletException; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 import java.io.IOException; 10 11 /** 12 * ServletContext对象的功能2:域对象-共享数据 13 * 14 * 1. setAttribute(String name,Object value) 15 * 2. getAttribute(String name) 16 * 3. removeAttribute(String name) 17 * 18 * * ServletContext对象范围:所有用户所有请求的数据,即最大范围 19 */ 20 @WebServlet("/servletContextDemo3") 21 public class ServletContextDemo3 extends HttpServlet { 22 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 23 ServletContext context = this.getServletContext(); 24 25 context.setAttribute("msg", "哈哈"); 26 } 27 28 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 29 this.doPost(request, response); 30 } 31 }
1 package com.haifei.servletcontext; 2 3 import javax.servlet.ServletContext; 4 import javax.servlet.ServletException; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 import java.io.IOException; 10 11 /** 12 * ServletContext对象的功能2:域对象-共享数据 13 * 14 * 1. setAttribute(String name,Object value) 15 * 2. getAttribute(String name) 16 * 3. removeAttribute(String name) 17 * 18 * * ServletContext对象范围:所有用户所有请求的数据 19 */ 20 @WebServlet("/servletContextDemo4") 21 public class ServletContextDemo4 extends HttpServlet { 22 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 23 ServletContext context = this.getServletContext(); 24 25 Object msg = context.getAttribute("msg"); 26 System.out.println(msg); 27 /* 28 http://localhost:8080/day15/servletContextDemo3 29 http://localhost:8080/day15/servletContextDemo4 30 31 哈哈 32 */ 33 } 34 35 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 36 this.doPost(request, response); 37 } 38 }
1 package com.haifei.servletcontext; 2 3 import javax.servlet.ServletContext; 4 import javax.servlet.ServletException; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 import java.io.File; 10 import java.io.IOException; 11 12 /** 13 * ServletContext对象的功能3:获取文件的真实(服务器[tomcat])路径 14 * 15 * 方法:String getRealPath(String path) 16 * 17 */ 18 @WebServlet("/servletContextDemo5") 19 public class ServletContextDemo5 extends HttpServlet { 20 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 21 ServletContext context = this.getServletContext(); 22 23 String realPath = context.getRealPath("/b.txt"); //web目录下资源的访问 24 System.out.println(realPath); 25 // D:\Workspace-java\idea\Workspaces\itheima-javaweb\out\artifacts\day15_war_exploded\b.txt 26 // "/b.txt"中的/代表D:\Workspace-java\idea\Workspaces\itheima-javaweb\out\artifacts\day15_war_exploded\,即web目录 27 File file = new File(realPath); 28 29 String realPath1 = context.getRealPath("/WEB-INF/c.txt"); //WEB-INF目录下资源的访问 30 System.out.println(realPath1); 31 //D:\Workspace-java\idea\Workspaces\itheima-javaweb\out\artifacts\day15_war_exploded\WEB-INF\c.txt 32 33 String realPath2 = context.getRealPath("/WEB-INF/classes/a.txt"); //src目录下资源的访问 34 System.out.println(realPath2); 35 //D:\Workspace-java\idea\Workspaces\itheima-javaweb\out\artifacts\day15_war_exploded\WEB-INF\classes\a.txt 36 } 37 38 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 39 this.doPost(request, response); 40 } 41 }