Using JSP Bean in a Servlet

假装没事ソ 提交于 2019-12-24 19:11:26

问题


So I'm using a bean on a JSP page to store some data, like so:

<jsp:useBean id="data" class="myclass" scope="session" />

Is there anyway to access this bean from a servlet at a later time in the same session?

EDIT:

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?


回答1:


Yes, you can obtain it as attribute from the session by the id as key.

Data data = (Data) request.getSession().getAttribute("data");

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.



来源:https://stackoverflow.com/questions/3418469/using-jsp-bean-in-a-servlet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!