Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 925
  • Last Modified:

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.
0
matthew016
Asked:
matthew016
  • 4
  • 4
  • 3
  • +3
5 Solutions
 
Madhukar_MythriCommented:
Try having the required scriplet part inside a java script method. Call this javascript method while submiting the form. The scriptlet would be executed as part of the funtion call before the form is posted to the server.

Thanks.
0
 
enachemcCommented:
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.
0
 
jaggernatCommented:
why scriplet. use JSTL. what exactly you want to do?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
bpmurrayCommented:
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.
0
 
ManishLeadCommented:
You can not execute scriptlet on submitting of form. You can execute javascript only ,which may contains values which are helpful to you to submit the form.
0
 
bpmurrayCommented:
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?
0
 
matthew016Author Commented:
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 ...
0
 
matthew016Author Commented:
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 .
0
 
enachemcCommented:
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.
0
 
matthew016Author Commented:
<html:hidden .... />

How can I get the URL without JSP scriplets ?
0
 
enachemcCommented:
JavaScript.
0
 
matthew016Author Commented:
how can I use a <html:hidden with javascript, sorry :-)
0
 
enachemcCommented:
assign an id to the html:hidden element, and use this id to get a reference to the object by using document.getElementById('hiddenInputId'). This does not involve any JSP Scriptlet ... just pure old fashion Javascript (written in a separate file or in the same file as the JSP page).
0
 
bpmurrayCommented:
Something like:

<html><head>
<script type="text/javascript">
   function setupHiddenField() {
      // Get the falue from the input field
      var newValue = document.getElementById("foo").value;
      // If it's blank, that's an error, so abort the submit by returning false.
      if (newValue == "")
         return false;
      // OK, set the hidden field to the input value
      var elem = document.getElementById("page");
      page.value = newValue;
      // Return true to allow the submit to continue
      return true;
   }
</script>
<style type="text/css">
</style>
</head><body>
<form id="form1">
   Input something here: <input type="text" id="foo">
   <input type="hidden" id="page" name="page" value="">
   <br />
   <input type="submit" value="Submit" onclick="return setupHiddenField();">
</form>
</body></html>
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 4
  • 4
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now