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