xtxylophone
xtxylophone

Reputation: 56

Java Servlet and Jquery

Im currently making a Java Servlet that can respond to jquery calls and send back data for my web page to use. But this is only a response using the doGet method.

Is there a way to have multiple methods in a Servlet and call them each with JQuery?

i.e. have a method called Hello and it returns a String "Hello" and another method called Bye and it returns a String "Bye". Is there a way using Jquery or some other technology to do this kind of thing?

Im quite new to servlets so Im still not sure what they are fully capable of. So is the doGet the only method to 'get in' and I just branch responses from there?

Upvotes: 2

Views: 426

Answers (3)

Ramesh PVK
Ramesh PVK

Reputation: 15446

Servlet Container supports Custom Http methods since Servlet 3.0. For Ex,

public void doHello(HttpServletRequest req, HttpServletResponse res) {
   //implement your custom method
}

The above method in Servlet can be invoked using hello http method.

But i am not sure if jquery has the support to invoke custom HTTP methods.

If it does not have, then the only option you have.

  • Invoke Servlet using GET and action parameter.
  • Read the action parameter and invoke the method using reflection.

Upvotes: 0

Joey Roosing
Joey Roosing

Reputation: 2155

Personally I use reflection in my controllers(servlets) which basically let me achieve this.

If I have a servlet called UserController

The main url to call the servlet will be /user. Knowing this, I always pass my first parameter as ?action=add

Then in my servlet I have a method called add or actionAdd. Whichever you prefer.

Then I use the following code;

String str = String str = request.getParameter("action").toLowerCase();
Method method = getClass().getMethod(str, HttpServletRequest.class, HttpServletResponse.class);
method.invoke(this, request, response);

Explanation:

str will have the action parameters value, add in this case. Method method will be a reference to a method with the given name(str) and its expected parameter types.

I then invoke the method, passing the context, request and response.

The add method would look something like this;

public void add(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { 
    //do add stuff

        String url = "/user/index.jsp";
        RequestDispatcher dispatcher = context.getRequestDispatcher(url);
        request.setAttribute("User", user);
        dispatcher.forward(request, response); 
}

I don't know about only passing back a string. But this should get you a basic idea.

Do note that reflection can cost you, altohugh it shouldnt really affect you much like this. And it is error prone as method names/signatures need to match perfectly.

So from jquery you would do an ajax request to the url:

 localhost/projectname/user/add (if you use urlrewrite)
 or
 localhost/projectname/user?action=add (if you dont)

Upvotes: 0

Jigar Joshi
Jigar Joshi

Reputation: 240900

With Servlet you can either call the service method, so may be for your scenario you could pass the parameter to decide which method to invoke from doGet()

also you could identify if request is coming from AJAX using header check

There are other technologies available which will allow you directly invoke method See JSF, DWR

See

Upvotes: 3

Related Questions