Java 如何在多个jsp页面之间共享存储的对象?
我想在从磁盘加载一个对象后,在多个JSP页面和会话之间共享它。到目前为止,我找到的最接近的解决方案是:Java 如何在多个jsp页面之间共享存储的对象?,java,jsp,javabeans,Java,Jsp,Javabeans,我想在从磁盘加载一个对象后,在多个JSP页面和会话之间共享它。到目前为止,我找到的最接近的解决方案是: <jsp:useBean id="inventory" class="shared.Inventory" scope="application" /> 然而,这限制了我使用一个新的bean。我想加载一个在应用程序启动时保存到磁盘的对象,并在所有JSP页面上共享它。应用程序启动时,您应该使用。然后,将其存储在应用程序范围中 public class AppListener im
<jsp:useBean id="inventory" class="shared.Inventory" scope="application" />
然而,这限制了我使用一个新的bean。我想加载一个在应用程序启动时保存到磁盘的对象,并在所有JSP页面上共享它。应用程序启动时,您应该使用。然后,将其存储在应用程序范围中
public class AppListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
//application is deployed
//create your object and load it
Inventory inventory = ...
//get the application context
ServletContext servletContext = sce.getServletContext();
//store the object in application scope
servletContext.setAttribute("inventory", inventory);
}
public void contextDestroyed(ServletContextEvent sce) {
//application is undeployed
}
}
然后,相应地在web.xml中注册过滤器:
package.where.you.store.AppListener
部署应用程序后,bean将可在所有用户的所有页面中使用,并可通过表达式语言进行访问:
${inventory.someField}
这是正确的方法。请更详细地解释您的问题。@LuiggiMendoza这种方法只允许我设置新“inventory”对象的属性,我已经在磁盘上保存了一个Inventory类的对象,我想在所有JSP页面上加载和共享该对象。因此,您想在应用程序启动时加载该对象,并在所有JSP文件上共享它吗?@luigimendoza这是正确的。抱歉搞混了,我本来应该这样写的。