aspose file tools*
The moose likes JSP and the fly likes request.setAttribute() and request.getAttribute() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "request.setAttribute() and request.getAttribute()" Watch "request.setAttribute() and request.getAttribute()" New topic
Author

request.setAttribute() and request.getAttribute()

Dinesh Remje
Ranch Hand

Joined: Jan 26, 2010
Posts: 57
Following is the code i have written in A.jsp page


Following is the code i have written in B.jsp page


Now i want to print the values that are selected in A.jsp page through checkbox in B.jsp page but whenever i m trying to do this as above it gives me an exception in B.jsp page.
Now in B.jsp page i am trying to print a single value but then too it shows me some exception.

So anybody can please point me out that where i am wrong.
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 507
Hi,
It might be helpful if you post the exception too.

Regds,
Amit
Dinesh Remje
Ranch Hand

Joined: Jan 26, 2010
Posts: 57


this is the exception
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18135
    
    8

You didn't say anything about the relationship between the two JSPs. My guess is that they aren't in the same request and so setting a request attribute for A's request has no effect on the attributes of B's request.
Dinesh Remje
Ranch Hand

Joined: Jan 26, 2010
Posts: 57


there is form action thrgh which i made to frwd to B.jsp
Dinesh Remje
Ranch Hand

Joined: Jan 26, 2010
Posts: 57
Will the request.getParameter("id") will help me.
Dinesh Remje
Ranch Hand

Joined: Jan 26, 2010
Posts: 57
@Paul Clapham So what changes shoould i make in my code inorder to make it working.
Also request.getParameter is working but its only printing one value. I need the values that the user select through the checkbox.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Dinesh Remje wrote:@Paul Clapham So what changes shoould i make in my code inorder to make it working.
Also request.getParameter is working but its only printing one value. I need the values that the user select through the checkbox.


What Paul Clapham was asking was the relationship between A.jsp and B.jsp. Other way, how do you invoke B.jsp ? Is it from A.jsp? or you invoke it in the browser directly?

If you invoke direclty, then there is no relation between those two jsp pages.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Dinesh Remje
Ranch Hand

Joined: Jan 26, 2010
Posts: 57
I invoke it from A.jsp. I have given a submit button with the help of which after pressing the button there is a form action which frwd to B.jsp
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Then that should be fine.

Looks like you are directly printing the values without checking whether headname is having a valid value (and NOT null).



If for some case, the headname is null you would get the NPE. Yes, that is what you are getting.

Did you ensure that you get the selected options in A.jsp? What are you geting in A.jsp through your out.println?
Dinesh Remje
Ranch Hand

Joined: Jan 26, 2010
Posts: 57
out.println is used in A.jsp only to know whether i am able to get the values or not when a particular checkbox is selected but for it i have to disable the form action. Then only i am able the see the id which i have selected thrgh checkbox
Dinesh Remje
Ranch Hand

Joined: Jan 26, 2010
Posts: 57

Actually the above statement is not working. the headname is not taking any value.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2712
    
    5

Dinesh Remje wrote:
Actually the above statement is not working. the headname is not taking any value.


That is because there is no such attribute in the request!
You have to forward the original request object to B.jsp.

Hope this helps


SCJP, SCWCD.
|Asking Good Questions|
Ajin Nair
Greenhorn

Joined: Aug 28, 2010
Posts: 4

Hi Dinesh,
Amit is right I am just giving the quick solution here.
here we go,
The following line of code in A.jsp



is of no use as String select[] = request.getParameterValues("id");
will always return null.
replace the code you have in B.jsp with this and it will work only remove the
I hope this helps

if you figured this out already then good.


Thanks,
Ajin
Dinesh Remje
Ranch Hand

Joined: Jan 26, 2010
Posts: 57
Thanks Amit actually i accidentally figured it out. But anyway thanks for your help. But can you tell me how those values get transfered in B.jsp page i mean how the value of id gets transfered to B.jsp.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Hi Dinesh,

Just to summarize,

You are actually trying to pass few values from one page to another (say, a.jsp to b.jsp).

In your a.jsp itself, you are doing the following.

1. Obtaining the list of values from the request object with the key "id" and stuff them into an array of Strings (select[]).
2. Setting / Binding the gathered values (select[]) into the request object with the key "idvalues".

Next in b.jsp, you are trying to obtain the same set of values through the same key "idvalues" to which you had bound the request values.

The mistake comes in your a.jsp itself. Wherein, you are trying to fetch the passed values through the key "id" from the request object. Ideally there is no request object existing at that point of time (when the page is getting executed/rendered) until you submit it! Hence, the return value what you get out of "id" is null. So nothing would be accumulated into "select[]". You are indeed binding the null value to "idvalues" and it gives the same null in b.jsp.

That's why you get a NPE (Null Pointer Exception).

As the previous poster said, you can very well avoid the manual transformation from "id" to "idvalues" in a.jsp. Rather you obtain the same set of values in b.jsp with the same key for the checkboxes that is "id". It will work fine.

Hope this explanation helps!
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Dinesh Remje wrote:Thanks Amit actually i accidentally figured it out. But anyway thanks for your help. But can you tell me how those values get transfered in B.jsp page i mean how the value of id gets transfered to B.jsp.



That is the magic of your request object! That's where the object comes in picture!!

You are actually forwarding a request from a.jsp to b.jsp (through your form action tag) and the checkboxes whatever you had selected are available as part of the present request object. You are fetching the values using the "request" object.

Remember, request is one of the 9 implicit objects in JSP! The container (Servlet/JSP engine) does the work of putting the values into request object! so you don't need to anything!
Dinesh Remje
Ranch Hand

Joined: Jan 26, 2010
Posts: 57
Thanks Raghavan that really helped me to have some knowledge about request object.
Once again thank you.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Dinesh Remje wrote:Thanks Raghavan that really helped me to have some knowledge about request object.
Once again thank you.


My Pleasure Dinesh!! You are welcome!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: request.setAttribute() and request.getAttribute()
 
Similar Threads
If loop in Sriptlet
strange output !! include / jsp:include
flush and forward works ?
How view of Table can be control with the help of Array Elements
create multiple Checkboxes with data base values in jsp