This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSP and the fly likes null is coming...when I set a HashMap in request object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "null is coming...when I set a HashMap in request object?" Watch "null is coming...when I set a HashMap in request object?" New topic
Author

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

Shashi Kala
Ranch Hand

Joined: Jan 27, 2008
Posts: 46
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

Joined: Jan 28, 2003
Posts: 4164
    
  21

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.


Steve
Shashi Kala
Ranch Hand

Joined: Jan 27, 2008
Posts: 46
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

Joined: Jan 12, 2005
Posts: 213
you are setting in attributes and trying to get it from parameters. got it?


:d
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60738
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jan 27, 2008
Posts: 46
So..the ultimate way is using session..right?

okay.!!

Thanks All.
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
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


If you want something you never had do something which you had never done
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3816

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.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
[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

Joined: Oct 08, 2006
Posts: 2458
[edit]Answer removed, it was not more applicable.[/edit]
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
Thanks Vijetha , Bauke.

I understood that this makes a complete new request.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: null is coming...when I set a HashMap in request object?
 
Similar Threads
Can i use HashMap in logic:iterate tag?
getting the URL for the calling jsp page?
Struts 2 request pararmeter problme
error "Status 404" on redirect
Cannot set attribute in request in jsp