Servlet obtains and stores the context-param parameter in web. xml, servletweb. xml
Context-param is defined in web. xml and will not be changed at will. Therefore, it is processed once in the listener. When the container starts, it reads and stores the content in Properties for future convenience.
The SysProperties class is used to store the context key value;
SystemListener listener class to process the context-param parameter.
/*** Stores the parameter key value */public final class SysProperties {private static SysProperties instance; private Properties initProperties = new Properties (); private SysProperties () {} public static SysProperties getInstance () {if (instance = null) {instance = new SysProperties ();} return instance ;} /*** get the corresponding value * @ param key String the provided key (param-name) * @ return String the corresponding value (param-value) */public String getProperty (String key) {return this. initProperties. getProperty (key);}/*** check whether the key exists * @ param key String key * @ return boolean returns true, otherwise, false */public boolean containsKey (String key) {return this. initProperties. containsKey (key);}/*** storage parameter * @ param key String param-name * @ param object String param-value */public void put (String key, String object) {this. initProperties. put (key, object );}}
/*** System listener. Parameters */public class SystemListener implements ServletContextListener {@ Override public void contextInitialized (incluservletcontextevent) {initContextParam (listener) are initialized and stored when the program starts );} @ Override public void contextDestroyed (ServletContextEvent servletContextEvent) {} private void initContextParam (ServletContextEvent event) {Enumeration <String> names = event. getServletContext (). getInitParameterNames (); while (names. hasMoreElements () {String name = names. nextElement (); String value = event. getServletContext (). getInitParameter (name); SysProperties. getInstance (). put (name, value );}}}