aspose file tools*
The moose likes JSP and the fly likes Value from select element is null in request Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Value from select element is null in request " Watch "Value from select element is null in request " New topic
Author

Value from select element is null in request

Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 327
Here is where I create the option list:


I would like to have the FIRSTNAME and LASTNAME concatenated as the display for the option list,
but the bigger problem is that the professor attribute in the request is null when accessed as follows:



I have been reading posts for quite a while and other articles, and I am just not seeing what the problem is.

Thanks for any direction.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Request attributes and request parameters are not the same thing.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61653
    
  67

Two things:

First, please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information. You can go back and change your post to add code tags by clicking the button on your post.

Second, examine the difference between getParameter() and getAttribute().


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 327
Thank you; I know that. At least the mistake was only in a value being used to print out a debug statement. I did use "getParameter" when setting the "sessionProfessor".

String sessionProfessor = request.getSession().setAttribute("professor", request.getParameter("professor"));


It is, however, null when I try to access it.

I did struggle a while before discovering the difference in getParameter and getAttribute, but I resolved that a few weeks ago, just failing to show that in this code. At any rate, the value is not in the request object.

When I leave this initial jsp page with the select element option items, it is with an HREF link. Would that prevent the values from the option list being stored in the request object?

I was using radio buttons to try to go to one of two pages, but I could not figure out how to do that so I changed them to two HREF links which takes me to the correct page but without the values stored in the request object. I can go back to using the radio buttons, but I could not determine how to use the value from the radio button to go to the correct following page. I understand using the action attribute of the form tag in html on the jsp page; but if I want to go to one of two different jsp pages based on a radio group value, how would I do that? Would that then enable the jsp page to store the values in the request object?

I am very new to web projects but have been reading a lot and learning a lot, just not "there" yet. Thank you for any advice.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

Mark,

When I leave this initial jsp page with the select element option items, it is with an HREF link. Would that prevent the values from the option list being stored in the request object?


href link do not submit forms by default. You need to use javascript to call submit() method to submit the form. Also to note is the request objects ( paramertes as well as attributes) exists only for that particular request , for the next request it new request object and it has to be set with required values again.





I was using radio buttons to try to go to one of two pages, but I could not figure out how to do that so I changed them to two HREF links which takes me to the correct page but without the values stored in the request object. I can go back to using the radio buttons, but I could not determine how to use the value from the radio button to go to the correct following page. I understand using the action attribute of the form tag in html on the jsp page; but if I want to go to one of two different jsp pages based on a radio group value, how would I do that? Would that then enable the jsp page to store the values in the request object?


You can submit to 2 different actions by invoking appropriate javascript function. During submission action attribute can be changed dynamically (but redefined) and submitted.

ex : you can use similar logic for radi button as well.








Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 327
I am not allowed to use JavaScript in this project. I can store the request parameters in the session object once I get them into the request object, but I can't do that using javascript.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Then use a form?
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 327
i don't understand the question. Of course I am using a form in one jsp with the select options described in the original post. Then there are two HREF links at the bottom of the form for the user to determine the next page to go to. I think the selection from the select options is not stored in the request when using an HREF link to go to the next jsp.
Is this correct?

I have moved the select option code to the same page as I have a file upload as follows:



However, I am still finding a null value for the select option in the request parameter.

What am I missing?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61653
    
  67

Mary Taylor wrote:I think the selection from the select options is not stored in the request when using an HREF link to go to the next jsp. Is this correct?

Absolutely correct. Form elements are submitted only when the form is submitted. Anchor tags with hrefs have no effect whatsoever on the form and submit nothing to do with the form.

You'll need to encode the request parameters to be submitted onto the href itself, which of course, you cannot do with a dynamic value like the selected option without JavaScript.

Without JavaScript, you are severely severely limited as to what sort of client-side activities can take place.
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 327
Thanks, Bear. Slowly learning this area. Now, can you tell me if I have a form with a select option and a file upload as you can see in the posts above, should that work when I submit the form? I have successfully uploaded several files to my database before starting to work on adding this information from the select option element. I need the information from the select option element to store with the file. Can I get that information within the form as I now have it with the select opotion and file upload both within the same form.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61653
    
  67

Yes, the other form elements (besides the file element) will be submitted as part of the multi-part form. You cannot, however, use getParameter(), which does not work with multi-part forms. If you are using a 3rd-party package to parse the multi-part form, it will have an API that allows you to get at the values.

Otherwise, you'll need to parse them out yourself. (not recommended!)
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 327
Are you saying I cannot get the selected value out of the request without a third party tool? I am already getting the file uploaded into a blob and retrieved so that part is ok. Maybe I can somehow redesign this thing so I only upload the file from that page which is what I previously had until I needed to move the selected option away from the page with the HREF links trying to get it's value.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61653
    
  67

Are you currently parsing the multi-part form yourself? If so, the request parameters are part of the input stream.

Most experienced developers will recommend the use of one of the 3rd-party packages however. See the JSP FAQ for links.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Value from select element is null in request