Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 485
  • Last Modified:

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
0
rdong
Asked:
rdong
  • 6
  • 4
  • 2
  • +1
1 Solution
 
cheekycjCommented:
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
 
cheekycjCommented:
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
 
rdongAuthor Commented:
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!

 
cheekycjCommented:
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
 
rrzCommented:
>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
 
rdongAuthor Commented:
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
 
cheekycjCommented:
>>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
 
cheekycjCommented:
can you post your logout code so we can try to find out why the browser gets closed.

CJ
0
 
rdongAuthor Commented:
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
 
cheekycjCommented:
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
 
rrzCommented:
>hashtable=(Hashtable)httpsessionevent.getSession().getServletContext().getAttribute("user");
looks ok.

Did you put a <listener> tag into your web.xml ?
0
 
rdongAuthor Commented:
Thanks so much for all your help. I will keep work on it.

RD
0
 
girionisCommented:
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now