meteoritepanama
meteoritepanama

Reputation: 6252

Using JSP Bean in a Servlet

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?

Upvotes: 0

Views: 2381

Answers (1)

BalusC
BalusC

Reputation: 1109532

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.

Upvotes: 5

Related Questions