Servlet obtains and stores the context-param parameter in web. xml, servletweb. xml

Source: Internet
Author: User
Tags key string

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 );}}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.