| Author |
Is it possible to call a servlet function using JavaScript?
|
max soyosa
Greenhorn
Joined: Feb 25, 2010
Posts: 24
|
|
hi everyone!
Is it possible to call a servlet function using javascript function?
thanks in advance.
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56554
|
|
|
Not directly. JavaScript executes on the client after the servlet or JSP has sent the HTML page to the browser. You can use Ajax to issue another request to a servlet under script control.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Abhijith Prabhakar
Ranch Hand
Joined: Dec 29, 2006
Posts: 56
|
|
|
Not sure whether I can understand your requirement correctly. Generally you can set a action on your form and do a form.submit() from javascript.
|
 |
max soyosa
Greenhorn
Joined: Feb 25, 2010
Posts: 24
|
|
so that means it is possible in AJAX..?
if so how can i do that..?
here is my sample code.
HTML code:
now, how can i call the add() function in the ajax?
please help me.
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56554
|
|
You can't call a method directly, you can GET or POST a request to the servlet.
If you really want a sort of function call, you can look into DWR (which uses Ajax and requests to make it seem like the function is being called).
|
 |
Zye Valfzaurk
Ranch Hand
Joined: Jun 07, 2011
Posts: 35
|
|
Yes it is possible to call a servlet using javascript(JSP).
one example is using the <form> tag...
here is a sample...
<form method ="get" action="myservlet">
<input type="submit" name="aaaa" value="zzz">
</form>
In this sample, your javascript would call a servlet with a filename myservlet.java upon clicking the button. Take note that you need not put the extension name of the servlet if a javascript would call it.
|
If I stare at something, it doesn't necessarily mean that it is beautiful..... because most of the time, I stare to see how ugly it truly is....
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56554
|
|
|
Of course it's possible to invoke a servlet with a form -- that's not what is being asked here.
|
 |
Zye Valfzaurk
Ranch Hand
Joined: Jun 07, 2011
Posts: 35
|
|
|
Sorry I didn't make myself clear.... if you want to call the add() function, then just create a seperate servlet which would contain all the methods in your add() function and call the newly created servlet... then would be as if you called the add() itself.... well just trying to help...
|
 |
Dan Walin
Ranch Hand
Joined: Nov 11, 2003
Posts: 109
|
|
The AJAX recommendation was a good suggestion. It's what we use. Here is a little portion of code were we call a servlet (CreateProgramCopy) from a JSP page using javascript. One thing to notice, that in this example is that we call this servlet "synchronously" which means the javascript code waits for the servlet to finish before moving on. In this implementation we found that if we made an asynchronous call we ended up having too many open connections because this code is called many times if a user selects multiple records to copy. For example of they select 20 records to copy, this code will run 20 times and we end up with 20 connections to our server and that was too much. The user has to wait a brief period before this completes but that was acceptable.
|
 |
leo donahue
Ranch Hand
Joined: Apr 17, 2003
Posts: 327
|
|
|
Why can't you send the parameters in your add method as a POST to your servlet and go from there?
|
Thanks, leo
|
 |
 |
|
|
subject: Is it possible to call a servlet function using JavaScript?
|
|
|