aspose file tools*
The moose likes Servlets and the fly likes Session.removeAttribute doesnt work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Session.removeAttribute doesnt work" Watch "Session.removeAttribute doesnt work" New topic
Author

Session.removeAttribute doesnt work

Paul McKenna
Ugly Redneck
Ranch Hand

Joined: Jul 08, 2000
Posts: 1006
Hello,
I'm using Websphere 5.0 Studio and I have a couple of attributes in my session object. At one point of time I remove some of them by using
request.getSession().removeAttribute("myAttribute")
However the values is still available elsewhere in the application. Any help on this?
Thanks
SR


Commentary From the Sidelines of history
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
I can't think of any reason why that would fail to work - I suggest:
1. break that statement up into separate steps
get the session
use isNew() and getAttribute() to see if the session is not new and if it does indeed have the attribute you expect.
2. use getId() in both places to see if both are using the same session.
Bill
Rohit Ahuja
Ranch Hand

Joined: Oct 04, 2001
Posts: 121
Hi,
okie, if i face such a problem which in the past i have, on tomcat, i had a problem with set and get Attribute methods, now here u have a problem with removeAttribute.
There r 2 work arounds.
1.Use putValue,getValue and removeValue methods of HttpSession, although deprecated they work well and serve our purpose.
2.Option number 2 and definatelt wud work if setAttribute is working in ur case is httpsession.setAttribute("XYZ",null), where XYZ would be the name of variable u wish to delete from ur HttpSession.
Man, prefer going by putValue,getValue and removeValue methods.
Tell me, whether u could crack it.If u have'nt i will think of some more new tricks.


Face Off.
Paul McKenna
Ugly Redneck
Ranch Hand

Joined: Jul 08, 2000
Posts: 1006
Hi..
Here is my code:
Servlet Code
------------
HttpSession session = request.getSession(false);

if (null != session) {
System.out.println("Session id : newtradeaction : "+session.getId());
if (null != session.getAttribute("newTradeForm")) {
session.setAttribute("newTradeForm", null);
session.removeAttribute("newTradeForm");
}
}
JSP Code
--------
<jsp:useBean class="com.gec.ctb.form.NewTradeForm" id="newTradeForm" scope="session"></jsp:useBean>
<%
System.out.println(newTradeForm.getId());
%>
And the damn attribute property is still there. The 2 session ids are the same. Cant understand it.
I used debug mode in IBM Websphere Studio and saw that even though I use the removeAttribute method it doesnt actually remove the value but removes something called "next"
Rohit Ahuja
Ranch Hand

Joined: Oct 04, 2001
Posts: 121
HttpSession Object is different from bean instances variable.
mark mathis
Greenhorn

Joined: Mar 14, 2002
Posts: 13
Careful with the <jsp:useBean session=true> tag. If the bean doesn't exist in the session(if you removed it), it will create a new one when the page with the <jsp:useBean session=true> tag in it is served up.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
I sure would look at the actual Java servlet code that JSP generates to see if it is doing what you think it should be doing. In Tomcat it is under the /work directory but I dunno where Websphere keeps it.
Bill
Geeta Ramasami
Ranch Hand

Joined: Mar 05, 2003
Posts: 72
Hi,
JSP useBean tag would create a new session if it does not exist already..So to check if the attribute persist in the session don;t use usebean tag.
use
<%@page session = true%>
try session.getAttribute("newTradeForm");
check if this is null.

Geeta
 
jQuery in Action, 2nd edition
 
subject: Session.removeAttribute doesnt work