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 these environments that contain all the parameters they might need. How can I reference the properties from this file in a JSP page?
My thoughts are:
UPDATE - I should've mentioned I'm using Spring 3.0 and Spring webmvc. So if there's some best practices way to do this using Spring, that's ideal!
Create a property file in the package with extension '. properties' and use those properties defined in the file in jsp by importing the resource bundle package in the jsp.
Expand the Web Applications folder, then highlight the icon that represents your application. Choose File | Web Application Properties.
You can load the properties using java.util.Properties
(or commons-configuration) in a ServletContextListener
's contextInitialized(..)
method.
register the listener with <listener>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With