How to set the application variable

Source: Internet
Author: User
The application| variable application variable is a valid global variable for a Web application and is useful for saving some globally valid values. Unlike session variables, application variables can be accessed using Application.getattribute () and Application.setattribute ().

In JSP, you can automatically reference the application variable, referring to the current Web application. (For Web applications, I'll discuss them in another place.) )

When referencing a application variable, you must use synchronization. Also, you need to test whether a application property already exists. Please study the following code, set a application property called "Change" below, and save an integer value. With respect to using application variables in a servlet, the method is consistent with the following code, however, you need to first describe a application as follows:

ServletContext Application=getservletcontext ();

The following function is a fragment of env.java that can be used to see how to use Application variables in a servlet.

Integer count = null;

Synchronized (application) {

Count = (Integer) application.getattribute ("Change");

if (count = null)

Count = new Integer (0);

Count = new Integer (Count.intvalue () + 1);

Application.setattribute ("Change", count);

}



private void Printapplication (PrintWriter pw, httpservletrequest req)

Throws IOException

{

ServletContext application = Getservletcontext ();



Pw.println ("
Pw.println ("<table>");

Pw.print ("<tr><td>major version<td>");

Pw.println (Application.getmajorversion ());

Pw.print ("<tr><td>minor version<td>");

Pw.println (Application.getminorversion ());

Pw.print ("<tr><td>server info<td>");

Pw.println (Application.getserverinfo ());

Pw.print ("<tr><td>real Path (of PathInfo) <td>");

Pw.println (Application.getrealpath (Req.getpathinfo ()));

Pw.print ("<tr><td>mime-type (of PathInfo) <td>");

Pw.println (Application.getmimetype (Req.getpathinfo ()));

Pw.println ("</table>");



Pw.println ("
Pw.println ("<table>");



Enumeration E = Application.getattributenames ();

while (E.hasmoreelements ()) {

String name = (string) e.nextelement ();

Pw.print ("<tr><td>");

Pw.print (name);

Pw.print ("<td>");

Pw.println (Application.getattribute (name));

}



Pw.println ("</table>");

}



%>





<body bgcolor= #ffffff >






</body>






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.