Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using application scope variables in java

My company is redoing our website over the next few months, going from a ColdFusion website to one written in Java. I am just learning Java and I am curious as to how I can set application scope variables in a Java web application. ColdFusion has the application.cfm file that holds variables that are accessible by all ColdFusion pages/components within the app. Java obviously does not have a direct equivalent to that file, so I was wondering how to recreate something similar in Java. I want to have one central place that all jsp pages, servlets, classes, etc. in the webapp can access.

So what is the best way to do something like that?

Does anyone have any examples of something similar that they did?

Should application scope variables be placed in a class? an xml file? a jsp page? something else?

Is it even feasible to have application scope variables a in java webapp?

Example: It would be a place that holds say the path to an images folder, so in individual JSP pages or classes or whatever would need acces to that images folder, you could reference the application scope variable for that path instead of writing it out in each place. Then if for some reason we needed to move the location of that images folder, we would just change the path variable in the one location and not have to update 20 places that reference that images folder.

I have had trouble finding any information on this type of thing in my research online, which is why I am asking about it here. I know it is a lot to ask for an explanation to this type of thing, but I figured I would ask and see what type of responses I could get.

Thank you for any help you can provide about this topic.

like image 594
kei23th Avatar asked Jun 18 '12 21:06

kei23th


People also ask

What is application scope in Java?

Context/Application scope begins when a webapp is started and ends when it is shutdown or reloaded. Parameters/attributes within the application scope will be available to all requests and sessions. The Context/Application object is available in a JSP page as an implicit object called application.

What is scope of variable in Java explain with example?

The scope of variables in Java is a location (or region) of the program where the variable is visible to a program and can be accessible. In other words, the variable scope is the location from which we can access its value. The scope of variables determines its accessibility for other parts of the program.

What is the scope of instance variable Java?

A variable which is declared inside a class and outside all the methods and blocks is an instance variable. The general scope of an instance variable is throughout the class except in static methods. The lifetime of an instance variable is until the object stays in memory.


1 Answers

The equivalent is probably the ServletContext

In your controller servlet:

ServletContext context = request.getSession().getServletContext();

Just like in the session object you can store attributes in the servlet context as well

context.setAttribute("someValue", "aValue");

Object attribute = context.getAttribute("someValue");

If you're starting out with Java and you have a non-trivial application to build, I would recommend using a popular framework like Spring or Struts

like image 164
Brad Avatar answered Nov 15 '22 17:11

Brad