ServletContext對象的使用


得到web應用路徑

getContextPath();用於請求重定向的資源名稱中

 1 public class ContextDemo extends HttpServlet {  2  @Override  3 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {  4 //1.得到ServletContext對象  5 //ServletContext context = this.getServletConfig().getServletContext();  6 ServletContext context = this.getServletContext();//推薦使用  7  8 //2.得到web應用路徑/testweb  9 /** 10  * web應用路徑:部署到tomcat服務器上運行的web應用名稱 11 */ 12 String path = context.getContextPath();//"/testweb項目名稱" 13  System.out.println(path); 14 15 /** 16  * 案例:應用到請求重定向 17 */ 18 //resp.sendRedirect("/webtest/index.html"); 19 resp.sendRedirect(context.getContextPath()+"/index.html"); 20  } 21 }

得到web應用的初始化參數(全局):

web應用參數可以讓當前web應用的所有servlet獲取。

在web.xml文件中,<web-app>中進行配置

1   <!-- 配置web應用參數 --> 2 <context-param> 3 <param-name>AAA</param-name> 4 <param-value>AAA's value</param-value> 5 </context-param> 6 <context-param> 7 <param-name>BBB</param-name> 8 <param-value>BBB's value</param-value> 9 </context-param>

獲取參數的方法

 1 public class ContextDemo2 extends HttpServlet{
 2     /**
 3      * 得到web應用的參數
 4      */
 5     @Override
 6     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 7         //得到ServletContext對象
 8         ServletContext context = this.getServletContext();
 9         
10         String paramValue = context.getInitParameter("AAA");
11         System.out.println(paramValue);
12         
13         Enumeration<String> enums = context.getInitParameterNames();
14         while(enums.hasMoreElements()){
15             String paraName = enums.nextElement();
16             String paraValue = context.getInitParameter(paraName);
17             System.out.println(paraName+"="+paraValue);
18         }
19     }
20 }

 域對象相關的方法:

域對象:作用是用於保存數據和獲取數據,可以在不同的動態資源之間共享數據。

案例:

  servlet1                        servlet2

  name=eric

  response.sendRedirect("/Servlet2?name=eric");  request.getParameter("name");

  保存到域對象中                    從域對象中取出

  Student

  

方案1:可以通過傳遞參數的形式共享數據,局限:只能傳遞字符串。

方案2:可以使用域對象共享數據,好處:可以共享任何類型的數據。

 

ServletContext就是一個域對象。

保存數據:setAttribute();

 1 /**
 2  * 保存數據
 3  * @author MaoDoer
 4  *
 5  */
 6 public class ContextDemo3 extends HttpServlet{
 7     @Override
 8     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 9         //1.得到域對象
10         ServletContext context = this.getServletContext();
11         //2.把數據保存在域對象中
12         //context.setAttribute("name", "bink");
13         context.setAttribute("student", new Student("bink",25));
14         System.out.println("保存成功");
15     }
16 }
17 class Student{
18     private String name;
19     private int age;
20     public String getName() {
21         return name;
22     }
23     public void setName(String name) {
24         this.name = name;
25     }
26     public int getAge() {
27         return age;
28     }
29     public void setAge(int age) {
30         this.age = age;
31     }
32     public Student() {
33         super();
34         // TODO 自動生成的構造函數存根
35     }
36     public Student(String name, int age) {
37         super();
38         this.name = name;
39         this.age = age;
40     }
41     @Override
42     public String toString() {
43         return "Student [name=" + name + ", age=" + age + "]";
44     }
45 }

 

取出數據:getAttribute(String name);

 1 /**
 2  * 取出數據
 3  * @author MaoDoer
 4  *
 5  */
 6 public class ContextDemo4 extends HttpServlet {
 7     @Override
 8     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 9         //1.得到域對象
10         ServletContext context = this.getServletContext();
11         //2.從域對象中取出數據
12         //String name = (String)context.getAttribute("name");
13         Student stu=(Student)context.getAttribute("student");
14         //System.out.println("name="+name);
15         System.out.println(stu);
16     }
17 }

 

刪除數據:removeAttribute(String name);

所有域對象:

HttpServletRequest:域對象

ServletContext:域對象

HttpSession:域對象

PageContext:域對象

 

轉發:

 1 public class ForwardDemo1 extends HttpServlet{
 2     @Override
 3     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 4         /**
 5          * 保存數據到request域對象
 6          */
 7         req.setAttribute("name", "rose");
 8         //轉發
 9         RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/GetDataServlet");
10         rd.forward(req, resp);
11     }
12 }

 

重定向:

 1 public class RedirectDemo1 extends HttpServlet{
 2     @Override
 3     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 4         /**
 5          * 保存數據到request域對象
 6          */
 7         req.setAttribute("name", "rose");
 8         
 9         //重定向
10         resp.sendRedirect(this.getServletContext().getContextPath()+"/GetDataServlet");
11     }
12 }

1)轉發

  a)地址欄不會改變

  b)轉發只能轉發當前web應用內的資源

  c)轉發的路徑可以寫成“/類名”

  d)可以在轉發過程中,可以把數據保存到request域對象中

2)重定向

  a)地址欄會發生改變,變成重新定向地址

  b)可以訪問web應用以外的資源,可以是其他web應用或者外部域名網站

  c)重定向的路徑需要寫項目名“/項目名/類名”

  d)不能在重定向的過程中,把數據保存在request對象中。

注意:如果要使用request域對象進行數據共享,只能用轉發技術。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM