Rabindra
Rabindra

Reputation: 101

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 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 piece:

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

Upvotes: 1

Views: 5980

Answers (2)

Viraj Nalawade
Viraj Nalawade

Reputation: 3225

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.

Upvotes: 2

Prasanna Kumar H A
Prasanna Kumar H A

Reputation: 3431

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.

Upvotes: 0

Related Questions