This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSP and the fly likes Maintain a variable across jsp pages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Maintain a variable across jsp pages" Watch "Maintain a variable across jsp pages" New topic
Author

Maintain a variable across jsp pages

Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

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.

SCJP, SCWCD.
|Asking Good Questions|
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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.


~ Mansukh
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

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

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Apr 28, 2013
Posts: 38
I was able to use String name = (String) session.getAttribute( "userid" ); to make it work. :)
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Apr 28, 2013
Posts: 38
The object needed to be converted to a compatible type to use the data.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Apr 28, 2013
Posts: 38
The problem is solved thank you for the help.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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.
 
jQuery in Action, 2nd edition
 
subject: Maintain a variable across jsp pages
 
Similar Threads
unable to do validation for select in struts2
How to include a part of html code in to a jsp page?
inputCalendar - absolute uri problem
JDBC connection for DB2 database
choosing eclipse