1

I wrote a server program in java but in order to give an interface with web i want to access java method in jsp when certain menu button is clicked. How can i do this?

jmj
  • 237,923
  • 42
  • 401
  • 438
Taskin
  • 61
  • 1
  • 2
  • 9
  • Related: [How to use servlets and ajax?](http://stackoverflow.com/questions/4112686/how-to-use-servlets-and-ajax) – BalusC Jun 11 '12 at 16:31

2 Answers2

5

Using ajax (using jQuery.ajax, you could make a request to server, In your case may be to a Servlet which will invoke method on server that you requested

For example:

function callMe(){
    $.ajax({
      type: "POST",
      url: "/someServlet",
      data: { methodToInvoke: "sayHello" , data: "Abc" }
    }).done(function( msg ) {
      alert( "Data Saved: " + msg );
    });
}

at Servlet end

doPost(...){
 String methodToCall = request.getParameter("methodToCall");
 //do some stuff to determine method to call and call it like
 methodService.invoke(request.getParameter("data"));
}

Also See

jmj
  • 237,923
  • 42
  • 401
  • 438
1

you cannot do this directly because JSP is server side and html is client side. However, it can be accomplished via AJAX. http://en.wikipedia.org/wiki/Ajax_(programming)