问题描述
所以我在JSP页面上使用bean来存储一些数据,就像这样:
So I'm using a bean on a JSP page to store some data, like so:
<jsp:useBean id="data" class="myclass" scope="session" />
以后是否可以在同一会话中从servlet访问此bean?
Is there anyway to access this bean from a servlet at a later time in the same session?
显然,当我加载jsp页面和servlet时,我没有访问相同的会话.我正在打印会话ID,这给我两个页面一个不同的值,所以我无法访问Bean.有什么想法吗?
Apparently I'm not accessing the same session when I load the jsp page and the servlet. I'm printing out the session ID and it's giving me a different value for both pages, so I can't access the bean. Any ideas?
推荐答案
是的,您可以通过id
作为键从会话中获取它作为属性.
Yes, you can obtain it as attribute from the session by the id
as key.
Data data = (Data) request.getSession().getAttribute("data");
请注意,您需要将类放入包中,否则无法导入它.您还想给它一个比myclass
更合理的名称.
Note that you need to put classes in a package, else you cannot import it. You'd also like to give it a more sensible name than myclass
.
这篇关于在Servlet中使用JSP Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!