0

In a Java web application, where is the best place to set (or best mechanism to use in setting) a string (or strings) for use, application-wide, which should never change while the application is running, and most likely never after it is installed on a given server?

One thing that would be key in this is that I want to be able to access it anywhere (in a Java Class -OR- in a JSP), as this would be for things like an application name, URL, address, telephone number, etc.

I believe the "easiest" would be to use application.setAttribute() in every single JSP (or perhaps in a global include file or such), but this hardly makes sense, as it never changes - why keep setting it? However, setting it in the application context would offer the ability to use EL expressions or application.getAttribute() to retrieve the value - is there a better way or a better place to set attributes like this? somehow in web.xml? not sure why it's so hard to find... maybe I just don't know the question to ask Google.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Code Jockey
  • 6,611
  • 6
  • 33
  • 45

1 Answers1

1

Use a ServletContextListener.

@WebListener
public class Config implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent event) {
        Data data = createItSomehow();
        event.getServletContext().setAttribute("data", data);
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        // NOOP.
    }

}

It'll be available in EL scope by ${data}.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • three questions: (1) do I just add this class to my project and it will work, or do I have to reference it somewhere special? (2) how do I access it in a Java Class? `servletContext.getAttribute("data")` or something? and (3) how many times / how often is this run? only once per application? once per session? once per servlet initialization? – Code Jockey Mar 07 '12 at 15:53
  • 1) Yes, assuming that you're on at least Servlet 3.0. Otherwise you need to register it as `` in `web.xml`. 2) What do you think yourself? :) It's set as servlet context attribute, so it should also be available as servlet context attribute. 3) Once on webapp's startup, like as you asked. – BalusC Mar 07 '12 at 16:00
  • Thanks - I'll look into this - I'm pretty familiar with `get/setAttribute()` in the _session_ and _request_ objects and even _pageContext_, I've heard of `application.get/setAttribute()`, but I've not seen much mention of any _servletContext_ object yet - still somewhat new to the Java World, I guess :) – Code Jockey Mar 07 '12 at 16:08
  • `ServletContext` in Servlet code == `application` in JSP code. – BalusC Mar 07 '12 at 16:08
  • Do you want to access the value in your non-servlet classes ? If you do, then it will take some more code. Please ask us if you want to see it. – rickz Mar 07 '12 at 16:49
  • @rickz: Just make the `Data` example in my answer a singleton-like class (static final class or enum). – BalusC Mar 07 '12 at 16:51
  • Ideally I would like to be able to access it via a single statement or EL expression in ANY file that can contain Java or HTML --- I have been programming for a few months here (a lot of CSS, Javascript and basic Java coding), coming from a primarily .Net world and for the most part desktop application development background, so I'm not entirely sure what makes a class a servlet class vs non-servlet. We're using Spring and Hibernate and as a team we are taking over from another team that did the initial development many years ago. The project is basically updating the code and methodologies. – Code Jockey Mar 07 '12 at 17:02
  • Uh, just a class which doesn't `extends HttpServlet` (and hence doesn't have `ServletContext` at its hands and so forth). Last but not least, I suggest you to go through [our `servlets` tag wiki page](http://stackoverflow.com/tags/servlets/info). It contains lot of valuable information and links to high quality tutorials. – BalusC Mar 07 '12 at 17:04
  • In the .Net world, you can access the "Current" Servlet context from a class library. We can do the same in Java. But we have to do coding ourselves. – rickz Mar 07 '12 at 17:17
  • @rickz: .NET (MVC) is not comparable to JSP/Servlet. It's more comparable to JSF. In JSF, there's the `FacesContext`. JSP/Servlet is more comparable to classic ASP. – BalusC Mar 07 '12 at 17:20
  • This is working for JSPs, but I'm not seeing how to use it in the Spring MVC controllers or other classes - feel free to help me out there if you can :) – Code Jockey Mar 07 '12 at 19:46
  • I don't do Spring MVC. I do JSF. Just ask a new question in Spring MVC tag. – BalusC Mar 07 '12 at 20:24