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 Retaining drop down values on page submit 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 "Retaining drop down values on page submit" Watch "Retaining drop down values on page submit" New topic
Author

Retaining drop down values on page submit

Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi Friends,
Im facing a problem while in my web app.I have a drop down in my jsp , that contains a list of choices , the user selects one of the choices and hits submit, however upon form submission the page is reset to the value corresponding to the first value in the drop down , why is this happening?
I use a javascript to submit the page upon change in the drop down . Ive pasted the code im using for the same .It would be great if anyone could point where im going wrong .Thanks

the JS

where "Main " is the name of the jsp im submitting to ..


Learning and Learning!-- Java all the way!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60756
    
  65

Why would you expect it to be retained? if the page is reloaded, it's like it's a completely new page to the browser. The browser doesn't care that on the previous request it was the same page.

If you want to retain the value, you actually need to restore the selection by placing the selected attribute on the appropriate option element.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ravi Majety
Ranch Hand

Joined: Feb 26, 2009
Posts: 59

Yes you can do that. You can do that by making use of ajax.

Instead of page submission. You submit the values to the destination jsp page by sending it through ajax. So in this , there would be no reload or submission of total page. You can even get the status through ajax. when you got the sucessfull status, then you can even keep an alert displaying "submission is successful".

May be this is not the preferrable one, but this is also an way where we can do it.

Thank you

Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi Bear and ravi,
Thanks for replying!.
I want to do the exact same thing as suggested by Bear , but at a loss on how to go about it ..right now this is how my jsp code runs ..

Could you please explain by helping about how i should modify my code?

@ravi,
Thanks for your reply, however i dont have knowledge of ajax , so I dont think i would be able implement it.
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

Bear Bibeault wrote: ....
If you want to retain the value, you actually need to restore the selection by placing the selected attribute on the appropriate option element.


vic, as Bear has mentioned, to retain the value you have to use the 'selected' attribute of option tag.

Note- you could have put the 'select' attribute in there.. but since you are iterating over the list, your code will put the selected attribute at all the option tags.. so one way I can think off is to put the selected attribute after you render the page in javascript.


Time is what we want the most, but what we use the worst. -- William Penn
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477



Robin John wrote:
Note- you could have put the 'select' attribute in there.. but since you are iterating over the list, your code will put the selected attribute at all the option tags.. so one way I can think off is to put the selected attribute after you render the page in javascript.

thanks a lot for replying Robin,
I did try your approach earlier however, like you pointed out , all of them appear to be selected. How do i modify my javascript to accommodate the same?
Thank you.
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

Remove the 'selected' attribute from the loop and add the script after body load


please test this... and remember the selectedIndex starts from 0, you can put some logic as to which index to find.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Thanks for the reply Robin!
I will try the snippet that you have provided , however if I the snippet worked as intended ,wouldnt the drop down be set to the one corresponding to value='3'? always? Im a newbie so please pardon if it was a lame question :s
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

Vic Hood wrote:
....
wouldnt the drop down be set to the one corresponding to value='3'? always?


exactly, that was just an example to show how you should use the script. You can set your own value which you want to select.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi Robin ,
Thank you for replying .I did try your snippet and did work by setting the value of the drop down to a fixed value . E.g like in your example setting it sets to the one corr. Value of '3' but when I do something like
The script fails . COuld you please point to where I'm going wrong ? Thanks .
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60756
    
  65

One: scriptlets? why not the EL?

Two: if it's script that's failing, looking at the JSP source is not helpful. What does this turn onto on the client?
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Bear Bibeault wrote:One: scriptlets? why not the EL?

Well , on reason in particular, after having used scriptlets for a while , they came off the back of my mind ,also I wasnt sure if EL would work in this case
Bear Bibeault wrote:
Two: if it's script that's failing, looking at the JSP source is not helpful. What does this turn onto on the client?

Im guessing you are asking how does the HTML source of the rendered page look like ?
It detects the script only when i hard code the value of the dropdown like -'3', but when i put in the scriptlet it fails as in not shown in the source at all,
Any idea where im going wrong?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60756
    
  65

Vic Hood wrote: I wasnt sure if EL would work in this case

WHen that's the case, then it usually means you are doing something you shouldn't be! But in this case, grabbing a param value is easy as pie. Or a piece of cake. I can't remember which.

Bear Bibeault wrote:Im guessing you are asking how does the HTML source of the rendered page look like ?

Yup. That's all the browser cares about.

not shown in the source at all,

The either your scriptlet (ick!) or your assumption of what's available is wrong.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Thanks for replying bear!
I did try something like this ..

And I was able to view the value of domainTypeOid upon initial page load .(in the rendered source).However , when I clicked on one of the values in the drop down , the page submitted and reloaded but the script (above) was missing in the new page().also , the table structure that displays in the page is non exsistent in the jsp page . I have no clue how this is occuring . Any ideas on where im fumbling?
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

Vic Hood wrote:
when I clicked on one of the values in the drop down , the page submitted and reloaded but the script (above) was missing in the new page().also , the table structure that displays in the page is non exsistent in the jsp page . I have no clue how this is occuring . Any ideas on where im fumbling?


This generally happens when the rendering of the page stops at a point, then the rest of the page is not shown in the source. Find out where it stops, this problem might be different - for example - not populating your dropdowns each and everytime etc. Separating different tags might help you.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Thanks for the reply John! That was a red herring .My servlet class was redirecting to an older version of the jsp. which is why i was seeing 'non exsistent' tables :s Im sorry .
However, when i do try running the program , and change the dropdown , the value is not being passed . The page always sets the "${domain.domainTypeOid}"; to 0, why is this?
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

Vic Hood wrote:
However, when i do try running the program , and change the dropdown , the value is not being passed . The page always sets the "${domain.domainTypeOid}"; to 0, why is this?


you tell me ? how do you pass the value when you change it ? are you using Ajax / Javascript ? how ? as far as I know... the page wont set anything unless you want it to...
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Okay . Im using javascript in the function

When the page submits , I take the value of selectedval in my servlet and assign it to a variable and process it and assign it to the bean property domainTypeOid.
However , when I change the drop down value , this is not reflected in the value of domainTypeOid.. why so?

EDIT : To add corresponding servlet code..

Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

Vic Hood wrote: RequestDispatcher rd = request.getRequestDispatcher("POC.jsp?Dtype="+domainType_Oid);


so does it open the POC.jsp ? and if yes what does the 'Dtype' in the URL contain ?
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi Robin,
Thanks for replying.
Im afraid it doesnt work the way I expect it to ..
When i change the value in the drop down the url changes from
http://localhost:8080/CRUDJspBean/
to
http://localhost:8080/CRUDJspBean/DAO
where DAO is the name of my servlet to which the form submits , however the parameter is not being passed.

Thanks for helping out
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

Vic Hood wrote: <FORM METHOD="POST" ACTION="DAO" NAME="crudForm" action="Main.jsp" ID="Main"><INPUT
TYPE="HIDDEN" NAME="selectedval" ID="selectedval"><!-- Hidden variable to store selected domaintype -->


what did you mean by this ?

action / ACTION means the same...
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

My Bad!
Thanks for replying Robin! That was an oversight! I only intended to have

However , making that change has no effect either.
Thanks .
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

can you paste your full jsp, servlet and the configuration files ?
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Thank you for replying ,Robin !
Im pasting my jsp file below .



Ive created a pastebin file for my servlet here to avoid clogging the page and my web.xml here.
Thank you once again for taking the time out to reply .
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

okay, so what is your problem now ? is it still just retaining the dropdown value ?
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi Robin,
Thanks for replying!
Well my problem , as from the beginning is that , the drop down value is being reset to the first value upon page submission .Ie. no matter what I click from the drop down , the page submits and shows me a list of rows that correspond to the first selection in the drop down .
However I did do some debugging , and did a bit of change to my servlet code by removing the lines.


where Dtype is the String that stores the value obtains the value of the selected dropdown .


Now , when I run my code and click on a value from the dropdown , the page submits and prints"Nothing found to display". However, through logging statments in my DAO i can see that the corresponding result set to that particular domaintype is being fetched , however its not being printed .Any clues as to why is this happening?
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

Here I found out some example and I have modified the example code to retain the dropdown... hope it helps.

MyServlet.java



index.jsp ( the welcome file should be set in web.xml )


final.jsp ( retaining name and dropdown values here )





Hope it helps...
NOTE: I have not tested the code just modified it as per my assumptions, please test it after deploying, let me know if you were able to retain the dropdown values.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi Robin ,
Thanks for your sample.
I did try deploying your example , Im asked to enter a name in the first form (index.jsp)and shown a drop down in the second(final.jsp).However there is no link or button that corresponds to "here" .It just appears as static text. And so Im not being able to test whether drop down values are being retained
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

Nope 'here' was not a link, and what did you see after changing the dropdown values ? did it retain the values that you selected?, the page is directed to the same page after submit so it should have the value in the dropdown to what you selected.
Ravi Majety
Ranch Hand

Joined: Feb 26, 2009
Posts: 59

Hi Vic, just check out this code. This is small jsp where submits it to itself and retains the selected combobox value. Hope it would help you a little.

File Name: Test1.jsp
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi Smarty ,
thanks for the reply and the code sample . I did try that out and it worked .
Ravi Majety
Ranch Hand

Joined: Feb 26, 2009
Posts: 59

@vic

All the best and welcomed with new issues and doubts for discussion.

Dax Joshi
Greenhorn

Joined: Jun 10, 2013
Posts: 11

I have a page request with many product set in session and a id in request to change product details

form in page is like...

Product.java is like..


values in dropdown are being set in form by script but on from submission I get '0'

Pleease reply as soon as possible.
 
jQuery in Action, 2nd edition
 
subject: Retaining drop down values on page submit
 
Similar Threads
Dropdown Selected returning unexpected value on submit.
Page not refreshing dynamic content.
Drop down population Issue.
Page submission on change of drop down value not working
What to do to make this web page thread safe?