jsp調(diào)用javeabean命令usebean中有scope設(shè)置,一般有application session page等設(shè)置,page就是每頁重新產(chǎn)生usebean中的javabean新對(duì)象,一般情況是用這種,如果多個(gè)jsp程序間為共享數(shù)據(jù),可以使用session
而application的意思,該javabean將一直存在,與session相對(duì)用戶來說,application是相對(duì)應(yīng)用程序的,一般來說,一個(gè)用戶有一個(gè)session,并且隨著用戶離開而消失;而application則是一直存在,類似一個(gè)servlet程序,類似整個(gè)系統(tǒng)的"全局變量",而且只有一個(gè)實(shí)例。
mvc中控制功能
因此application這個(gè)特性,很適合用來做mvc中的控制功能,一般傳統(tǒng)mvc是用servlet做控制功能,v基本是jsp頁面,m就是中間件javabean之類。
但是隨著jsp功能的完善和推廣,逐漸有替代servlet之趨勢,我們?cè)趯?shí)踐中更多使用的也是jsp,有時(shí)為了省卻麻煩的事情,就使用jsp代替servlet.尤其是其控制功能。
實(shí)際上,這個(gè)控制功能是封裝在一個(gè)javabean中,jsp使用scope=application來調(diào)用這個(gè)javabean,這樣,具備控制功能的javabean就類似servlet常駐內(nèi)存,并和后臺(tái)各種中間件交互操作。
“首頁”的展現(xiàn)
在實(shí)際應(yīng)用中,我們經(jīng)常有多個(gè)用戶要同時(shí)訪問一個(gè)頁面,如首頁,這個(gè)首頁中有很多功能要運(yùn)行,比如目錄分類,首頁程序要從數(shù)據(jù)庫中讀入樹形數(shù)據(jù)并展開,輸出到首頁,這個(gè)功能是封裝在javabean中的。
那么首頁jsp調(diào)用這個(gè)javabean時(shí),使用scope=application, 再通過樹形數(shù)據(jù)的緩沖算法,這樣,多個(gè)用戶同時(shí)訪問首頁時(shí),首頁jsp就無需每次啟動(dòng)javabean然后再反復(fù)讀取數(shù)據(jù)庫了。無疑大大提高速度。
所以如果你的首頁jsp訪問量很高,那么就應(yīng)該在這方面多花點(diǎn)時(shí)間優(yōu)化。
數(shù)據(jù)庫連接緩沖
<jsp:usebean id="cods"
class="oracle.jdbc.pool.oracleconnectioncacheimpl"
scope="application" />
<event:application_onstart>
<%
cods.seturl("jdbc:oracle:thin:@host:port:sid");
cods.setuser("scott");
cods.setpassword("tiger");
cods.setstmtcache (5);
%>
</event:application_onstart>
<%@ page import="java.sql.*, javax.sql.*, oracle.jdbc.pool.*" %>
<!----------------------------------------------------------------
* this is a javaserver page that uses connection caching over
application
* scope. the cache is created in an application scope in
globals.jsa file.
* connection is obtained from the cache and recycled back once
done.
--------------------------------------------------------------------!>
<html>
<head>
<title>
conncache jsp
</title>
</head>
<body bgcolor=eofffo>
<h1> hello
<%= (request.getremoteuser() != null? ", " +
request.getremoteuser() : "") %>
! i am connection caching jsp.
</h1>
<hr>
<b> i get the connection from the cache and recycle it back.
</b>
<p>
<%
try {
connection conn = cods.getconnection();
statement stmt = conn.createstatement ();
resultset rset = stmt.executequery ("select ename, sal " +
"from scott.emp order by ename");
if (rset.next()) {
%>
<table border=1 bgcolor="c0c0c0">
<th width=200 bgcolor="white"> <i>employee name</i> </th>
<th width=100 bgcolor="white"> <i>salary</i> </th>
<tr> <td align=center> <%= rset.getstring(1) %> </td>
<td align=center> $<%= rset.getdouble(2) %> </td>
</tr>
<% while (rset.next()) {
%>
<tr> <td align=center> <%= rset.getstring(1) %> </td>
<td align=center> $<%= rset.getdouble(2) %> </td>
</tr>
<% }
%>
</table>
<% }
else {
%>
<p> sorry, the query returned no rows! </p>
<%
}
rset.close();
stmt.close();
conn.close(); // put the connection back into the pool
} catch (sqlexception e) {
out.println("<p>" + "there was an error doing the query:");
out.println ("<pre>" + e + "</pre>
<p>");
}
%>
</body>
</html>
使用application緩存數(shù)據(jù)庫的連接,每次使用時(shí),從緩沖中取出,用完就返回。
新聞熱點(diǎn)
疑難解答
圖片精選