java 如何创建可以跨多个页面或在框架/iframe 内部访问的全局 JSP 变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10107962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:39:58  来源:igfitidea点击:

How do I create a global JSP variable that I can access across multiple pages or inside frames/iframes?

javahtmljspvariablesglobal-variables

提问by user717236

Simply, how do I create a global variable in JSP, such that I can access it across other JSP pages and/or inside frames/iframes? I tried <%!..%> but I got an error that the variable could not be resolved in a separate jsp page. Is it even possible to access JSP variables in more than one page without resorting to query strings, session variables, etcetera?

简单地说,我如何在 JSP 中创建一个全局变量,以便我可以跨其他 JSP 页面和/或框架/iframe 内部访问它?我尝试了 <%!...%> 但我收到一个错误,指出无法在单独的 jsp 页面中解析该变量。甚至可以在一个以上的页面中访问 JSP 变量而不求助于查询字符串、会话变量等吗?

Thank you.

谢谢你。

回答by Luiggi Mendoza

As I've already commented, you can use ServletContextto maintain the variables for all your application. Do not confuse this with static variables, because the ServletContext will die when your application is undeployed but static variables will be alive until the JVM is turned off.

正如我已经评论过的,您可以使用ServletContext来维护所有应用程序的变量。不要将其与静态变量混淆,因为当您的应用程序被取消部署时 ServletContext 将死亡,但静态变量在 JVM 关闭之前将保持活动状态。

You can save variables in ServletContext by using setAttributemethod. Also, you can get the actual value by using getAttribute. Let's see a sample of ServletContext in a servlet:

您可以使用setAttribute方法将变量保存在 ServletContext 中。此外,您可以使用getAttribute. 让我们看一个 servlet 中的 ServletContext 示例:

public class SomeServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        ServletContext servletContext = getServletContext();
        String someAttribute = servletContext.getAttribute("someAttribute");
        System.out.println("someAttribute value: " + someAttribute);
    }
}

Also, you can use a Listener to ServletContext, so you can execute some code when the application starts (is deployed correctly) to initialize the attributes on the ServletContext) and when it finish (before its undeployed).

此外,您可以使用 ServletContext 侦听器,因此您可以在应用程序启动(正确部署)时执行一些代码以初始化 ServletContext 上的属性)和完成时(在取消部署之前)。

public final class MyAppListener implements ServletContextListener {
    public void contextInitialized(ServletContextEvent event) {
        System.out.println("Application gets started.");
        ServletContext servletContext = event..getServletContext();
        servletContext.setAttribute("someAttribute", "Hello world!");
    }

    public void contextDestroyed(ServletContextEvent event) {
        System.out.println("Application has finished.");
    }
}

If you're using Java EE 5, you should configure the listener in the web.xml

如果您使用的是 Java EE 5,则应在 web.xml 中配置侦听器

<listener>
    <listener-class>mypackage.listener.MyAppListener</listener-class>
</listener>