Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Maintain a variable across jsp pages

 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:See, you just gave him the fish. Let's teach him how to fish. :wink:

I did not. I told where to look for the exception. In your terms I told where to look for the fish.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit Ghorpade wrote:
matthew irving wrote:The return type is object and the object is the variable type like session or scope?

The return type for session.getAttribute() is Object. You need to type cast it to the appropriate type which was used while session.setAttribute(). In your case this is String.


Are you sure about that Amit? What would happen if, say, you tried running the following code:



Do you think this would run without any exceptions? Are you sure that a base type can be cast into a subtype? If yes why, if not, why not? Read Ernest's reply in this thread.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:Are you sure about that Amit?
Are you sure that a base type can be cast into a subtype?

You are missing the important part of my post above, it clearly says that the type cast should be done to the object class used while setting the attribute.
Did I say set an Object and cast it to String during get?
When in doubt of the incoming type, I would not cast without the instanceof check.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I still follow a thumb rule that one must do the instanceof check irrespective of the degree that one is sure what type of Object it is, just to be on the safer side.
 
matthew irving
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was able to use String name = (String) session.getAttribute( "userid" ); to make it work. :)
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
matthew irving wrote:I was able to use String name = (String) session.getAttribute( "userid" ); to make it work. :)


Good. But did you understand why you had to do that? Why was casting required?
 
matthew irving
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The object needed to be converted to a compatible type to use the data.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64688
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:But I still follow a thumb rule that one must do the instanceof check irrespective of the degree that one is sure what type of Object it is, just to be on the safer side.

I'd say that "it depends". Doing that sort of check everywhere can make the code bloated and harder to read. If the code is under close control, and it's reasonably known that a scoped attribute will be a String, then the check could be superfluous.

This is especially true when it's a bug if the value is anything other than the expected type. Just let the ClassCastException happen and bubble its way out to the error handler where the developer can clearly see that there is a problem in the code that needs fixing.

But sure, when the type cannot be known, or there is something concrete to do locally if the type is incorrect, then do the check.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed Bear. More of a design question rather than a technical one. Ideally, one should design his application schema model in a manner where it is clearly outlined what the class hierarchies are. Minimal instanceof checks are the sign that your design is good. Although certain methods , like the famous java.lang.Object.equals(Object object), impose that restriction on the developer just because of its signature. you have to know what type that passed object is before comparing it with another one.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
matthew irving wrote:The object needed to be converted to a compatible type to use the data.


Hmm. So is your problem solved now? Or are you stuck somewhere else?
 
matthew irving
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is solved thank you for the help.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you really mean that, will you do one thing Matt? Learn a little bit of basic java everyday. Study from Head First Java. Once your basics are solid, you will be able to resolve such issues without our help. Wouldn't that be great? :wink: You are always welcome anytime Matt. :beerchug: Make notes and come back with as many doubts as possible.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic