Set the configuration of the properties file to the global variable implementation method of the entire Web application. properties global variable
Four major scopes:
Variables in Web applications are stored in different jsp objects with different scopes. the sorting of four different scopes is pageContext <request <session <application;
1. pageContext: Page domain,Valid only for the current page. After the page is left, the pageContext attribute values are invalid no matter the redirection or redirection (whether redirect or forward;
2. request: request domain,Valid in one request. If forward is used for redirection, the attribute values in the last request can be retained in the next request, and the property values in the last request will be invalid when redirect redirection is redirected to another page;
3. session: session domain,During a session (from opening the browser to closing the browser), the attribute values of the session object remain valid. During this session, the values of the session can be obtained on any page;
4. application: application domain,As long as the application is not closed, the attribute value in this object is valid and shared by all sessions.
Using the ServletContextListener listener, once the application is loaded, the properties value is stored in the application.
Now, you need to be able to read properties from all JSPs using EL expressions and target all sessions. Therefore, the application scope is used here,
So when will properties be stored in the application? Because the attribute values of properties are used as global variables to facilitate any EL acquisition, the values are stored in the application during web application loading,
Here we will use ServletContextListener:
ServletContextListener is an interface in the Servlet API. It can listen to the lifecycle of the ServletContext object, which is actually the lifecycle of the Web application.
When a Servlet container starts or terminates a Web application, the ServletContextEvent event is triggered, which is handled by ServletContextListener.
The procedure is as follows:
1. Create a class PropertyListenter to implement the contextInitialized method of the ServletContextListener interface;
2. Read the properties configuration file and store it in Map;
3. Use the ServletContext object to store the Map in the application scope;
/*** Set the value global variable * @ author meikai * @ version October 23, 2017 2:15:19 */public class PropertyListenter implements ServletContextListener {/* (non-Javadoc) * @ see javax. servlet. servletContextListener # contextDestroyed (javax. servlet. servletContextEvent) */@ Override public void contextDestroyed (ServletContextEvent arg0) {// TODO Auto-generated method stub}/* (non-Javadoc) * @ see javax. servlet. servletCont ExtListener # contextInitialized (javax. servlet. servletContextEvent) */@ Override public void contextInitialized (ServletContextEvent sce) {/*** read properties file **/final Logger logger = (Logger) LoggerFactory. getLogger (PropertyListenter. class); Properties properties = new Properties (); InputStream in = null; try {// obtain the properties file stream through the class loader in = PropertiesUtil. class. getClassLoader (). getResourceAsStr Eam ("kenhome-common.properties"); properties. load (in);} catch (FileNotFoundException e) {logger. error ("properties file not found");} catch (IOException e) {logger. error ("IOException occurred");} finally {try {if (null! = In) {in. close () ;}} catch (IOException e) {logger. error ("an exception occurred when the properties file stream is closed");}/*** transfers the properties file to map */Map <String, String> pros = new HashMap <String, string> (Map) properties);/*** store Map to the global scope through ServletContext */ServletContext sct = sce. getServletContext (); sct. setAttribute ("pros", pros );}}
4. Configure the listener PropertyListenter in web. xml:
<! -- The global variable listener reads the properties file and sets the value to the global variable --> <listener-class> com. meikai. listener. propertyListenter </listener-class> </listener>
After configuration, run the Web application to obtain the attribute values in properties using the EL expression on all jsp pages.
In the above section, the configuration of the properties file is set to the global variable implementation method of the entire Web application. This is all the content that I have shared with you. I hope to give you a reference, we also hope that you can support the customer's home.