• Post Reply Bookmark Topic Watch Topic
  • New Topic

session's getAttribute method help.  RSS feed

 
Yogendra Joshi
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Ranchers ,

I have a query here for my problem or lets not say problem , rather a anxiety with a mince of excitement for new class(StringBuilder)!

I buyed SCJP 1.5 K & B Book , In that , i learnt a new StringBuilder class which is not thread safe but is faster then StringBuffer and String. Sun too recommends it. Now , As strings are immutable , in our project i.e in JSP , there is a lot of place where String's are used and i want to use StringBuilder in place of String which might be taking a lot of memory on heap.

For testing this , i wrote small JSP(s) through which we add a variable in session with StringBuilder like this



In above , i am taking "user" session variable from other page where i am setting it like this :



Now, receiver.jsp is the page where session.getAttribute is used to print the value of Session Variable.

An attempt to do this slaps me with a error saying :

found : String
required:StringBuilder

But this has already been casted to StringBuilder.

Can someone please shred some light on this ?

Thanks in advance.

Yogendra Joshi.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know 1.5,
but in a=request.getParameter("name"), getParameter returns a String, not a StringBuilder.

Maybe you could try instead :
a=new StringBuilder(request.getParameter("name"));
[ June 06, 2006: Message edited by: Satou kurinosuke ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure that using StringBuilder everywhere instead of String is going to bring you any good anyway. But as I said, I don't know 1.5 ;)
 
Isuru Sampath
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joshi,

If you look at the StringBuilder API you will see that StringBuilder java.lang.StringBuilder does not inherit the java.lang.String class. So, your cast is illegal in the first place.

However you can do it like this:

When you are setting the value:


So when you recall you can:

[ June 06, 2006: Message edited by: Isuru Sampath ]
 
Yogendra Joshi
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends ,

Thanks a lot for your valuable information. I would try it and would come back i have some problem.

Thanks again.

Yogendra Joshi.
 
Paul Clapham
Sheriff
Posts: 22831
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Yogendra Joshi:
In that , i learnt a new StringBuilder class which is not thread safe but is faster then StringBuffer and String.
Somebody told you that "StringBuilder is faster than String"? I think you have mis-read something. So if you're converting your application to store text data in StringBuilder objects rather than String objects, just because of that, then don't. A StringBuilder is used when you are building a String, just as its name suggests. If you have text data, then just store it in a String. Otherwise people reading your code in future will be completely confused.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!