The application object represents the web application, so the application is used to operate web application-related data. The Application Object usually has the following two functions:
1. Data is shared among servlets across multiple JSP pages of the entire web application.
The application uses the setattribute (string attrname, object Value) method to set a value to the attrname attribute of the application. The value of this attribute is valid for the entire web application, therefore, each JSP page or servlet of the Web application can access this attribute by getattribute (string attrname ).
Website traffic statistics: application. setattribute ("count", String. valueof (I ++ ));
Note: on different pages, you can use application. getattribute ("count") to obtain the value of the count attribute. The attributes of the application are shared with the JSP of the entire web application.
No application built-in objects exist in the servlet. Each web application has only one servletcontext instance. On the JSP page, you can access the instance through the Application built-in object, while the servlet must obtain the instance through code.
2. Access the configuration parameters of the Web application.
You can use application. getinitparameter (string paramname) to obtain the configuration parameter. You must first configure the parameter in the context-Param element in the web. xml file.
<! -- Configure the first parameter -->
<Context-param>
<Param-Name> name1 </param-Name>
<Param-value> value1 </param-value>
`
`
`
`
`
</Context-param>
In this way, some configuration information can be configured in the web. xml file, avoiding the use of hard encoding in the Code, thus improving program portability.