The method of using application in JSP programming

Source: Internet
Author: User
Tags relative stmt

JSP call Javeabean command Usebean has scope settings, generally have application session page, and so on, page is the Usebean of each page to recreate the JavaBean new objects, the general situation is this, If you are sharing data between multiple JSP programs, you can use the session

and application meaning, the JavaBean will always exist, and the session relative to the user, application is relative to the application, in general, a user has a session, and as the user left and disappeared , and application is always there, like a servlet program, like the entire system of "global variables", and only one instance.

Control functions in MVC

So application this feature, is suitable to do MVC control function, the general MVC is used to do control functions, V is the basic JSP page, M is the middleware JavaBean and so on.

But with the improvement of JSP functions and promotion, and gradually replace the servlet trend, we in practice more use is JSP, sometimes in order to save trouble things, use JSP instead of servlet. especially its control function.

In fact, this control function is encapsulated in a JavaBean, JSP uses Scope=application to invoke this javabean, so that the JavaBean with control function is similar to the servlet resident memory, and interacts with various middleware in the background.

"Home" of the show

In practical applications, we often have multiple users to visit a page at the same time, such as the homepage, the home page has a lot of functions to run, such as directory classification, home program to read from the database tree data and expand, output to the home page, this function is encapsulated in the JavaBean.

Then home JSP call this JavaBean, use the scope=application, and then through the tree-shaped data buffer algorithm, so that many users at the same time to visit the home page, JSP will not need to start every time JavaBean and then repeatedly read the database. Undoubtedly greatly improve the speed.

So if your homepage JSP traffic is very high, then you should spend more time in this area optimization.

Database connection buffering

<jsp:usebean id= "Cods"
Class= "Oracle.jdbc.pool.OracleConnectionCacheImpl"
Scope= "Application"/>
<event:application_OnStart>
<%
Cods.seturl ("Jdbc:oracle:thin: @HOST:P ort: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 which uses Connection Caching over
Application
* scope. The Cache is created in a 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" 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>
&LT;TD align=center> $<%= rset.getdouble (2)%> </TD>
</TR>
<% while (Rset.next ()) {
%>
<TR> <td align=center> <%= rset.getstring (1)%> </TD>
&LT;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 is an error doing the query:");
Out.println ("<PRE>" + E + "</PRE> \ n <P>");
}
%>
</BODY>
</HTML>

Use application to cache the connection to the database, take it out of the buffer each time you use it, and return it when you run out.

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: info-contact@alibabacloud.com 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.