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.utilH ashtable"/ >
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
I have A.jsp which has a application scope Hashtable as below
<jsp:useBean id="user" scope="application"class="
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
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.utilH ashtable"/ >
right?
CJ
<jsp:useBean id="user" scope="application"class="
right?
CJ
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
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
<jsp:useBean id="user" scope="application"class="
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.utilH ashtable"/ >
user.put(session.getId(), loginName)
…..
B.java
========================== =======
package xxx,xx,xxx
import java.util.Hashtable;
import javax.servlet.ServletConte xt;
import javax.servlet.http.*;
import java.util.Hashtable;
import javax.servlet.ServletConte xt;
import javax.servlet.http.*;
public class sessAdmin implements HttpSessionListener
{ private String loginName;
Hashtable hashtable=null;
public void sessionCreated(HttpSession Event httpsessionevent)
{ }
public void sessionDestroyed(HttpSessi onEvent httpsessionevent)
{ hashtable=(Hashtable)https essioneven t.getSessi on().getSe rvletConte xt().getAt tribute("u er");
hashtable.remove(httpsessi onevent.ge tSession() .getId());
System.out.println("user "+ hashtable);
}
}
========================== ========
C.jsp
<%@page language="java" %>
<%@page import="java.io.*,java.sql .*,java.ut il.*,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
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="
user.put(session.getId(), loginName)
…..
B.java
==========================
package xxx,xx,xxx
import java.util.Hashtable;
import javax.servlet.ServletConte
import javax.servlet.http.*;
import java.util.Hashtable;
import javax.servlet.ServletConte
import javax.servlet.http.*;
public class sessAdmin implements HttpSessionListener
{ private String loginName;
Hashtable hashtable=null;
public void sessionCreated(HttpSession
{ }
public void sessionDestroyed(HttpSessi
{ hashtable=(Hashtable)https
hashtable.remove(httpsessi
System.out.println("user "+ hashtable);
}
}
==========================
C.jsp
<%@page language="java" %>
<%@page import="java.io.*,java.sql
<jsp:useBean id="user" scope="application"class="
<%=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(">>>>>>
}
%>
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:<%=val ue%><br>
<% } %>
CJ
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:<%=val
<% } %>
CJ
can you post your logout code so we can try to find out why the browser gets closed.
CJ
CJ
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)https essioneven t.getSessi on().getSe rvletConte xt().getAt tribute("u ser");
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
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)https
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(HttpSessi onEvent httpsessionevent) {
HttpSession session = httpsessionevent.getSessio n();
Hashtable hashtable=(Hashtable) session.getAttribute("user ");
hashtable.remove(session.g etId());
//System.out.println("user "+ hashtable);
session.setAttribute("user ", hashtable);
}
Try using the modified method above.
CJ
public void sessionDestroyed(HttpSessi
HttpSession session = httpsessionevent.getSessio
Hashtable hashtable=(Hashtable) session.getAttribute("user
hashtable.remove(session.g
//System.out.println("user
session.setAttribute("user
}
Try using the modified method above.
CJ
>hashtable=(Hashtable)http sessioneve nt.getSess ion().getS ervletCont ext().getA ttribute(" user");
looks ok.
Did you put a <listener> tag into your web.xml ?
looks ok.
Did you put a <listener> tag into your web.xml ?
ASKER
Thanks so much for all your help. I will keep work on it.
RD
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
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
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