In the life cycle of the application object, every JSP program running on the current server can access the values of parameters (or Java objects) bound to the application object. These features of the application object allow us to share some global information (such as the current number of online users) in multiple JSP programs.
Application attribute range
It is saved on the entire server and can be used by all users, but the attributes cannot be obtained after the server is restarted.
Set application attribute range: application_scope_01.jsp
The code is as follows: |
Copy code |
<% @ Page language = "java" contentType = "text/html" pageEncoding = "UTF-8" %> <% @ Page import = "java. util. *" %> <Html> <Head> <Title> Test </title> </Head> <Body> <% Application. setAttribute ("name", "James "); Application. setAttribute ("birthday", new Date ()); %> <A href = "application_scope_02.jsp"> application link jump </a> </Body> </Html> Application_scope_02.jsp <% @ Page language = "java" contentType = "text/html" pageEncoding = "UTF-8" %> <% @ Page import = "java. util. *" %> <Html> <Head> <Title> Test </title> </Head> <Body> <% String username = (String) application. getAttribute ("name "); Date birthday = (Date) application. getAttribute ("birthday "); %> <H1> Name: <% = username %> <H2> Birth: <% = birthday %> </Body> </Html> |
The application object shares data between users and stores global variables. It starts from the start of the server until the server is closed. During this period, this object will always exist. In this way, the object can be connected between users or between users, you can perform operations on the same attribute of this object. Operations on this object attribute anywhere will affect access by other users. The startup and shutdown of the server determine the life of the application object. It is an instance of the ServletContext class.
Serial number method description
1 Object getAttribute (String name) returns the attribute value to the name
2 Enumeration getAttributeNames () returns the Enumeration of all available attribute names
3 void setAttribute (String name, Object obj)
4 void removeAttribute (String name) delete a property and its attribute values
5 String getServerInfo () returns the JSP (SERVLET) engine name and version number.
6 String getRealPath (String path) returns the actual path of a virtual path.
7 ServletContext getContext (String uripath) returns the application object of the specified WebApplication.
8 int getMajorVersion () returns the maximum Servlet API version supported by the server.
9 int getMinorVersion () returns the maximum Servlet API version supported by the server.
10 String getMimeType (String file) returns the MIME type of the specified file
11 URL getResource (String path) returns the URL path of the specified resource (file and directory)
12 InputStream getResourceAsStream (String path) returns the input stream of the specified resource
13 RequestDispatcher getRequestDispatcher (String uripath) returns the RequestDispatcher object of the specified resource
14 Servlet getServlet (String name) returns the Servlet with the specified name
15 Enumeration getServlets () returns the Enumeration of all servlets
16 Enumeration getServletNames () returns the Enumeration of all Servlet names
17 void log (String msg) writes the specified message to the Servlet log file
18 void log (Exception exception, String msg) writes the stack track and error message of the specified Exception to the Servlet log file
19 void log (String msg, Throwable throwable) writes stack tracing and Throwable exception descriptions to the Servlet log file.