Solved

execute a jsp scriplet on submit of form

Posted on 2006-11-30
14
812 Views
Last Modified: 2012-08-13
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
Comment
Question by:matthew016
  • 4
  • 4
  • 3
  • +3
14 Comments
 
LVL 3

Assisted Solution

by:Madhukar_Mythri
Madhukar_Mythri earned 100 total points
ID: 18047719
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
 
LVL 12

Expert Comment

by:enachemc
ID: 18048256
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
 
LVL 10

Assisted Solution

by:jaggernat
jaggernat earned 100 total points
ID: 18049586
why scriplet. use JSTL. what exactly you want to do?
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 18050429
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
 
LVL 11

Assisted Solution

by:Manish
Manish earned 100 total points
ID: 18052092
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
 
LVL 15

Expert Comment

by:bpmurray
ID: 18052433
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
 
LVL 9

Author Comment

by:matthew016
ID: 18059250
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 9

Author Comment

by:matthew016
ID: 18059258
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
 
LVL 12

Expert Comment

by:enachemc
ID: 18059302
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
 
LVL 9

Author Comment

by:matthew016
ID: 18063172
<html:hidden .... />

How can I get the URL without JSP scriplets ?
0
 
LVL 12

Expert Comment

by:enachemc
ID: 18063587
JavaScript.
0
 
LVL 9

Author Comment

by:matthew016
ID: 18069164
how can I use a <html:hidden with javascript, sorry :-)
0
 
LVL 12

Accepted Solution

by:
enachemc earned 100 total points
ID: 18069199
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
 
LVL 15

Assisted Solution

by:bpmurray
bpmurray earned 100 total points
ID: 18072200
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now