views:

24

answers:

1

I am currently a asp.net guy, but have to do some jsp work now. I know that in asp.net, you can define a public static method with [WebMethod] attribute e.g.

  [WebMethod]
  public static string GetIt(string code)
  {
    return GetSomething(code);
  }

then, I can call this mehtod in jquery

$.ajax({
  type: "POST",
  url: "PageName.aspx/GetIt",
  data: "{'code':'+$("#code").val()+"'}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(msg) {
    // Do something interesting here.
  }
});

can someone give me some light how to do it using jsp?

+1  A: 

With pure jsp and servlets you'll have to do a lot of things to achieve that.

It can easily be done with spring-mvc, almost in the same way you showed:

  • create a class and annotate it with @Controller
  • create the method to be like this:

    @RequestMapping("/path/get")
    @ResponseBody
    public String getIt() {
        ...
    }
    
  • you'll need <mvc:annotation-driven /> and jackson on your classpath.

Note that you are calling "get", so the logical http method is GET, rather than POST

Bozho