GeeCON Prague 2014*
The moose likes Servlets and the fly likes Resfresh page and getting values when option is selected Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Resfresh page and getting values when option is selected" Watch "Resfresh page and getting values when option is selected" New topic
Author

Resfresh page and getting values when option is selected

apurv suthar
Ranch Hand

Joined: Feb 05, 2012
Posts: 35
I wants to know that is it possible that, when i select the option from drop-down menu on jsp page ,it sends it on servlet and return back to same page with results as per option was selected.

Like, if I am selecting country name from drop-down menu at a time page is refreshed and the value below that country options which is ,options for cities are available with indian cities!!
I have tried to find but, in each code javascript is their...Is it possible without javascript?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61305
    
  66

Doing this with JavaScript is the correct and modern way. Back before Ajax, you had to submit and refresh the whole page. You can still do that, but it will make your application looks like it was written 10 years ago.

Sort of like building a car today using a crank in the front to turn the motor.

Do have a valid reason to avoid JavaScript?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

Most modern sites use Javascript to provide a dynamic interface, almost every browser supports it. Personally I would use the onChange event on the dropdown box and use an Ajax approach to request and display the results. I hate sites that refresh or reload the whole page when you select something; it looks so outdated and clunky. You can write your own ajax code or use some 3rd party solutions.
apurv suthar
Ranch Hand

Joined: Feb 05, 2012
Posts: 35
Ok..then can you give some advise on how to do this task?
and also that ,can javascript interact with FormBeans to get values from FromBean?
(I don't know javascript, so sorry If you found this questions stupid)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61305
    
  66

What's a FormBean? It's not part of Servlets or JSP.

In any case, no: JavaScript, which executes on the client after the HTML is generated and delivered as the response, cannot directly interact with Java code running on the server to create that response.

There are likely hundreds to thousands of examples of doing this on the Web. Please search.

If you want to use jQuery for your Ajax and JavaScript needs (you should use a library, Ajax has too many cross-browser nuances to do it yourself) you can grab the free sample source code for my jQuery book. The code for chapter 8 shows how to use Ajax and JSP to populate dropdowns dynamically
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61305
    
  66

Oh, and if you want to understand how JSP works and why it cannot interoperate with JavaScript, I suggest reading this article.
Sachin Kadian
Ranch Hand

Joined: Jan 24, 2012
Posts: 33
you can do that very easilye using javascript like this..

run this code...

apurv suthar
Ranch Hand

Joined: Feb 05, 2012
Posts: 35
Thanks to all of you .. I got it..Ajax is necessery for this kind of problems and its a modren day design approach.I will try to follow the same in my project also.
apurv suthar
Ranch Hand

Joined: Feb 05, 2012
Posts: 35
can anyone says that how to do the same using struts? because, when i am doing




and define the same demo() function as per above code , it doesn't gives any thing....code remains same!!
 
GeeCON Prague 2014
 
subject: Resfresh page and getting values when option is selected