JSP/servlet read parameters from properties file?

前端 未结 4 960
梦谈多话
梦谈多话 2020-12-31 22:34

My JSP pages need to display different information depending on which environment they\'re in (dev, production, sandbox, etc). I want to have a properties file for each of

4条回答
  •  借酒劲吻你
    2020-12-31 22:42

    • You can load the properties using java.util.Properties (or commons-configuration) in a ServletContextListener's contextInitialized(..) method.

    • register the listener with in web.xml

    • You then store the Properties into the ServletContext (you can get it from the event) (ctx.setAttribute("properties", properties)

    • then access the properties using ${applicationScope.properties.propName} (as BalusC noted, applicationScope is optional)

    Update:

    Initially I thought spring had some ready-to-use facility for that, but it turns out it's not exactly the case. You have two options:

    • this article explains something similar to my suggestion above, but using spring's PropertyPlaceholderConfigurer

    • this answer and this answer allow you to expose all your beans, including a PropertyPlaceholderConfigurer to the servlet context.

提交回复
热议问题