Skip to content Skip to sidebar Skip to footer

Call Jsp Method From Javascript

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 c

Solution 1:

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');

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();
}

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();}
%>

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

Solution 2:

Use form submission or ajax request or framework action to go back end and process the things needed for you.In your approach its not possible I think. And please dont use scriplets in jsp,its not a good practice.

Post a Comment for "Call Jsp Method From Javascript"