Results 1 to 4 of 4
  1. #1
    Twixly is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default 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.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    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".

  3. #3
    Twixly is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default

    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!

  4. #4
    flyfisherman is offline Member
    Join Date
    Mar 2010
    Posts
    26
    Rep Power
    0

Similar Threads

  1. Problem with Session Scope while using with Java Bean
    By sulthanmytheen in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-27-2009, 10:50 AM
  2. Replies: 0
    Last Post: 03-31-2008, 10:08 AM
  3. How to use session scope in Spring
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-31-2008, 10:07 AM
  4. Replies: 0
    Last Post: 09-28-2007, 12:47 PM
  5. How to use session scope in Spring
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 09-28-2007, 12:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •