Java ServletContext

前端 未结 5 798
遥遥无期
遥遥无期 2021-01-23 16:26

I have a JSP web site, not Spring MVC, and it has a config file web.xml.

There are a couple of settings within the web.xml file that I\'d like to get.

However, I

相关标签:
5条回答
  • 2021-01-23 16:58

    I really don't like classes reading from web.xml... Why do you need that? IMHO it would be easier, cleaner and by far much more manageable if you prepared a properties file and a manager class that reads from there.

    0 讨论(0)
  • 2021-01-23 17:00

    I think this is very close to your description: link.

    Basically, you want to read parameters from web.xml programatically, right?

    0 讨论(0)
  • 2021-01-23 17:06

    This is not easily possible and may not be an elegent solution. The only option I can suggest is to have your configuration options i a xml or properties file and put it in your WEB-INF/classes directory so you can look it up using ClassLoader.getResource or ClassLoader.getResourceAsStream

    I know it may be a duplication of the configuration, but IMO its the elegent way.

    0 讨论(0)
  • 2021-01-23 17:08

    Using a javax.servlet.ServletContextListener implementation, that allows a singleton-like access to context:

    package test.dummy;
    import javax.servlet.ServletContextListener;
    import javax.servlet.ServletContextEvent;
    
    public  class ContextConfiguration implements ServletContextListener {
    
      private static ContextConfiguration _instance;
    
      private ServletContext context = null;
    
      //This method is invoked when the Web Application
      //is ready to service requests
      public void contextInitialized(ServletContextEvent event) {
        this.context = event.getServletContext();
    
        //initialize the static reference _instance
         _instance=this;
      }
    
      /*This method is invoked when the Web Application has been removed 
      and is no longer able to accept requests
      */
      public void contextDestroyed(ServletContextEvent event) {
        this.context = null;
    
      }
    
      /* Provide a method to get the context values */
      public String getContextParameter(String key) {
         return this.context.getInitParameter(key);
      }
    
      //now, provide an static method to allow access from anywere on the code:
      public static ContextConfiguration getInstance() {
         return _instance;
      }
    }
    

    Set it up at web.xml:

    <web-app>
    <listener>
        <listener-class>
         test.dummy.ContextConfiguration
        </listener-class>
      </listener>
    <servlet/>
    <servlet-mapping/>
    </web-app> 
    

    And use it from anywhere at the code:

    ContextConfiguration config=ContextConfiguration.getInstance();
    String paramValue=config.getContextParameter("parameterKey");
    
    0 讨论(0)
  • 2021-01-23 17:08

    Hmmm... I am assuming that once your web app is up then you are not going to make any change in the web.xml....

    Now what you can do is a create a servlet which loads on the startup and initialize a singleton class. You can use the following setting in your web.xml.

      <servlet>
        <description></description>
        <display-name>XMLStartUp</display-name>
        <servlet-name>XMLStartUp</servlet-name>
        <servlet-class>com.test.servlets.XMLStartUp</servlet-class>
        <init-param>
          <param-name>log4j-init-file</param-name>
          <param-value>WEB-INF/classes/log4j.properties</param-value>
        </init-param>
        <load-on-startup>0</load-on-startup>
      </servlet>
    

    In tomcat if you set load-on-startup value 0, then it means that while loading it has got the highest priority. now in the servlets init method read all the init-parameters like this and set it in your singleton class.

    String dummy= getInitParameter("log4j-init-file");
    
    0 讨论(0)
提交回复
热议问题