views:

46

answers:

2

The question is self explanatory I guess. This is what I am doing:

Student student = Student.findStudent(s.getRegNumber(), (ClientSession)httpSesn);

findStudent() method returns me an object of the Student class. As you can see I am casting HttpSession object into a ClientSession. Is it advisable? Are their any hidden snags involved? What are the best practices?

+1  A: 

So I am assuming you are running this code in an app server and it is a line from a servlet. Since HttpSession does not necessarily inherit from or is implemented by ClientSession in the Java EE spec (unless I missed something fundamental), the code will likely blow up when you run it on a different server (Say Glassfish or Tomcat or JBoss to name a few).

Additionally, if you try to Mock out objects for testing, the Mocks will implement the HttpSession interface and not the ClientSession interface. This will give you a ClassCastException.

Summers Pittman
A: 

I don't think there is a thing called ClientSession. Is this the class that you designed? But the question is why to cast the httpSesn into ClientSession? Why not to pass httpSesn directly into method and use getAttribute?

RaviG