Hi,
> > Is there a method that you can use on a JSP session bean to get a list
> > of all the variables?
> >
> > I tried using session.getValueNames(), but that only gives me the bean
> > name itself (and the associated id as the value). I don't want the
> > variables in the servlet session, I want a list of all the variables
> > (and values) that I've stored in my JSP session bean.
> >
>
> What about something like this:
>
> Enumeration names = session.getValueNames();
> while (names.hasMoreElements()) {
> String name = (String) names.nextElement();
> Object bean = session.getValue(name);
> System.out.println("Session bean '" + name + "' has value '" +
> bean.toString());
> }
>
> (In a 2.2 servlet container, you would replace getValueNames() by
> getAttributeNames() and getValue() by getAttribute() -- these
> names were changed
> for consistency with the other scopes.)
One thing..getValueNames returns a String[], not an Enumeration. In my
opinion, its a little easier to get the String[]..so I miss this in the
Servlet 2.2, which you have to get an Enumeration and do a bit more work
with. No big deal. I would like to know if the Enumeration code, when
compiled is "more" code than the String[] code used. For example:
Servlet 2.1:
String[] names = session.getValueNames();
for( int cntr = 0; cntr < names.length; cntr++ )
System.out.println( names[cntr] );
Servlet 2.2:
Enumeration names = session.getAttributeNames();
while( names.hasMoreElements() )
{
String name = (String) names.nextElement();
System.out.println( name );
}
I would think the Servlet 2.1 produces less code, and thus would be a little
faster..just my opinion on this. If anyone knows for sure..enlighten me. :)
As a side..I would like to add a little "warning" I learned..not sure if it
is a "fact", but Craig can probably correct me.
When you want to "remove" ALL objects in a session, but keep the session
alive, you dont want to do:
session.invalidate();
This will remove all the objects..but it also kills the session. Which means
if you do something like:
session.invalidate();
session.setAttribute("MyObjectName", someObject); // servlet 2.2 way of
doing things
you'll get some sort of problem..probably NPE, but I can't remember.
The trick is..if you want to remove all objects, you would do one of two
things..not sure which is the best approach:
session.invalidate();
request.getSession(true); // create new session
or
Enumeration names = session.getAttributeNames();
while( names.hasMoreElements() )
{
String name = (String) names.nextElement();
session.removeAttribute( name );
}
The reason I know this..is that we store a "LoggedIn" object when a client
logs in. We want this object to stay in the session the entire time they are
logged in. However, we also store other objects.."sessional" logic objects
in memory, that should only last during a transaction across multiple
page/forms. When the last page is reached, we want to remove these objects.
Actually..we have a more specialized case on our site..we have a few "top"
tabs, each being a new login required (dont ask why..I didn't design it this
way). So, when they click one of the top tabs, it "relogs" them in..and we
want ALL objects EXCEPT the "LoggedIn" object to be removed. So in the above
code, I insert:
if( ! name.equalsIgnoreCase( "LoggedIn" ) )
session.removeAttribute( name );
This way..I don't remove the "LoggedIn" object..only everything else. This
is done because as it turns out, the 3 top choices all point to the same one
WWW dir. Thus, we have radio, tv and cable top links. Below them we have the
"area" links for the media type links above. Thus, if they click on Radio,
then click on the tab for Orders, it calls the SAME jsp page that Tv and
Cable call..assuming they are on those tabs. Because of this..we don't want
cross-talk when the "bottom" tab session object is added to the HttpSession.
Man..this is hard to explain..so I'll shut up. But anyways..we have had some
problems that the code above seems to resolve. I could include each top
media type in its own WAR file..but it would use the same WWW 3 times to
separate them..which seems to me a waste of space, using the same WWW in 3
separate files. I don't know for sure though.
What do you all think? Good/bad design?
Thanks.
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets