weixin_33725807 2019-05-30 06:55 采纳率: 0%
浏览 80

AJAX调用Java方法

There are many similar questions but I am not clear about one thing as mentioned below-

I have ajax call

var url = '/test/testjsp.do?param1=' + xyz;    
$.ajax({
                    type:'GET',
                    dataType:'html',
                    url:url,
                    success:function (data) {
                        alert("Success");
                    }
                });

which is calling an JSP page and in that file, I am calling method from SM class

<%@ page import="com.testAjax.SM" %>

<%

    if (null != request.getParameter("
        SM.randomMethod(request.getParameter("param1"));
    }
%>

So my question is ,

I there any alternative method where I don't have to create extra Jsp file and call java method directly from Ajax call

Please help and Advise

  • 写回答

1条回答 默认 最新

  • weixin_33681778 2019-05-30 07:33
    关注

    As javascript is a client side script, it cannot invoke java methods directly which resides on the server.

    To do that you have to create a web service or jsp like you did.

    But I have seen vaadin gives option to call java code from javascript without writing a service or jsp. I dont know how it will help you.

    https://vaadin.com/tutorials/calling-java-from-javascript

    There are some concept like java adaptors and javavm to combine javascript and java try them to find it suits your need.

    评论

报告相同问题?