JSP details -- application
Application Object
The application object is used to save the common data in all applications. It is automatically created when the server is started and destroyed when the server is stopped. When the application object is not destroyed, all users can share the application object. Compared with session, the application object has a longer life cycle, similar to "global variables"
1. Access Application initialization parameters
Application provides methods to access application initialization parameters. Application initialization parameters are set in the web. xml file, where the web. xml file is located in the WEB-INF subdirectory under the directory where the Web application is located. In web. xml Configure the initialization parameters of the application.
Example:
The url parameters required by the MySQL database are configured in web. xml. The example is as follows:
Url
Jdbc: mysql: 127.0.0.1: 3306/db_database
The application Object provides two methods to access application initialization parameters. They are described as follows:
A. getInitParameter () method:
In this method, the user returns the named parameter value. The syntax format is as follows:
Application. getInitParameter (String name );
Use this method to obtain the url parameter value in the preceding web. xml file. You can use the following code:
Application. getInitParameter ("url ");
B. getAttributeNames () method
Application. getAttributeNames () returns an enumeration of the application initialization parameter names defined. The syntax format is as follows:
Application. getAttributeNames ();
Example:
The web. xml file is as follows:
Xmlns = "http://java.sun.com/xml/ns/javaee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd>
Index. jsp
Url
Jdbc: mysql: 127.0.0.1: 3306/db_database
Result. jsp file to obtain the initialization parameters of the application.
<% @ Page language = "java" import = "java. util. *" pageEncoding = "ISO-8859-1" %>
<% @ Page import = "java. util. *" %>
<%
String path = request. getContextPath ();
String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
%>
My JSP 'result. jsp 'starting page
<%
Enumeration enumeration = application. getInitParameterNames ();
While (enumeration. hasMoreElements ())
{
String name = (String) enumeration. nextElement ();
String value = (String) application. getInitParameter (name );
Out. println (name );
Out. println (value );
}
%>
2. Manage the environment attributes of an application
You can use the following method to manage application environment attributes of an application object:
GetAttributeNames (): obtains the attribute names used by all application objects.
GetAttribute (String name): obtains the value of the specified object name from the application object.
SetAttribute (String key, Object obj): Set the attribute value of the application Object.
RemoveAttribute (String name): removes the attribute of the specified name from the application object.