• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

calling a servlet from a javascript

 
Surya Bavirti
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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) .
 
Pravin Thakare
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's an example:


You can also combine the 2 forms into one.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic