• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Is it possible to call a servlet function using JavaScript?

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everyone!

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

thanks in advance.
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
please buy this thing and then I get a fat cut of the action:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic