JSP - SERVLET - 从servlet调用javascript函数

时间:2022-12-28 16:43:14

i have a simple JSP page that calls a servlet on a click of a button and then the servlet call some internal methods to perform a task and then the time comes when the servlet action comes to an end and the goal now is, at the end of the servlet function, i want to return the control to the jsp page and soon execute a java script (there will be no event triggered, as soon as the control comes to teh jsp, the Javascript should run). how can i achieve this. as far as i have googled, it is impossible to call a javascript without any event(like a click, hover, etc) being triggered. is there a way to achieve my goal?

我有一个简单的JSP页面,只需单击一个按钮即可调用servlet,然后servlet调用一些内部方法来执行任务,然后是servlet操作结束时的时间,现在的目标是,最后对于servlet函数,我想将控件返回到jsp页面并很快执行一个java脚本(没有事件触发,一旦控件到达jsp,Javascript就应该运行)。我怎么能实现这一目标。就我用Google搜索而言,如果没有任何事件(如点击,悬停等)被触发,则无法调用javascript。有没有办法实现我的目标?

UPDATE

<form action="http://localhost:90/Jdfront1/hello" onsubmit="return validate()">

<label>Topic :</label><input type="text" name="topic" id="topic"/><br>
<label>Number of pages :</label><input type="text" name="Nopages" id="Nopages"/><br>
<label>URL :</label><input type="text" name="link" id="link"/><br>
<label>isRange :</label><input type="text" name="range" id="range"/><br>
<input type="submit" value="click here!!" >

this is the form i have.

这是我的形式。

1 个解决方案

#1


0  

Use AJAX to invoke the Servlet, then use the success handler to invoke your JS function.

使用AJAX调用Servlet,然后使用success处理程序调用JS函数。

You can use jQuery to easily do this:

您可以使用jQuery轻松执行此操作:

$("#button-id").click(function() {
    $.ajax({
        url: "/url?topic="+$('#topic').val()+'&Nopages='+$('#Nopages').val()+'&link='+$('#link').val()+'&range='+$('#range').val(),
    })
    .done(function( data ) {
        // Invoke your function here
        validate();
    });
});

See https://api.jquery.com/jQuery.ajax/ for more details.

有关详细信息,请参阅https://api.jquery.com/jQuery.ajax/。

Server-side, you can use http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameter(java.lang.String) to get the query string parameters and process them.

在服务器端,您可以使用http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameter(java.lang.String)来获取查询字符串参数并对其进行处理。

#1


0  

Use AJAX to invoke the Servlet, then use the success handler to invoke your JS function.

使用AJAX调用Servlet,然后使用success处理程序调用JS函数。

You can use jQuery to easily do this:

您可以使用jQuery轻松执行此操作:

$("#button-id").click(function() {
    $.ajax({
        url: "/url?topic="+$('#topic').val()+'&Nopages='+$('#Nopages').val()+'&link='+$('#link').val()+'&range='+$('#range').val(),
    })
    .done(function( data ) {
        // Invoke your function here
        validate();
    });
});

See https://api.jquery.com/jQuery.ajax/ for more details.

有关详细信息,请参阅https://api.jquery.com/jQuery.ajax/。

Server-side, you can use http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameter(java.lang.String) to get the query string parameters and process them.

在服务器端,您可以使用http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameter(java.lang.String)来获取查询字符串参数并对其进行处理。