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

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

问题描述

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

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?

应用范围变量应该放在一个类中吗?一个xml文件?一个jsp页面?还有什么?

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

在java webapp中使用应用范围变量a是否可行?

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

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

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.

推荐答案

等价的可能是 ServletContext

在您的控制器 servlet 中:

In your controller servlet:

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

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

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");

如果您刚开始使用 Java 并且要构建一个重要的应用程序,我建议您使用一个流行的框架,例如 SpringStruts

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天全站免登陆