11 java 王少飞-ServletContext应用

Servlet容器在启动时会加载Web应用,并为每个Web应用创建唯一的ServletContext对象。可以把ServletContext看成是一个Web应用的服务器端组件的共享内存。在ServletContext中可以存放共享数据,它提供了4个读取或设置共享数据的方法。

1)setAttribute(String name,Object object):把一个对象和一个属性名绑定,将这个对象存储在ServletContext中;

2)getAttribute(String name):根据指定的属性名返回所绑定的对象;

3)removeAttribute(String name):根据给定的属性名从ServletContext中删除相应的操作;

4)getAttributeNames():返回Enumeration对象,它包含了存储在ServletContext对象中的所有属性名。

ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,可以通过ServletConfig.getServletContext方法获得ServletContext对象, 也可以使用 this.getServletContext方法

由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现通讯。多个Servlet通过ServletContext对象实现数据共享。

ServletContext对象通常也被称之为context域对象。

ServletConfig和ServletContext的区别

–   整个Web应用只有一个ServletContext,在部署Web应用的时候,容器会建立这一个ServletContext对象,这个上下文对Web应用中的每个Servlet和JSP都可用。

–  Web应用中的各个Servlet都有自己的ServletConfig,它只对当前Servlet有效。

下面是ServletContext的几个应用知识点:

1.写出获取ServletContext的两种方式
①ServletConfig.getServletContext方法获得ServletContext对象。
ServletContext context = this.getServletConfig().getServletContext();
②使用 this.getServletContext方法
ServletContext context = this.getServletContext();
2.使用ServletContext实现两个Servlet数据共享
 Context1---------------------
 String data="JavaWeb编程";
 this.getServletContext().setAttribute("data",data);
 
 Context2----------------------  
 String value=(String) this.getServletContext().getAttribute("data1");
 System.out.println(value);

3.设置ServletContext初始化参数,然后对其之。
 在XML文件中设置----------------
  <context-param>
    <param-name>data</param-name>
    <param-value>aaaaa</param-value>
  </context-param>
 
 在Servlet文件中输出--------------
 System.out.println(this.getServletContext().getInitParameter(data));

4.编写一个转发
在Servlet文件中------------
this.getServletContext().setAttribute("username","zhangsan");
RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/index.jsp");
rd.forward(request, response);

index.jsp中-----------------
<body>
    <%=application.getAttribute("username") %>
</body>

5.通过ServletContext读取配置文件的内容。(使用两种方式)

通过ServletContext读取配置文件 方法1------------
       public void test1() throws IOException {
                InputStream in = this.getServletContext().getResourceAsStream(
    "/WEB-INF/classes/db.properties");

  Properties prop = new Properties();
  prop.load(in);
  String driver = prop.getProperty("driver");
  String url = prop.getProperty("url");
  String username = prop.getProperty("username");
  String password = prop.getProperty("password");

  System.out.println(url);
 }

通过ServletContext读取配置文件 方法2,获取了文件的绝对路径-----------
 public void test2() throws IOException {
  String path = this.getServletContext().getRealPath(
    "/WEB-INF/classes/db.properties");
  int i = path.lastIndexOf("\\");
  System.out.println(path.substring(i + 1));

  FileInputStream fis = new FileInputStream(path);

  Properties prop = new Properties();
  prop.load(fis);

  String driver = prop.getProperty("driver");
  System.out.println(driver);
 }


6.通过一般的java类读取配置文件的内容。

StudentDao类读取配置文件的内容-----------------------
public class StudentDao {
 
 private static String driver;
 
 static{
  //如果读取配置文件不是Servlet,而是一般的类,使用类加载器。
  InputStream in = StudentDao.class.getClassLoader().getResourceAsStream("db.properties");
  Properties prop = new Properties();
  try {
   prop.load(in);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
   driver = prop.getProperty("driver");
  String url = prop.getProperty("url");
  String username = prop.getProperty("username");
  String password = prop.getProperty("password");
 }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值