How to call a java method from jsp by clicking a menu in html page?

后端 未结 2 912
滥情空心
滥情空心 2020-12-07 05:52

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?

相关标签:
2条回答
  • 2020-12-07 06:16

    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)

    0 讨论(0)
  • 2020-12-07 06:30

    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

    • DWR
    0 讨论(0)
提交回复
热议问题