Solved

execute a jsp scriplet on submit of form

Posted on 2006-11-30
14
879 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains the fundamentals of industrial networking which ultimately is the backbone network which is providing communications for process devices like robots and other not so interesting stuff.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

617 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