数据加载中……
javaweb 中对于不同的路径,获得某一个属性的值的方法。
今天学习了三个内容,
第一个:就是通过直接设置web。xml来获得属性的值
第二个:两个servlet之间通过servletcontext来实现值的发送和获得。
第三个:是通过一个监听。直接调用servlet获得某个属性的值。

第一个       就是通过直接设置web。xml来获得属性的值

   详细步骤:
1.新建一个servlet,例如hello。然后打开web。xml对servlet的名字为hello的增加一定的语句。
 <init-param>
    <param-name>city</param-value>
  <param-value>shengyang</param-value>
</init-param>--相当于一个标签,有属性有名字
  2.然后在hello的doget方法里,添加如下语句。String get=getInitparameter('"city");
           pw.printlun(get);
3.运行网页,小地球,输入http://localhost:8080/test/servlet/hello即出现city的属性值,shengyang。
第二个      两个servlet之间通过servletcontext来实现值的发送和获得。两个servlet之间通过servletcontext来实现值的发送和获得。
详细步骤:
1. 新建servlet B。更改他的doget方法添加如下代码
   Servevcontext t=this.getServcontext()--获得两个servlet共有的内容区
    t。setAttribute("名字ok","值good");
2.同样新建C。更改他的doget方法
    Servevcontext t=this.getServcontext()--获得两个servlet共有的内容区
  String s=(String)  t.getAttribute("名字ok");--加上强制转换
    然后可以定一个输出。即可显示ok里的值good。完成
第三个是通过一个监听。直接调用servlet获得某个属性的值。
详细步骤:
    1.新建一个类实现ServletContextListener
 并实现这里的两个方法,主要是public void contextInitialized(ServletContextEvent arg0) {
  ServletContext sc=arg0.getServletContext();
  sc.setAttribute("ok", "good");
  }
2.在web.xml 里添加
<listener>
  <listener-class>List</listener-class>
  </listener>
3.然后运行,既可以直接显示ok里的值good ,完成。

posted on 2010-09-08 16:52 晓玲 阅读(170) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: