从javascript调用JSP方法 [英] Call JSP method from javascript

查看:254
本文介绍了从javascript调用JSP方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有上面的js和JSP代码,我想在确认窗口poopup(在"if"语句内)上单击ok按钮时调用procedureCall(). 有可能吗?如果可以,怎么办?

I have the above js and JSP code, I want to invoke procedureCall() while clicking on ok button on confirm window poopup (inside "if" statement). Is it possible and if yes, how it can be done?

<script type="text/javascript">
        function confirmDelete() {
            var answer = confirm("Are you sure you want to delete");
            if (answer == true) {
                return true;
            } else {
                return false;
            }
        }
</script>

JSP片段:

<%!    public void procedureCall(int cId) {
            String procedureCall = "{call NEW_PORTING_PRC.delete_album_metadata(cId)}";
                try (CallableStatement cal = conn.prepareCall(procedureCall);) {

                } catch (Exception e) {
                    e.printStackTrace();
                }
%>

推荐答案

javascript在您的浏览器中运行,您的Java代码部署在您的容器(Tomcat)中. 因此,调用此方法的唯一方法是通过Web调用.您的javascript应该调用对一个servlet(在web.xml中配置)的ajax调用,并且该servlet应该调用您的java方法. 就像您写成JS一样

javascript runs in your browser, your java code is deployed in your container(Tomcat). So only way to invoke this is via a Web call. Your javascript should invoke an ajax call to one servlet(configured in web.xml) and this servlet should invoke your java method. like from JS you write as

 $.get('URLofServlet');

您可以尝试的是使用一些参数提交JSp并重新加载JSP.

OR what you can try is submit the JSp with some parameters and reloading the JSP..

if (answer == true) 
{
   document.getElementById("param1").value='paramvalue';
   document.getElementById("myForm").action='yourJspPath.jsp';
   document.getElementById("myForm").submit();
}

为了简单起见,我还假定id和name均为'param1'. 而在scriptlet中,您可以喜欢

Also I assume id and name will both be 'param1' for simplicity.. and in scriptlet you can do like

<%
    if(request.getParamter("param1")=='paramvalue'){procedureCall();}
%>

仅需添加,我将建议删除scriplets并使用ajax作为第一种方法.

Just to add I will recommend to remove scriplets and use ajax as in first approach.

这篇关于从javascript调用JSP方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆