I cant replace attributes in sessions

Hi! :)

I need to replace an attribute value in a session, but it seems to become 2 attributes instead.

I need this method to replace or remove a value:

publicvoid changeItemCount(HttpServletRequest request, String nr, String id){

try{

int itemCount = Integer.valueOf(nr).intValue();

if(itemCount == 0){

session.removeAttribute(id);

}else{

session.removeAttribute(id);

session.setAttribute(id, itemCount);

}

}catch (NumberFormatException e){}

}

But removeAttribute does not remove it, and setAttribute instead insersts one more with the same id :(

If i call the method like this: changeItemCount(request, "2", "item1")

and, print all attributes i get:

item:nr:

item11

item11

I want:

item:nr:

item12

Here is all the code (cart.jsp)

<%@ include file="head.jsp" %>

<script language="javascript">

function getInfo(inputId){

var antal = document.getElementById(inputId).value;

window.location ="cart.jsp?update=" + inputId +"&itemNr=" + antal;

}

</script>

<%!

ProductAccessor accessor = ProductAccessorSimulator.Factory.getInstance();

HttpSession session;

%>

<%!

//

publicint getItemCount(HttpServletRequest request, String id){

int itemCount = 0;

try{

itemCount = Integer.valueOf((String)session.getAttribute(id)).intValue();

}catch (NumberFormatException err){}

catch (NullPointerException err){}

catch (ClassCastException err){}

return itemCount;

}

//

publicvoid insertId (HttpServletRequest request, String id){

session = request.getSession();

if(session.getAttribute(id) !=null){

int itemCount = Integer.valueOf((String)session.getAttribute(id)).intValue();

session.removeAttribute(id);// ta bort

session.setAttribute(id,Integer.toString(itemCount+1));

}else{

session.setAttribute(id,"1");

}

}

//

publicvoid changeItemCount(HttpServletRequest request, String nr, String id){

try{

int itemCount = Integer.valueOf(nr).intValue();

if(itemCount == 0){

session.removeAttribute(id);

}else{

session.removeAttribute(id);

session.setAttribute(id, itemCount);

}

}catch (NumberFormatException e){}

}

%>

<%

if(request.getParameter("update") !=null && request.getParameter("itemNr") !=null){

String nr = request.getParameter("itemNr");

String id = request.getParameter("update");

changeItemCount(request, nr, id);

response.sendRedirect("cart.jsp");

}

if(request.getParameter("id") !=null){

String idt = request.getParameter("id");

insertId(request, idt);

response.sendRedirect("cart.jsp");

}

out.println("<table>");

out.println("<tr>");

out.println("<th>Item</th>");

out.println("<th>Nr</th>");

out.println("<th>Refresh</th>");

out.println("<th>Rempve</th>");

out.println("</tr>");

for(Enumeration e = session.getAttributeNames(); e.hasMoreElements() ;){

String str = (String)e.nextElement();

int items = getItemCount(request, str);

try{

Id id = Id.parse(str);

Product p = accessor.getProduct(id);

str = p.getName();

}catch (Exception err){

}

out.println("<tr>");

out.println("<td>" + str +"</td>");

out.println("<td><input id="+str+" type='text' value='"+ items +"' size='1' name='nr'/></td>");

out.print("<td><button onclick=");

out.print("getInfo(");

out.print("'"+str+"'");

out.println(");>Refresh</button></td>");

out.println("<td><button>Remove</button></td>");

out.println("</tr>");

}

out.println("</table>");

%>

<%@ include file="foot.jsp" %>

[7548 byte] By [willea] at [2007-11-27 10:47:45]
# 1

I suspect the problem is in the String variable 'id' as follows:

When you call session.removeAttribute(id), the value is dereferenced in the session object's internall collection of items, but remains attached to the reference variable 'id' (ie, 'id' still points at the memory address). Then when you call session.setAttribute(id, itemCount), the collection is reattached to the same memory address via 'id' and somehow the collection doesn't like that. Normally (for all other users of removeAttribute, setAttribute), when you call removeAttribute(id), all references to object that id points to are removed when the function returns to the caller (the id reference is dereference and at that time, and the item is actually removed from the collection). I suggest trying this:

FROM:

int itemCount = Integer.valueOf(nr).intValue();

if(itemCount == 0) {

session.removeAttribute(id);

} else {

session.removeAttribute(id);

session.setAttribute(id, itemCount);

}

TO:

int itemCount = Integer.valueOf(nr).intValue();

if(itemCount == 0) {

session.removeAttribute(id);

} else {

session.removeAttribute(id);

String idNew=id;

id=null;

session.setAttribute(idNew, itemCount);

}

The above assigns the value of id to idNew (its a string copy to a new address space, not simply a reference assignment). Now id is free to be set to null (dereferenced).

Last point: its not such a good idea to use id (1, 2, 3, etc) for a name as an attibute. They are not unique names. Some other part of the program may use those names and overwrite them by mistake. I suggest prepending a unique name to the front of them such as personItem1, personItem2.

George123a at 2007-7-28 22:02:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...