File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes generics in servlets(sessions) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "generics in servlets(sessions)" Watch "generics in servlets(sessions)" New topic
Author

generics in servlets(sessions)

Scott Sorensen
Greenhorn

Joined: Mar 02, 2006
Posts: 3
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Scott Sorensen
Greenhorn

Joined: Mar 02, 2006
Posts: 3
cheers for that
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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.


There is no emoticon for what I am feeling!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: generics in servlets(sessions)