1

I have two JSP scriptlet function as follows

    <%!
          public void method1(){
    System.out.println("method one");
}
%>

     <%!
          public void method2(){
    System.out.println("method two");
}
%>

I want to call the functions after checking a condition on button click event using JAVASCRIPT like below

   <script type="text/javascript">          
            $('#btnSubmit').click(function(e) {   
                var partyid=$("#txtPartyName").val();
               if(partyid==1){
                <%method1();%>
              }
             else{
               <%method2();%>              
             }
                });
        </script>

Is this possible or is there any other way to achieve this functionality?

4

1 回答 1

0

请通过客户端和服务器端编程有什么区别?你会知道它是如何不起作用的。

如果您想在页面提交时执行该操作,您可以执行以下技巧。

将这两个方法移动到 servlet 并单击提交按钮检查 txtPartyName 的值,并根据 txtPartyName 的值调用如下方法:

@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        MyClass myClass = new MyClass();

        if (request.getParameter("txtPartyName") == "1") {
            myClass.method1();
        } else {
            myClass.method2();
        } 
        ....
于 2013-10-18T10:05:17.897 回答