Analysis of Application in JSP program

Source: Internet
Author: User

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

  1. <jsp:useBean id="cods" 
  2. class="oracle.jdbc.pool.OracleConnectionCacheImpl"   
  3. scope="application" /> 
  6. <event:application_OnStart> 
  7. <%   
  8. cods.setURL("jdbc:oracle:thin:@HOST:PORT:SID");  
  9. cods.setUser("scott");  
  10. cods.setPassword("tiger");  
  11. cods.setStmtCache (5);   
  12. %> 
  13. </event:application_OnStart> 
  16. <%@ page import="java.sql.*, javax.sql.*, oracle.jdbc.pool.*" %> 
  18. <!----------------------------------------------------------------  
  19. * This is a JavaServer Page that uses Connection Caching over  
  20. application  
  21. * scope. The Cache is created in an application scope in  
  22. globals.jsa file.   
  23. * Connection is obtained from the Cache and recycled back once  
  24. done.  
  26. --------------------------------------------------------------------!> 
  28. <HTML>   
  29. <HEAD>   
  30. <TITLE> 
  31. ConnCache JSP  
  32. </TITLE> 
  33. </HEAD> 
  35. <H1> Hello   
  36. <%= (request.getRemoteUser() != null? ", " +  
  37. request.getRemoteUser() : "") %> 
  38. ! I am Connection Caching JSP.  
  39. </H1> 
  40. <HR> 
  41. <B> I get the Connection from the Cache and recycle it back.  
  42. </B>   
  44. <P> 
  45. <%  
  46. try {  
  47. Connection conn = cods.getConnection();  
  49. Statement stmt = conn.createStatement ();  
  50. ResultSet rset = stmt.executeQuery ("SELECT ename, sal " +   
  51. "FROM scott.emp ORDER BY ename");  
  53. if ( {  
  54. %> 
  56. <TH WIDTH=200 BGCOLOR="white"> <I>Employee Name</I> </TH> 
  57. <TH WIDTH=100 BGCOLOR="white"> <I>Salary</I> </TH> 
  58. <TR> <TD ALIGN=CENTER> <%= rset.getString(1) %> </TD> 
  59. <TD ALIGN=CENTER> $<%= rset.getDouble(2) %> </TD> 
  60. </TR> 
  62. <% while ( {  
  63. %> 
  65. <TR> <TD ALIGN=CENTER> <%= rset.getString(1) %> </TD> 
  66. <TD ALIGN=CENTER> $<%= rset.getDouble(2) %> </TD> 
  67. </TR> 
  69. <% }  
  70. %> 
  71. </TABLE> 
  72. <% }   
  73. else {  
  74. %> 
  75. <P> Sorry, the query returned no rows! </P> 
  77. <%   
  78. }  
  79. rset.close();  
  80. stmt.close();  
  81. conn.close(); // Put the Connection Back into the Pool  
  83. } catch (SQLException e) {  
  84. out.println("<P>" + "There was an error doing the query:");  
  85. out.println ("<PRE>" + e + "</PRE> \n <P>");  
  86. }  
  87. %> 
  89. </BODY> 
  90. </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.

  1. How to Use JavaMail in JSP
  2. Dynamically generate and call JSP programs in JSP
  3. JSTL introduction-New JSP programming components allow tag Programming
  4. Eclipse plug-ins required for JSP development and installation and configuration methods
  5. JDBC and JSP simulate the MVC three-layer architecture

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.