Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Session object doubt

 
Nandini Sriram
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am new to JSP. I am trying to understand the "session object".
Assume there's an HTML file containing a text field for name, File1.jsp, and File2.jsp.


Here I am getting the output from File2.jsp as Hello, null. Why?
Regards
Nandini
 
Kunaal A Trehan
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
The reason is that in File1.jsp,session attribute is set to false.
Because of which even though u have added an attribute in File1.jsp.
Its not available in File2.jsp
Kunal
 
sivakumar ganti
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In File1.jsp you are trying to get a input parameter and trying to set in the session as an attribute.
<% String name = request.getParameter( "username" );
session.setAttribute( "theName", name );
%>
But from where does the File1.jsp gets the I/P so it gives a null in the output file ie File2.jsp.

Try to give some input parameter this way
http://localhost:8080/File1.jsp?username=abcde

siva
 
Nandini Sriram
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kunal and Siva,
Thanks for your response. But I have not set the session attribute to "false" in my code. And I get the input for File1.jsp from "GetName.html" where the text field's name is "username". I am able to retrieve the correct output from File1.jsp but not from File2.jsp.
Regards
Nandini
 
Kunaal A Trehan
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I just pasted the code for File1 and File 2
and given this url
http://localhost:8080/dummy/File1.jsp?username=kunal
It clearly shows Hello,kunal
Try it and let me know
Kunal
 
Nandini Sriram
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kunal,
Thanks. If I pass the parameters in the way you and Siva said, it works. But if I try to get the parameter values from an HTML file, will it not work? Am I making any mistake here?
Regards
Nandini
 
Will St Hubert
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can you put your code for the HTML file b/c if you can access that value from File1.jsp and set it as an attribute in the session you should be able to retrieve it in File2.jsp.
 
Moorthi Ramasamy
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nandini Sriram:

But if I try to get the parameter values from an HTML file, will it not work? Am I making any mistake here?


<form method="POST" action="File1.jsp">
<input type="text" name="username" >
<input type="submit" name="submit">
]

Make sure that your html file looks like the above code snippet.It will work.
Thanks,
Moorthi
[ February 16, 2004: Message edited by: Moorthi Ramasamy ]
[ February 16, 2004: Message edited by: Moorthi Ramasamy ]
 
Nandini Sriram
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Moorthi and Will,
I have the following code for the HTML which is similar to yours.

In File1.jsp, the values are retrieved, but when it comes to File2.jsp, it shows 'null' (the session attribute is NOT set to 'false').
Will look into it again.
Regards
Nandini
 
Moorthi Ramasamy
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nandini Sriram:

In File1.jsp, the values are retrieved, but when it comes to File2.jsp, it shows 'null' (the session attribute is NOT set to 'false').
Will look into it again.

Add the following line in File2.jsp. If you see "true", check your browser whether cookie is enabled or not.

Regardless of "true/false" in the above check, add the following line in the File1.jsp. Everything should be fine.

Thanks,
Moorthi.
 
Nandini Sriram
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moorthi,
Thanks for your response. I tried including those lines. But the result was the same.
However, I tried including "toString()" to request.getAttribute("username").toString() in File1.jsp and I was able to see the correct output "Hello, Nandini" in File2.jsp.
Regards
Nandini
 
Paul Yen
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nandini,
I think the reason might be when you try to use session.getAttribute( "theName" )to display the value. You have to cast it to a String, because seesion.getAttribute returns a Object not a String. Even the toString() inside the object will not return a value for the object, it will only return a reference to it which is null. That's why you get a null value. Try to do this (String)session.getAttribute. It will work. If I am wrong, please let me know. By the way, the default value of session is set to true for JSP page.
Regards,
Paul
[ February 19, 2004: Message edited by: Paul Yen ]
[ February 19, 2004: Message edited by: Paul Yen ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic