wood burning stoves*
The moose likes JSP and the fly likes Getting a String out of a dropdown menu Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Getting a String out of a dropdown menu" Watch "Getting a String out of a dropdown menu" New topic
Author

Getting a String out of a dropdown menu

Sam Gardner
Ranch Hand

Joined: Oct 02, 2007
Posts: 31
Hey guys,

I'm having trouble getting a string variable out of a dropdown form that I have on one of my pages.

What I would like is for the page to store a String value locally that is based on which option in the menu is selected when the form is submitted.

ie: hello
goodbye
good morning

submit

If the user clicks submit when "hello" is selected, the page will store "hello" locally in a String value.

I need this String value to be readable in a JSP statement, if that makes sense

ie (<% user.newPage(helloStringVariable);")

If someone could show me how to do this, it'd be greatly appreciated!

Sam
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
The value of the selected dropdown selection will post to wherever you are posting as a name/value pair. Hopefully, you are going to a Servlet and not straight to a JSP.

More info please
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Originally posted by Sam Gardner:
the page to store a String value locally

You'll have to define what you mean by "locally".


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sam Gardner
Ranch Hand

Joined: Oct 02, 2007
Posts: 31
Heh, well, first to explain my background. I have one year of Java programming experience through college, and I was hired to write a frontend for some data entry that research lab on campus needed to have done using assorted undergrads (ie, needs to be simple and easy to use, and maintainable). I looked around online (I really don't have any web development experience) and stumbled upon the Sun Application Server and JSP, so I've learned enough in the last few weeks to make a web-deployable interface with a MySQL database.

So, I don't claim that anything I'm doing is remotely close to intelligent. Please phrase your suggestions accordingly.

Anyway, currently I have my app set up to go through a password verification server and then get to the actual data entry page.

At one point, I want the user to have a drop-down list of surveys that they can enter data in. I need the actual text from this list (ie survey1) to be stored on the JSP page that the user is currently on, so the JSP can call a method in a class that I have written that will output the survey's prototype (ie q1 = true or false, q2 has choices a, b, c, etc) as an HTML table.

So, they choose survey1, click submit
and a method is called like createTable(survey1) that'll output the SQL survey prototype as an HTML table.

Let me know if that makes sense, I can't imagine this should be too much trouble but I'm just having issues conceptualizing the whole concept of beans.

Sam
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Originally posted by Sam Gardner:
I need the actual text from this list (ie survey1) to be stored on the JSP page that the user is currently on, so the JSP can call a method in a class that I have written that will output the survey's prototype (ie q1 = true or false, q2 has choices a, b, c, etc) as an HTML table.


JSPs (and HTTP) doesn't work that way. The JSP executes on the server and sends an HTML page to the client. Any user interaction with the page at that point can have no effect on code in the JSP which executed long ago and back on the server.

Perhaps this article can give you a better handle on the JSP life-cycle and how they operate.
Sam Gardner
Ranch Hand

Joined: Oct 02, 2007
Posts: 31
hmm, that was a good article, Bear, thanks.

Now that my original question seems a bit dumb, is there a way I can have JSP dynamically generate the contents of a frame based on the selection made in my dropdown?

I think it might work, since the frame would be dynamically generated, but I'm not sure.

Sam
Sam Gardner
Ranch Hand

Joined: Oct 02, 2007
Posts: 31
hmm, that was a good article, Bear, thanks.

Now that my original question seems a bit dumb, is there a way I can have JSP dynamically generate the contents of a frame based on the selection made in my dropdown?

I think it might work, since the frame would be dynamically generated, but I'm not sure.

Sam
Sam Gardner
Ranch Hand

Joined: Oct 02, 2007
Posts: 31
hmm, that was a good article, Bear, thanks.

Now that my original question seems a bit dumb, is there a way I can have JSP dynamically generate the contents of a frame based on the selection made in my dropdown?

Or, I should say, is there a way I might be able to do this without creating a separate frame for every survey.

eg: have a frame "survey prototype" that would just call my bean method and create a table based on which survey was chosen.

I think it might work, since the frame would be dynamically generated, but I'm not sure.

Sam
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

There are a number of ways to proceed.

You could have the dropdown selection trigger a full-page submit and you could render whatever you wanted on the JSP the next time around.

Or, you could submit to an iframe that can hold the results.

Or, if you want to dynamically add the results to the current page, you could start digging into Ajax.
Sam Gardner
Ranch Hand

Joined: Oct 02, 2007
Posts: 31
The iFrame idea sounds just like what I need.

Thanks!
Sam Gardner
Ranch Hand

Joined: Oct 02, 2007
Posts: 31
Well, the iframe idea sounds almost perfect, but...

The dynamic iframe that will be refreshed whenever the "submit" button is clicked will refresh differently for different surveys that the user has selected. How do I send the text of what the user selected to the iframe reference JSP so that it will generate the right context?

Sam
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

You update the iframe by either setting its src attribute to a URL, or by making it the target of a form submission. In either case, the seelction can be sent to the server-side resource as a request parameter.
Sam Gardner
Ranch Hand

Joined: Oct 02, 2007
Posts: 31
What exactly do I set the FORM ACTION =
value to if I want to just refresh my IFRAME?

I have it sending a request alright, but I can only get my form submit button to open the src of the IFRAME rather than just refresh the IFRAME.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

The action is independent of which window the request will be submitted to -- it will be the same no mattrer what. It's the target attribute you want to set to the name of the iframe.
Sam Gardner
Ranch Hand

Joined: Oct 02, 2007
Posts: 31
Yep, that did it.

Thanks for all the help Bear.

Someone can close this topic now if they want.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting a String out of a dropdown menu