?
Solved

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

Posted on 2003-02-27
13
Medium Priority
?
481 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:rdong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 19

Expert Comment

by:cheekycj
ID: 8035036
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
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8035061
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

0
 

Author Comment

by:rdong
ID: 8035138
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:cheekycj
ID: 8035251
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
0
 
LVL 28

Accepted Solution

by:
rrz earned 400 total points
ID: 8035266
>I have a java bean B.java which is supposed to remove the loginName when session expires.
Are you using Servlet 2.3 API ?

If so, then let your bean implement javax.servlet.http.HttpSessionListener

>1. how do I pass hashtable object from A.jsp to the bean B.java
If you use listener that won't be necessary.
Look at this for example.
http://www.experts-exchange.com/Web/Web_Languages/JSP/Q_20514811.html 

If you need help, just let us know here.
0
 

Author Comment

by:rdong
ID: 8036627
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
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8037252
>>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
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8037255
can you post your logout code so we can try to find out why the browser gets closed.

CJ
0
 

Author Comment

by:rdong
ID: 8037490
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
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8037604
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
0
 
LVL 28

Expert Comment

by:rrz
ID: 8037638
>hashtable=(Hashtable)httpsessionevent.getSession().getServletContext().getAttribute("user");
looks ok.

Did you put a <listener> tag into your web.xml ?
0
 

Author Comment

by:rdong
ID: 8050415
Thanks so much for all your help. I will keep work on it.

RD
0
 
LVL 35

Expert Comment

by:girionis
ID: 9990321
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
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Learn how to use the free Acronis True Image app to easily transfer data between iPhones and Android phones.
Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month13 days, 22 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question