Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

null is coming...when I set a HashMap in request object?

 
Shashi Kala
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Actually I have two jsps, say first.jsp and second.jsp. In first.jsp, I want to put some HashMap object in request object, by that I can get it in second.jsp. To achieve this, I have used setAttribute() method in first.jsp as shown below..



where as hm is an object of HashMap.

But in second.jsp, if I am trying to get the hm by..



but hm1 is getting assigned by null..why null is coming here..Can anybody help??
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you navigating from the first JSP to the second?

If you are using anything other than RequestDispatcher#forward or RequestDispatcher#include then two jsps will be accessed via different Requests, which means they will have different HttpServletRequest object, and as such you will not be able to share data between the two pages using the request.

You will have to use an alternative location to store the information from one request to the next. The easiest such place is the session scope.
 
Shashi Kala
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steve,

I am not using include or forward to navigate to the second.jsp. Simply I am using form.submit() in java script, and the form tag is as shown below..

As you said, if two jsp's request object are different..But I am able to get the value of hidden parameter param1 in second jsp. Then how the request object of both jsps could be different?
[ August 07, 2008: Message edited by: Shashi Kala ]
 
pooja jain
greenhorn
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are setting in attributes and trying to get it from parameters. got it?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shashi Kala:
As you said, if two jsp's request object are different..But I am able to get the value of hidden parameter param1 in second jsp. Then how the request object of both jsps could be different?
Parameters and scoped variables have nothing whatsoever to do with each other. A scoped variable set into request scoped is only available during that same request. Parameters are submitted from one request to the other. They are completely separate concepts.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sashi kala,


if you want to get data(in second.jsp) from requestScope ,you need to forward request object to that page(second.jsp).

but here you are doing nothing. so in this scenario...
----------------------------------------------------------
as per steve suggession use session instead of request.
-----------------------------------------------------------
Hope This Helps
 
Shashi Kala
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So..the ultimate way is using session..right?

okay.!!

Thanks All.
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:How are you navigating from the first JSP to the second?

If you are using anything other than RequestDispatcher#forward or RequestDispatcher#include then two jsps will be accessed via different Requests, which means they will have different HttpServletRequest object, and as such you will not be able to share data between the two pages using the request.




Sorry for the interruption in middle of the Thread?

I got a doubt on this regarding the explanation from steve .

So steve you mean to say that we can't use response.sendRedirect() to other JSP and access the Map Object in that JSP.

Please clear my doubt
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So steve you mean to say that we can't use response.sendRedirect() to other JSP and access the Map Object in that JSP.


"sendRedirect" means it's a completely different request. Once server responded to a given request the request object is gone (no longer accessible) in the server.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Edit]Geesh, I was replying to the topicstart but I apparently wasted my time because one was so smart to resurrect an old topic. Please start a new topic for every independent problem instead of resurrecting dead cows.[/edit]
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[edit]Answer removed, it was not more applicable.[/edit]
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Vijetha , Bauke.

I understood that this makes a complete new request.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic