The JSP program calls the Javeabean command UseBean with Scope settings. Generally, there are Application session page and other settings. Page means that each page re-generates a new usean object in usebean. This is generally used, if multiple JSP programs share data, you can use session
Application means that the javabean will always exist. Compared with the session, the Application is relative to the Application. Generally, a user has a session and disappears as the user leaves; the Application always exists, similar to a servlet program, similar to the "global variable" of the entire system, and there is only one instance.
Control functions in MVC
Therefore, the application feature is suitable for MVC control functions. In general, traditional MVC uses servlet 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 tendency to replace servlet. In practice, JSP is more used. Sometimes, to save trouble, JSP is used instead of servlet. especially its control functions.
In fact, this control function is encapsulated in a Javabean. the JSP program uses scope = application to call this Javabean. In this way, the javabean with the control function is similar to the servlet resident memory, and interact with various background middleware.
Display of "Homepage"
In practical applications, we often have multiple users accessing a page at the same time, such as the homepage, which has many functions to run, such as Directory classification, the homepage program reads tree data from the database, expands the data, and outputs the data to the homepage. This function is encapsulated in Javabean.
When the homepage JSP program calls this Javabean, it uses scope = application and then uses the tree data buffer algorithm. In this way, when multiple users access the homepage at the same time, the homepage JSP program does not need to start Javabean every time and then read the database repeatedly. Undoubtedly, the speed is greatly improved.
Therefore, if your homepage JSP has a high access volume, you should spend more time optimizing it.
Database Connection Buffer
- <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> \n <P>");
- }
- %>
-
- </BODY>
- </HTML>
In the actual running of the JSP program, the Application caches the connection of the database. Each time it is used, it is retrieved from the buffer and returned when it is used up.
- How to Use JavaMail in JSP
- Dynamically generate and call JSP programs in JSP
- JSTL introduction-New JSP programming components allow tag Programming
- Eclipse plug-ins required for JSP development and installation and configuration methods
- JDBC and JSP simulate the MVC three-layer architecture