博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ServletContext对象的使用
阅读量:4464 次
发布时间:2019-06-08

本文共 5092 字,大约阅读时间需要 16 分钟。

得到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   
2
3
AAA
4
AAA's value
5
6
7
BBB
8
BBB's value
9

获取参数的方法

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
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     @Override42     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域对象进行数据共享,只能用转发技术。

 

转载于:https://www.cnblogs.com/binklei/p/6428688.html

你可能感兴趣的文章
从无到满意offer,你需要知道的那些事
查看>>
P1516 青蛙的约会 洛谷
查看>>
SDOI2011 染色
查看>>
JQuery EasyUI combobox动态添加option
查看>>
面向连接的TCP概述
查看>>
前端快捷方式 [记录]
查看>>
亲测可用,解决端口被占用的指令!!
查看>>
MySQL--视图、触发器、事务、存储过程、内置函数、流程控制、索引
查看>>
Django--数据库查询操作
查看>>
自定义配置文件的使用
查看>>
js-20170609-运算符
查看>>
算法笔记_065:分治法求逆序对(Java)
查看>>
MSP430FLASH小结
查看>>
STM32 ADC转换时间
查看>>
结合实际业务场景聊一聊MVP模式的应用
查看>>
WinPE启动U盘的制作方法与软件下载(通用PE工具箱/老毛桃/大白菜WinPE)(转载)...
查看>>
行为型设计模式之5--中介者模式
查看>>
Android DevArt6:Android中IPC的六种方式
查看>>
PMP学习感想
查看>>
Zookeeper全解析——Paxos作为灵魂
查看>>