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?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Myth
  • 446
  • 7
  • 18
  • Duplicate of [Reference: Why does the PHP (or other server side) code in my Javascript not work?](http://stackoverflow.com/questions/13840429/reference-why-does-the-php-or-other-server-side-code-in-my-javascript-not-wor) – Quentin Oct 18 '13 at 10:00

1 Answers1

0

Please go through What is the difference between client-side and server-side programming? you will get idea about how it will not work.

If you want to execute that at the time of page submit You can do a following trick.

move those two methods to a servlet and on click of submit button check the value of txtPartyName and depending on the value of txtPartyName call the method as follow:

@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();
        } 
        ....
Community
  • 1
  • 1
Hemant Metalia
  • 29,730
  • 18
  • 72
  • 91
  • Actually I am currently using that in the case of singel funtion it works for me perfectly. But i want to use that condition as i shown in my question Here is my code which is working – Myth Oct 18 '13 at 10:12