user2714195
user2714195

Reputation: 59

passing Java object to another JSP

is there any way that i can declare an instance of a Java Object in one JSP file, and call this instance in another JSP file?

for example: in 1.jsp:

<%
  Obj o = new Obj();
%>

and in 2.jsp:

<@%include file = "1.jsp">
<%
  o.toString();
%>

(of course, the above example does not work. it's just to clearify what i was talking about).

Thank you!

Upvotes: 1

Views: 6274

Answers (2)

anbu selvan
anbu selvan

Reputation: 745

You should defining the java object as Bean in JSP. The Bean in JSP can be defined using :- < jsp:useBean..> standard jsp tag. And set and get property using < jsp:setProperty..> and < jsp:getProperty..> standard jsp tags.And then you can use the Object as well as share between jsp pages.

Upvotes: 0

Piotr M&#252;ller
Piotr M&#252;ller

Reputation: 5558

You can put your object as request attribute:

Obj o= new Obj();
request.setAttribute("myCreatedObject" , o );

...

Obj myObj = (Obj)request.getAttribute("myCreatedObject");

Consider another contexts than request like session, page - it's like variable scope.

Upvotes: 5

Related Questions