Link to home
Start Free TrialLog in
Avatar of rdong
rdong

asked on

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
Avatar of cheekycj
cheekycj
Flag of United States of America image

you can either attach the hashtable object to the request and forward from A.jsp to B.jsp

or you can store the object in Session and retrieve it from there.

a third option is to serialize the object to disk and have B.jsp read it.

CJ
actually since your hashtable object is an application level object all pages should have access to it using:

<jsp:useBean id="user" scope="application"class="java.utilHashtable"/>

right?

CJ

Avatar of rdong
rdong

ASKER

What I want to know is how to access it in a java bean? How do pass the hashtable populated in A.jsp to B.java?

Thanks
if you add :
<jsp:useBean id="user" scope="application"class="java.util.Hashtable"/>
to B.jsp you should have access to your hashtable.

to pass it a JavaBean just create a method that takes a HashTable as a param and then pass the variable user to it.

CJ
ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rdong

ASKER

Thanks a lot for your great help.


Well,  the program I am working on is to  display all currently logged on users by loginName.  Yes am using servlet2.3.


A.jsp (login page)

…..
<jsp:useBean id="user" scope="application"class="java.utilHashtable"/>
user.put(session.getId(), loginName)
…..

B.java
=================================
package xxx,xx,xxx
import java.util.Hashtable;
import javax.servlet.ServletContext;
import javax.servlet.http.*;

import java.util.Hashtable;
import javax.servlet.ServletContext;
import javax.servlet.http.*;

public class sessAdmin implements HttpSessionListener
{  private String loginName;
   Hashtable hashtable=null;
   public void sessionCreated(HttpSessionEvent httpsessionevent)
   { }
   public void sessionDestroyed(HttpSessionEvent httpsessionevent)
   { hashtable=(Hashtable)httpsessionevent.getSession().getServletContext().getAttribute("uer");
     hashtable.remove(httpsessionevent.getSession().getId());
     System.out.println("user "+ hashtable);
   }
}
==================================

C.jsp
<%@page language="java" %>
<%@page import="java.io.*,java.sql.*,java.util.*,javax.servlet.*" %>
<jsp:useBean id="user" scope="application"class="java.util.Hashtable"/>
<%=user%>


I did get a display of user session id and login name in C.jsp. but I could not test logout because whenever I logout. the whole browser closed itself automatically. I do not know why.  Can you take a look at the code  and see what is wrong?

Also if I use the following code in C.jsp, nothing get displayed

<%
for (Enumeration en=user.keys(); en.hasMoreElements();)
  { String name = (String)en.nextElement();
    String value = user.get(name).toString();
    System.out.println(">>>>>> Key : " + name + " Value: " + value);
  }
%>

Why is that?

I am new on this. Please help

RD
>>System.out.println(">>>>>> Key : " + name + " Value: " + value);
prints it out to Stdout (which is usually a file in the servlet engine called stdout.log)

to get it displayed do this:

<%
for (Enumeration en=user.keys(); en.hasMoreElements();) {
   String name = (String)en.nextElement();
   String value = user.get(name).toString();
%>
   Key:<%=name%>,Value:<%=value%><br>
<% } %>

CJ
can you post your logout code so we can try to find out why the browser gets closed.

CJ
Avatar of rdong

ASKER

CJ,

Thanks so much for your help.  I guess it is my mistake. That the the link to logout.jsp is wrong. so I can logout now. However, when I logout., it seems that the sessionDestroy method does not remove the user id form the Hashtable.  Could you take a look at the B.java and see if there is a problem. I am not sure if I have correctly passed the correct Hashtable reference to the bean.
 
I am not sure about this code:
 hashtable=(Hashtable)httpsessionevent.getSession().getServletContext().getAttribute("user");

Can this get the hashtable reference from A.jsp?  

What I really want is  to have the  bean (B.java) 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.

Thanks again for your help.

RD
Not sure but try this:

  public void sessionDestroyed(HttpSessionEvent httpsessionevent) {
   HttpSession session = httpsessionevent.getSession();
   Hashtable hashtable=(Hashtable) session.getAttribute("user");
    hashtable.remove(session.getId());
    //System.out.println("user "+ hashtable);
    session.setAttribute("user", hashtable);
  }

Try using the modified method above.

CJ
>hashtable=(Hashtable)httpsessionevent.getSession().getServletContext().getAttribute("user");
looks ok.

Did you put a <listener> tag into your web.xml ?
Avatar of rdong

ASKER

Thanks so much for all your help. I will keep work on it.

RD
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points to cheekycj

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer