• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Switching form action based upon radio selection

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Does anyone know if it is possible for a webpage to be mapped to several servlets?

For instance, is it possible for my webpage (shown below) to 'call' a servlet, say EasyQnServlet, when a user chooses the radio button 'Easy' and another servlet, say DifficultQnServlet, when the option 'Difficult' is selected?



An advance thank to all
 
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes you can attain using AJAX... Using "OnClick" event on Radio button, Toggling of URL should be done correspondingly based on option selected...
 
Apple Khoa
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ram Narayan.M wrote:yes you can attain using AJAX... Using "OnClick" event on Radio button, Toggling of URL should be done correspondingly based on option selected...



Hi Ram Narayan.M,

Thank you for your prompt reply.

I will take a look at the ajax examples available

Thanks for your help once again.
 
Sheriff
Posts: 67756
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
Ajax is not necessary. Just a little JavaScript to change the action of the form based upon what element is selected.
 
Apple Khoa
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Ajax is not necessary. Just a little JavaScript to change the action of the form based upon what element is selected.



Hi Bear Bibeault,

May I know what do you mean by "Just a little JavaScript to change the action of the form based upon what element is selected"?
Could you kindly provide me with some examples?

By the way, I have tried using Ajax as suggested by Ram Narayan.M. However, I guess I have done something wrong somewhere as I am not getting what is desired. I will be explaining the issue in my next post (as I will be quoting Ram Narayan.M). Do you mind advising me on where my mistakes could be?

An advance thank for your help
 
Apple Khoa
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ram Narayan.M wrote:yes you can attain using AJAX... Using "OnClick" event on Radio button, Toggling of URL should be done correspondingly based on option selected...



Hi Ram Narayan.M,

I have tried using AJAX to map my webpage to different servlets based on the option selected by the user. However, I guess I have done something wrong somewhere as the output is not what as desired. My guess is that the AJAX is not responding to the 'onclick' event for the radio buttons. Could you kindly advise me on this issue?

My html code:


My web.xml code:


The pdf file generated contains only the title and the instructions as texted in by the users. The 'question' and 'answer' are not reflected in the output.

By the way, I am using doGET method in my servlet.

Is the "get" method used in my html script the possible cause? (I saw an example on a website using POST method.)
 
Bear Bibeault
Sheriff
Posts: 67756
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

Apple Khoa wrote:
May I know what do you mean by "Just a little JavaScript to change the action of the form based upon what element is selected"?


You can write one or two lines of JavaScript to establish an event handler for the change of selection and replace the action value accordingly.

By the way, I have tried using Ajax as suggested by Ram Narayan.M


That's like using nuclear weapons to crack walnuts.

Ajax is a powerful and useful tool that is completely unnecessary for this issue.
 
Ram Narayan.M
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are trying to change "action" attribute in form, then you can go with the way Bear Bibeault suggested .. no need of AJAX...

If you are intending to display some dynamic large forms based on option selected, use AJAX in that aspect...

Actually, What will be displayed in the "message"?...

I cant see form named with "MyForm"... Please check the HTML Prgm again...
 
Apple Khoa
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:You can write one or two lines of JavaScript to establish an event handler for the change of selection and replace the action value accordingly.



Hi Bear Bibeault,

I have tried using an event handler onclick="location.href='/EasyQnServlet'" (Line 36 of HTML code) & onclick="location.href='/EasyQnServlet'" (Line 37) to perform the task of mapping to several servlets with one webpage. However, I am still not getting the desired output. The content for 'question' and 'answer' (radio buttons) are not being displayed; only the content 'title' and 'instruction' (text boxes) appear on the output.

Could you kindly advise me on this issue?

My HTML code:


My web.xml code:

 
Bear Bibeault
Sheriff
Posts: 67756
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
You might want to fix the quoting in your reply.

In any case, just post the relevant parts of the code or an SSCCE. I'm not going to look at code that covers more than one screen to find the one line of JavaScript that's relevant.

By the way, when it comes to event handling, you really want to use jQuery or similar. Otherwise, you've got to deal with IE's nonsense.
 
Apple Khoa
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:You might want to fix the quoting in your reply.

In any case, just post the relevant parts of the code or an SSCCE. I'm not going to look at code that covers more than one screen to find the one line of JavaScript that's relevant.

By the way, when it comes to event handling, you really want to use jQuery or similar. Otherwise, you've got to deal with IE's nonsense.



My HTML code (shortened)


Used <form method="get" action="simul"> in the html.

My web.xml code (shortened)
 
Bear Bibeault
Sheriff
Posts: 67756
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
No, you're causing a redirect, not submitting the form. and that's not what you do either, is it?

Using jQuery, here's something off-the-cuff (not guaranteed to be 100% correct). In the ready handler:

If you don't want to use jQuery, it'll take a bit more code, but this should give you the idea.

You don't want the change of radio value to trigger a redirect or a submission, you just want it to switch out the action, right?

Of course, it might just be easier to handle it on the server by make the servlet do the work based upon the submitted value of 'difficulty'.
 
Bear Bibeault
Sheriff
Posts: 67756
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
At this point this is all about JavaScript and not servlets, so I've changed the title a bit and moved it to the HTML forum.
 
Ram Narayan.M
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is your plan to render on toggling Easy and Difficulty mode?...

Meanwhile In,

<

location.href cannot be invoked in this manner... It should be embodied in a function and have to call that function... But, using location.href will redirect to the next page...

And you cant get the effect of actions based on toggling effect happening in the same page...

 
Apple Khoa
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ram Narayan.M wrote:What is your plan to render on toggling Easy and Difficulty mode?...

Meanwhile In,

<

location.href cannot be invoked in this manner... It should be embodied in a function and have to call that function... But, using location.href will redirect to the next page...

And you cant get the effect of actions based on toggling effect happening in the same page...



My Mathematics webpage has a form which has two options (radio buttons) for the users - Easy Question or Hard Question

If the user selects 'Easy Question', the webpage has to call the EasyQnServlet (with 'easy questions' mathematical logic in servlet).
If the user selects otherwise, the webpage has to call the HardQnServlet.

Everything is working fine when my webpage maps only to ONE servlet. My issue now is how to make my webpage maps to the relevant servlet according to what the user chooses (Easy / Hard).

By the way, output is set as pdf file in my servlet, not HTML.

(I am not using any database.)
 
Bear Bibeault
Sheriff
Posts: 67756
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
If the JavaScript is beyond you at this point, I'd just make the switch on the server.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic