Is there a way to access web.xml properties with Java Bean?

Is there a way in the servlet API to access the properties specified in web.xml (for example, initialization parameters) from a Bean or Factory class that is not associated with the web container at all?

For example, I am writing a Factory class, and I would like to include some logic in Factory to check the hierarchy of files and configuration locations, to find out which ones are available, to determine which implementation the class to instantiate - for example,

  • class properties file
  • web.xml parameter,
  • system property or
  • some default logic if nothing is available.

I would like to be able to do this without inserting links to ServletConfig or anything similar to my Factory - the code should work fine outside the servlet container.

This may sound a little unusual, but I would like this component that I'm working on to be packaged with one of our web applications, and also universal enough to be packaged with some of our on-line, without requiring The new properties file is only for my component, so I was hoping to link on top of other configuration files such as web.xml.

If I remember correctly, .NET has something like Request.GetCurrentRequest() to get a link to the current executable Request - but since this is a Java application, I'm looking for something simliar that can be used to access ServletConfig .

+4
source share
3 answers

One way to do this:

 public class FactoryInitialisingServletContextListener implements ServletContextListener { public void contextDestroyed(ServletContextEvent event) { } public void contextInitialized(ServletContextEvent event) { Properties properties = new Properties(); ServletContext servletContext = event.getServletContext(); Enumeration<?> keys = servletContext.getInitParameterNames(); while (keys.hasMoreElements()) { String key = (String) keys.nextElement(); String value = servletContext.getInitParameter(key); properties.setProperty(key, value); } Factory.setServletContextProperties(properties); } } public class Factory { static Properties _servletContextProperties = new Properties(); public static void setServletContextProperties(Properties servletContextProperties) { _servletContextProperties = servletContextProperties; } } 

And then in your web.xml

follow these steps:
 <listener> <listener-class>com.acme.FactoryInitialisingServletContextListener<listener-class> </listener> 

If your application is running in a web container, the listener will be called by the container after creating the context. In this case, _servletContextProperties will be replaced with any contextual parameters specified in web.xml.

If your application runs outside the web container, then _servletContextProperties will be empty.

+5
source

Have you considered using the Spring framework for this? This way, your beans do not get any extra cracks, and Spring does the configuration for you.

+1
source

I think you will have to add an associated bootstrap class that references ServletConfig (or ServletContext) and transcribes these values ​​to the Factory class. At least in this way you can pack it separately.

@toolkit: Great, most humiliated ones are what I tried to do for a while

0
source

Source: https://habr.com/ru/post/1276532/


All Articles