在JSP编程下Application的使用方法详解
发布时间:2023-05-11 13:29:51 所属栏目:教程 来源:
导读:jsp调用javeabean命令usebean中有scope设置,一般有application session page等设置,page就是每页重新产生usebean中的javabean新对象,一般情况是用这种,如果多个jsp程序间为共享数据,可以使用session
而
而
jsp调用javeabean命令usebean中有scope设置,一般有application session page等设置,page就是每页重新产生usebean中的javabean新对象,一般情况是用这种,如果多个jsp程序间为共享数据,可以使用session 而application的意思,该javabean将一直存在,与session相对用户来说,application是相对应用程序的,一般来说,一个用户有一个session,并且随着用户离开而消失;而application则是一直存在,类似一个servlet程序,类似整个系统的"全局变量",而且只有一个实例。 mvc中控制功能 因此application这个特性,很适合用来做mvc中的控制功能,一般传统mvc是用servlet做控制功能,v基本是jsp页面,m就是中间件javabean之类。 但是随着jsp功能的完善和推广,逐渐有替代servlet之趋势,我们在实践中更多使用的也是jsp,有时为了省却麻烦的事情,就使用jsp代替servlet.尤其是其控制功能。 实际上,这个控制功能是封装在一个javabean中,jsp使用scope=application来调用这个javabean,这样,具备控制功能的javabean就类似servlet常驻内存,并和后台各种中间件交互操作。 “首页”的展现 在实际应用中,我们经常有多个用户要同时访问一个页面,如首页,这个首页中有很多功能要运行,比如目录分类,首页程序要从数据库中读入树形数据并展开,输出到首页,这个功能是封装在javabean中的。 那么首页jsp调用这个javabean时,使用scope=application, 再通过树形数据的缓冲算法,这样,多个用户同时访问首页时,首页jsp就无需每次启动javabean然后再反复读取数据库了。无疑大大提高速度。 所以如果你的首页jsp访问量很高,那么就应该在这方面多花点时间优化。 ||| 数据库连接缓冲 <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缓存数据库的连接,每次使用时,从缓冲中取出,用完就返回。 (编辑:聊城站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
站长推荐