Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is it possible to call a servlet function using JavaScript?

 
max soyosa
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi everyone!

Is it possible to call a servlet function using javascript function?

thanks in advance.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abhijith Prabhakar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course it's possible to invoke a servlet with a form -- that's not what is being asked here.
 
Zye Valfzaurk
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why can't you send the parameters in your add method as a POST to your servlet and go from there?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic