• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Retaining drop down values on page submit

 
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ..
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 59
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you paste your full jsp, servlet and the configuration files ?
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay, so what is your problem now ? is it still just retaining the dropdown value ?
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 281
Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 59
Eclipse IDE Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Smarty ,
thanks for the reply and the code sample . I did try that out and it worked .
 
Ravi Majety
Ranch Hand
Posts: 59
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@vic

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

 
Greenhorn
Posts: 14
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
If I had asked people what they wanted, they would have said faster horses - Ford. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic