0

Possible Duplicate:
How to use Servlets and Ajax?

I have the following (part of) code:

$.ajax({
           type: "POST",
           url: "Myclass.DOSOMETHING(PARAMETER???)",
           data: dataString,
           dataType: "json???",

My question is: How can I send the post to a specific method of a Java servlet class (extension of HttpServlet)? For example: (OverviewServlet.java)

public void removeItem(int idItem) {
    itemDAO.delete(idItem);
}
Community
  • 1
  • 1
John Hendrik
  • 661
  • 1
  • 7
  • 20
  • Try following url. there is nicely explained example. Hope it will help you in your issue. http://www.mysamplecode.com/2012/04/jquery-ajax-request-response-java.html – Hamid Seta Nov 06 '12 at 12:50

1 Answers1

4

You don't send a request to a Java method, but to a URL. If your servlet is mapped to this URL, then its doPost() method will be called (since you chose POST to send your request).

Many MVC frameworks (like Stripes, Play, Spring MVC) allow defining actions (sort of servlets) with several methods, each mapped to a given URL.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • How we can access array objects passed from javascript ajax post method in java controller(Sping MVC)? That is `var arrayObj = [{id: 1, value: 'one'}, {id: 2, value: 'two'}];` in javascript passed as ajax post data to server. How we can access object variables too in servlet? – Justin John Jan 12 '14 at 08:41
  • @JustinJohn: ask your own question, with the complete JS code used to send the request. All I can see is an array definition here, and answering in comments is not ideal. – JB Nizet Jan 12 '14 at 08:46
  • Please find the [question](http://stackoverflow.com/q/21062014/1036917) here. – Justin John Jan 12 '14 at 09:47