Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generics in servlets(sessions)

 
Scott Sorensen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
firstly im not really sure shoud this be in the begginers section or not but here goes..im in college doing this stuff and spotted something in my notes,basically there was some code for using sessions in a servlet..this is the code

HttpSession session = request.getSession(true);
ShoppingCart cart = (ShoppingCart)session.getAttribute("shoppingCart");
if(cart == null)
{ //no cart already in session
cart = new ShoppingCart();
session.setAttribute("shoppingCart", cart);
}
doSomethingWith(cart); //psuedo code

i take it that in this example,there is an object called ShoppingCart that has its own attributes? also if that is the case does this mean that generics could be used here to ensure that what is cast to the object is of type ShoppingCart..ie... ShoppingCart<ShoppingCart> cart = session.getAttribute("shoppingCart") or do i have them all mixed up in my head?

any help would be great as i would love to get this sorted in my head
regards
scott sorensen
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Scott,

Welcome to JavaRanch!

"ShoppingCart" is indeed a class with (presumably) its own attributes here. Unfortunately, this isn't a case where generics could be used to remove the cast. The method "getAttribute" returns different types depending only on its String argument; it's not possible to use generics to overload a method based on the contents of a String. Generics involve only data types; since the argument to getAttribute is always a String, it can't return different types based on its argument.

Finally, note that you as a client of the "getAttribute" method, can't do anything to make it generic; only the author of that method could do that -- and the javax.servlet classes are not "genericized" in any way at this time.
 
Scott Sorensen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cheers for that
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Scott Sorensen:

i take it that in this example,there is an object called ShoppingCart that has its own attributes? also if that is the case does this mean that generics could be used here to ensure that what is cast to the object is of type ShoppingCart..ie... ShoppingCart<ShoppingCart> cart = session.getAttribute("shoppingCart") or do i have them all mixed up in my head?

There appears to be a *class* named ShoppingCart. As for generics, take a look at the API. HttpSession's getAttribute method has a return type of Object -- you need to cast there and generics won't help. If you are worried about the cast failing you can always test with instanceof first.

And as for generics, I think you are confused about them, since "ShoppingCart<ShoppingCart>" doesn't make must sense.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic