Hi,
I have a problem related to Java servlet sessions. I don't understand why the getAttribute() function of the session object is used before setAttribute(); Here is the code:

Vector buylist=(Vector)session.getAttribute("Register");
if (action.equals("del")) {
        String del = request.getParameter("deli");
        int d = (new Integer(del)).intValue();
        buylist.removeElementAt(d);
      }
session.setAttribute("Register", buylist);

Thanks.

Recommended Answers

All 2 Replies

Probably doesn't want to create a new one if one already exists. But, in that case it has still been done wrongly, and there is no reason to have to place it back into the session, unless it is null after the get call, in which case it should be created an immediately placed into the session attritube. i.e.

ArrayList<String> bogus = session.getAttribute("bogus");
if (bogus == null) {
  bogus = new ArrayList<String>();
  session.putAttribute("bogus", bogus);
}
// do whatever you want with bogus except redefining it
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.