Jsp calls the Javeabean command. There are Scope settings in UseBean, and generally there are settings such as Application session page. Page is
a new javabean object in usebean that is regenerated for each page. This is generally used. If multiple Jsp programs share data, you can use it.
Session
and application mean that the javabean will always exist. Compared with session, application is relative to application.
Generally speaking, a user has a session, and it will disappear when the user leaves; while application will always exist. , similar to
a servlet program, similar to the "global variables" of the entire system, and there is only one instance.
Control functions in MVC
Therefore, the application feature is very suitable for the control functions in MVC. Generally, traditional MVC uses servlets for control functions. V is
basically
a Jsp page, and M is a middleware Javabean.However, with the improvement and promotion of Jsp functions, there is a gradual trend of replacing servlets. In practice, we use Jsp more. Sometimes
in order to
save trouble, Jsp is used instead of servlet, especially its control function.In fact, this control function is encapsulated in a Javabean. Jsp uses scope=application to call this Javabean. In this way
, the Javabean with control function is like a servlet resident in memory and interacts with various background middleware.
The display of "home page"
in practical applications. We often have multiple users accessing a page at the same time, such as the home page. There are many functions to run on this home page, such as
directory classification. The home page program must read tree data from the database and Expand and output to the homepage. This function is encapsulated in Javabeans.
Then when the homepage Jsp calls this Javabean, use scope=application, and then use the tree data buffering algorithm. In this way, when multiple
users
access the homepage at the same time, the homepage JSP does not need to start the Javabean every time and then read the database repeatedly.It will undoubtedly greatly increase the speed.
So if your homepage Jsp has a high number of visits, you should spend more time optimizing it.
Database connection buffering
< 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>
Use the application to cache the database connection. Each time it is used, it is taken out of the buffer and returned after use.