Results 1 to 4 of 4
- 04-27-2010, 11:17 AM #1
Member
- Join Date
- Apr 2010
- Posts
- 2
- Rep Power
- 0
Java JSP UseBean and Session scope
I have a problem, I have some class, let's say for example :
public class SomeClass {
private String myVar = "";
public SomeClass(){
}
public void setMyVar(String s){
this.myVar = s;
}
public String getMyVar(){
return myVar;
}
}
I am trying to use this in a JSP as a bean, like follows :
<jsp:useBean id="someclass" class="com.test.SomeClass" scope="session" />
on page1.jsp I make a new instance:
<%
someclass = new SomeClass();
someclass.setMyVar("test");
%>
Then I try to get the data on page2.jsp where I also include the UseBean :
<jsp:useBean id="someclass" class="com.test.SomeClass" scope="session" />
Inside body tag :
myVar value is : <%= someclass.getMyVar() %>
This ends up returning NULL all the time. My bean is not saved in the session.
I can make a workaround by saving the class in :
....session.setAttribute("test", someclass)....
then I can get the class in page2.jsp by session.getAttribute and casting that to the apropriate class, but that's going to be a real hassle over time as the project grows.
Anyone have any idea why session.setAttribute and .getAttribute would work just fine, but my Bean is not working? I am clueless now.
Thanks.
- 04-27-2010, 12:00 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,475
- Rep Power
- 16
useBean actualyl does the creation.
Your second "new SomeClass()" is unecessary, and the instance created by that is not stored in the session. Consequently the setMyVar() is not on the instance of SomeClass that is stored in the session. So, skip the "new".
- 04-27-2010, 12:35 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 2
- Rep Power
- 0
Thank you. I found the problem by your explaination.
In my real project I was actually passing the bean(user) into another bean(database) which made a new user instance and assigned that to the first bean(user).
So "user" ended up being a new instance and not the one created in UseBean declaration.
Well a bit tricky being my first time using this stuff, hope this helps some other people too!
- 04-27-2010, 03:15 PM #4
Member
- Join Date
- Mar 2010
- Posts
- 26
- Rep Power
- 0
Similar Threads
-
Problem with Session Scope while using with Java Bean
By sulthanmytheen in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 03-27-2009, 10:50 AM -
Defining global session scope in Spring framework
By Java Tip in forum Java TipReplies: 0Last Post: 03-31-2008, 10:08 AM -
How to use session scope in Spring
By Java Tip in forum Java TipReplies: 0Last Post: 03-31-2008, 10:07 AM -
Defining global session scope in spring framework
By JavaBean in forum Java TipReplies: 0Last Post: 09-28-2007, 12:47 PM -
How to use session scope in Spring
By JavaBean in forum Java TipReplies: 0Last Post: 09-28-2007, 12:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks