It's not a secret anymore!*
The moose likes Servlets and the fly likes calling a servlet from a javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "calling a servlet from a javascript" Watch "calling a servlet from a javascript" New topic
Author

calling a servlet from a javascript

Surya Bavirti
Greenhorn

Joined: Sep 08, 2008
Posts: 13
i have a form with 3 text fields(txt1,txt2,txt3).i have done all javascript validations for the 3 text fields.once all the validations are passed, i nedd to call a servlet from a javascript to insert the all 3 text field data into a database.can you some one guide me?


Thanks in advance
Bavirti
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Surya Bavirti wrote: i nedd to call a servlet from a javascript


use html button and onclick call the javascript to submit your form
Surya Bavirti
Greenhorn

Joined: Sep 08, 2008
Posts: 13
i have written a button, it's a image. img src="images/submit.jpg" id="SubmitImage" onclick="return ValidateForm()"
In ValidateForm javascript function , i have written all text field validations.After that i need to redirect the control to a servlet with in that javascript function.i don't know how to call a servlet with in the javascript function.

thanks
Bavirti
Arpit Purohit
Greenhorn

Joined: Jan 09, 2007
Posts: 21
Use following code for reference:



Further you can access txt1,txt2 and txt3 as parameters in the servlet(whose URL you are providing in Form's action attribute) and implement any functionality (like DB operations) .


Regards,
Arpit Purohit
Pravin Thakare
Greenhorn

Joined: Mar 26, 2009
Posts: 1
Surya Bavirti wrote:i have a form with 3 text fields(txt1,txt2,txt3).i have done all javascript validations for the 3 text fields.once all the validations are passed, i nedd to call a servlet from a javascript to insert the all 3 text field data into a database.can you some one guide me?


Thanks in advance
Bavirti


You need to create a XMLHttpRequest object then open the request using open function like
request.open("GET/POST","servlet url","true or false");
request.send();
in case of POST insert all parameter in send method
Nick Potter
Ranch Hand

Joined: May 07, 2008
Posts: 126
Here's an example:


You can also combine the 2 forms into one.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
All of the above suggestions are cumbersome and makes no sense.

Just let the form action point to the servlet let the outcome of the Javascript function control the submit of the form.

E.g.

Whenever the onclick event returns false, the button's default action (submitting the form) won't be invoked. Simple as that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: calling a servlet from a javascript
 
Similar Threads
JSF and client side validation
persistent values in jsp
How do I get my JSP to call a servlets doPost
How to catch the Tab key code
how many types of validations are available in struts framework