在java中使用应用程序范围变量 [英] Using application scope variables in java

查看:81
本文介绍了在java中使用应用程序范围变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的公司在接下来的几个月重做我们的网站,从ColdFusion网站转到使用Java编写的网站。我只是在学习Java,我很好奇如何在Java Web应用程序中设置应用程序范围变量。 ColdFusion具有application.cfm文件,该文件包含应用程序内所有ColdFusion页面/组件都可访问的变量。 Java显然没有与该文件直接等价的东西,所以我想知道如何在Java中重新创建类似的东西。我希望有一个中心位置,即web应用程序中的所有jsp页面,servlet,类等都可以访问。



那么做这种事情的最佳方法是什么?



有没有人有任何类似的例子他们做到了?

应用程序范围变量应放在类中吗?一个XML文件?一个jsp页面?还有其他什么东西?



在java webapp中使用应用程序范围变量a甚至是可行的吗?例子:

b $ b这将是一个保存图像文件夹路径的地方,所以在单独的JSP页面或类或任何需要访问该图像文件夹的内容中,您可以引用该路径的应用程序范围变量而不是将其写出在每个地方。然后,如果由于某种原因我们需要移动该图像文件夹的位置,我们只需在一个位置更改路径变量,而不必更新引用该图像文件夹的20个位置。



在我的在线研究中,我很难找到关于此类事物的任何信息,这就是为什么我在这里询问它的原因。我知道要求解释这种类型的东西有很多,但我想我会问,看看我能得到什么类型的答复。



谢谢您可以提供有关此主题的任何帮助。 解决方案

相当于 ServletContext

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

就像在会话对象中一样,您也可以在servlet上下文中存储属性

  context.setAttribute(someValue,aValue); 

Object attribute = context.getAttribute(someValue);

如果您刚开始使用Java并且您有一个不平凡的应用程序要构建,我会建议使用流行的框架,如 Spring Struts


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.

解决方案

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

这篇关于在java中使用应用程序范围变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆