Link to home
Start Free TrialLog in
Avatar of matthew016
matthew016Flag for Belgium

asked on

execute a jsp scriplet on submit of form

Hi,

I want to be able to execute a jsp scriplet when the user click on submit and *before* the form is posted to the server.

How can I do this ?

Thank u.
SOLUTION
Avatar of Madhukar_Mythri
Madhukar_Mythri

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can use AJAX to do the request on the server. The submiting will be canceled until the response is received (asynchronous). When the response is received, you can submit the form.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just use JavaScript, and if you want to continue, return true; if you want to abort the submit, return false. Something like:

<html><head>
<script type="text/javascript">
   function myScriptlet() {
      var elem = document.getElementById("foo");
      if (elem.value == "OK")
         return true;
      else
         return false;
   }
</script>
</head><body>
Put some text here:
<input type="test" id="foo">
<br />and click this:
<input type="submit" value="Click Me" onclick="return myScriptlet();">
</body></html>


This does the submit, only if the input field contains the text "OK". Otherwise, it terminates the submit by returning false.
SOLUTION
Avatar of Manish
Manish
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
On reflection, it's impossible to answer your question without more information. The possible solutions are:

1. If you don't need to access an server-side data or functionality, use JavaScript.
2. If you want to access some canned response, consider doing it usin AJAX to query a special servlet/scriptlet, and continue or terminate your submit based on the response.
3. Submit the request and either continue to the next page, or return to the current page with some extra markup to indicate why.

When trying to guess what you want to do, I can only think that you want to perform some sort of validation. The usual way to do this is to execute your scriptlet and return an error text in some predefined are of the page, with the field(s) in error highlighted using a different style.

Is this what you want to do?
Avatar of matthew016

ASKER

I didn't tell exactly what I want to do because it's some kind of complicated :

I have a left menu on my website (all the pages contains this left menu) *in a separate JSP! : left_menu.jsp*, that contains a form being able to log-in (asks for a username and password).
So when a user submits the form in the left menu,
I need to know where he was (in which page, since the left menu is contained by all the pages, I need to store somewhere where he was). I store this in a session attribute "page".

I could do this just in the body of the page, store the name of the page in session attribute "page", but then I noticed there is a problem :
If he stays on this page during more then 20 minutes, and then submits the form, the "page" attribute in the session had been removed.

So I want to store the name of the page in "page" attribute just before he submits the form to the server, then I'm sure the page attribute will exist and I can forward to the correct page.


Forget my solution of putting a session attribute just before the submits, that won't work since if I'm in left_menu.jsp,
I won't know in which page he is (index containing this menu ? registration page containing this menu ?)

I don't know how to solve this ...
If I need to store in which page he is, that's because if he submits from left_menu.jsp, it goes in LoginAction.do Action and there I need to forward to the page he was in .
you could add a hidden input in your login form. When submitting, fill this variable with the url of the current page. In the login page, you will have the URL in the request parameters. After you log the user, just do a redirection to the URL just received.
<html:hidden .... />

How can I get the URL without JSP scriplets ?
JavaScript.
how can I use a <html:hidden with javascript, sorry :-)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial