getAttribute returns NULL

Reply

Join Date: Oct 2008
Posts: 1
Reputation: itzzmeyoyo is an unknown quantity at this point 
Solved Threads: 0
itzzmeyoyo itzzmeyoyo is offline Offline
Newbie Poster

getAttribute returns NULL

 
0
  #1
Oct 20th, 2008
Iam not able to pass a value from servlet to jsp using setAttribute and getAttribute.

My test code :

Test1.jsp
=======
  1. <%@ page language="java" contentType="text/html" %>
  2. <html>
  3. <head><title>Login Example</title></head>
  4. <body>
  5. Enter Your Text
  6. <hr><p>
  7. <form name = login action="../servlet/TestServlet1" method=POST>
  8. Your Text : <input type=text name=yourText><br>
  9. <input type=submit value=LogIn>
  10. </form>
  11. </body>
  12. </html>

TestServlet1.java
============
  1. import java.io.*;
  2. import javax.servlet.*;
  3. import javax.servlet.http.*;
  4.  
  5. public class TestServlet1 extends HttpServlet {
  6.  
  7. public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
  8. doPost(request, response);
  9. }
  10.  
  11. public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
  12.  
  13. // initalize some variables
  14. HttpSession session = request.getSession(true);
  15. String userText = request.getParameter("yourText");
  16. session.setAttribute("myText", userText);
  17. RequestDispatcher rd = request.getRequestDispatcher("/../TEST/Test2.jsp");
  18. rd.forward(request, response);
  19. }
  20. }
  21. }

Test2.jsp
=======
  1. <%@ page language="java" contentType="text/html" %>
  2. <% String up = (String)session.getAttribute("myText"); %>
  3.  
  4. <html>
  5. <head><title>Login Example</title></head>
  6. <body>
  7. <% if (up == null)
  8. { %>
  9. its null again
  10. <% } else {
  11. %>
  12. <%= up %> is my text!<br>
  13. <% } %>
  14. </body>
  15. </html>

All the time iam getting null from getAttribute.
  1. <% String up = (String)session.getAttribute("myText"); %>

When i printed the sessionId in the servlet and both jsp (Test1,Test2), it showed thw same session id
the session id is same but the attributes which i set is not there...

code for printing session id
===================
  1. String id = session.getId();
  2. System.out.println(id);

can anyone help me in this ?
Thanks in advance
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,620
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: getAttribute returns NULL

 
0
  #2
Oct 22nd, 2008
Since you are using a forward, there is no need to use session variables. The HttpServletRequest is preserved when forwarding.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 9
Reputation: arunmagar is an unknown quantity at this point 
Solved Threads: 0
arunmagar arunmagar is offline Offline
Newbie Poster

Re: getAttribute returns NULL

 
0
  #3
Oct 26th, 2008
instead of this
<% String up = (String)session.getAttribute("myText"); %>


try this
<% String up = session.getAttribute("myText").toString(); %>


The session attribute is of object datatype.so we have to convert it to string .
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,620
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: getAttribute returns NULL

 
0
  #4
Oct 26th, 2008
> try this <% String up = session.getAttribute("myText").toString(); %> .

...which would anyways throw a NullPointerException in this case.

> The session attribute is of object datatype.so we have to convert it to string .

What? The ` session ' variable is of the type ` HttpSession '.
Last edited by ~s.o.s~; Oct 26th, 2008 at 10:11 am.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC