This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Session object doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Session object doubt" Watch "Session object doubt" New topic
Author

Session object doubt

Nandini Sriram
Ranch Hand

Joined: Jul 04, 2003
Posts: 132
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


Discipline, Dedication and Determination define Destiny.
Kunaal A Trehan
Ranch Hand

Joined: Feb 04, 2004
Posts: 63
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

Joined: Jan 19, 2004
Posts: 22
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

Joined: Jul 04, 2003
Posts: 132
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

Joined: Feb 04, 2004
Posts: 63
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

Joined: Jul 04, 2003
Posts: 132
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

Joined: Apr 28, 2003
Posts: 6
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.


wawi
Moorthi Ramasamy
Greenhorn

Joined: Jan 02, 2004
Posts: 15
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

Joined: Jul 04, 2003
Posts: 132
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

Joined: Jan 02, 2004
Posts: 15
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

Joined: Jul 04, 2003
Posts: 132
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

Joined: Feb 08, 2004
Posts: 19
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 ]

SCJP 1.4, SCWCD, SCBCD,<br />SCDJWS (preparing)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Session object doubt