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 ,完成。
第一個:就是通過直接設置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 ,完成。