• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting Data anywhere By storing in Session

 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I've sample code of jsp and servlets. Below is my code.

Index.html



welcome.jsp



AnnotationServlet.java


Now I want to store those username and age(in .html) somewhere and I want those information in welcome.jsp. Now, I'm storing all those in session itself(In doGet method(servlet)). I think, there may be still better and good way to store. Please can anyone suggest me, with proper explanation.

2 Doubt) Also, when I get the session in servlet line No. 27. The session value is
org.apache.catalina.session.StandardSessionFacade@69a8bf4f
. I'm expecting that session is to be empty. Because, I dint store anything until that line. Please explain me this too.

Thanks
Ramakrishna K.C
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For starters, you should never handle a login via GET - it transfers parameters in the URL, and thus makes the password visible in all kinds of places where it should not be visible. Very bad.

I'm expecting that session is to be empty. Because, I dint store anything until that line.

You're assuming that the toString method shows the contents of the session - it does not. See the javadocs of the HttpSession class to learn about the various methods you can use to examine the session's content.

if ( name != "" && age != "" && name != null && age != null )

Never compare strings using "==" or "!=", use the "equals" method instead. And when you do that, you'll find that that you need to check for null before using equals.
 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For starters, you should never handle a login via GET - it transfers parameters in the URL, and thus makes the password visible in all kinds of places where it should not be visible. Very bad.


Yeah. I got that. Just for quick post I typed dummy prog.
Thanks for your suggestion.

Never compare strings using "==" or "!=", use the "equals" method instead
Yeah. equals method is right. But, != comparison also wrong?

Now I want to store those username and age(in .html) somewhere and I want those information in welcome.jsp. Now, I'm storing all those in session itself(In doGet method(servlet)). I think, there may be still better and good way to store. Please can anyone suggest me, with proper explanation.

This is my main Problem.

Thanks
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, != comparison also wrong?

Yes, it's the same as with "==".

I think, there may be still better and good way to store.

Why do you think that? In which way would you like to improve on it?
 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
like, If I have more and more parameters.. then I can't store all those in session right? I mean, its not good right?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer, as always, is: it depends. 10 attributes per user of 100 bytes each with 1000 concurrent sessions - no problem. 100 attributes per user of 1000 bytes each with 1000 concurrent session - maybe a problem, but probably not (code that deals with 100 session attributes per user is likely badly designed, though).
 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so.. No need to worry? This code is right? I've to store all attributes in session itself, causes no problem?

 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You missed my point: it depends on the quantity of data per user, and the number of concurrent users. (And, of course, on the amount of memory allocated to the JVM.) Having two simple attributes per user -like in the code you showed- should be fine.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another question to consider:

How long will this data be needed in your application?

If only for a single session then that is exactly what HttpSession is for. All data will be "forgotten" when the session times out after the user is finished.

If for an extended relationship then you obviously need a long term storage solution.

Bill
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!