#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    5
    Rep Power
    0

    How do I pass reference of a global hashtable to a java bean?


    Please help with this:

    I have A.jsp which has a application scope Hashtable as below
    <jsp:useBean id="user" scope="application"class="java.utilHashtable"/>
    use.put(session.getId(), loginName)



    I have a java bean B.java which is supposed to remove the loginName when session expires.

    C.jsp is supposed to retrieve the hashtable data from B.java and display in the browser for the currently logged on users.


    My question I
    1. how do I pass hashtable object from A.jsp to the bean B.java so that it can manipulate it? Or How do I establish a reference of the Hashtable(A.jsp) in B.java?
    2. How do I retrieve the hashtable from B.java so that it can be displayed in C.jsp?


    Thanks a lot,

    RD
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    Wel, to answer your question, you can retrieve the hashtable from the application the same way you made it. When you do this:
    Code:
    <jsp:useBean id="user" scope="application" class="java.util.Hashtable"/>
    It looks in the application for a variable named "user". If it finds one, it returns it. If it doesn't, it makes a new one, saves it in the application and then returns it.

    So, all you would have to do is put the exact same line in every jsp that you want to access the hashtable from. If a user has already been to A.jsp, then on B.jsp and C.jsp you will be getting the object created previously.

    Having said all that, what you are doing seems a little odd to me. If you want some piece of data deleted when the users sessoin expires, why not put it in the session instead of the application? Are you making something that shows all logged in users who else is logged in also?
    Last edited by Nemi; February 27th, 2003 at 03:48 PM.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    5
    Rep Power
    0
    Nemi, Thanks so much for your reply. As a matter of fact it is B.java not B.jsp. It is the a bean that I have question about.

    I want to a jsp program which displays all CURRENTLy loggied on users. So I want to A.jsp to populate login info basically login names.

    Then I need a bean B.java(NOT jsp) which implements HttpSessionListener to get the login information from A.jsp and process them. If user log out or session expires. the sessionDestroy method will delete that user from the global hashtable.

    Then, I need C.jsp to display the currently logged on users. Sorry I did not make myself clear.

    RD
  6. #4
  7. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    Ah, gotcha. My bad, I did not notice that.

    I am assuming you are storing this bean in the application scope also? How are you storing it in the application? Basically, you need to pass a reference to the hashtable you are storing in the application to B.java.

    So, maybe you could do something like this:
    Code:
    <jsp:useBean id="user" scope="application" class="java.util.Hashtable"/>
    <jsp:useBean id="B" scope="application" class="myPackage.B">
      <setProperty name="B" property="lookupTable" value='<%= user %>'/>
    </jsp:useBean>
    NOTE: I *believe* that when using a script tag for the value of a 
    setProperty tag you have to enclose it in single quotes. I did not test this.
    Your B.java would have to have a private variable called lookupTable and a setter method called setLookupTable(). After that it can access this table as it sees fit.

IMN logo majestic logo threadwatch logo seochat tools logo