1. 程式人生 > 其它 >JavaWeb15.4【response:ServletContext物件的獲取方式和功能】

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 }