0

In my jsp page I have a jsp button like this.

     <input class="submit_button" type="submit" name="Payment" value="Payment"          
     style="position: absolute; left: 350px; top: 130px;" 
     onclick="javascript:payment();">

And I have onclick javascript function which call the jsp method "callProcedure()" like this.

<script>
     function payment(){
     alert('Payment done successfully...');
    <%                  
      callProcedure();  // calls the procedure                       
    %>
</script>

And the jsp method callProcedure() calls the oracle procedure like this

<%! void callProcedure() {
    CallableStatement cst = null;
    Connection conn = null;      

    try {

        DBDataSource dbDataSource = new DBDataSource();
        conn = dbDataSource.makeConnection();
        conn.setAutoCommit(false);           
        String insertStoreProc = "{call 
        p_webservice_test(?,?,?,?,?,?,?,?,?,?,?,?,?,?)}";
        cst = conn.prepareCall(insertStoreProc);

        cst.setString(1, TransID);
        cst.setString(2, RemitNo);
        cst.setString(3, senderFName + senderMName + senderLName);
        cst.setString(4, RFName + RMName + RLName);
        cst.setString(5, RAddr);
        cst.setString(6, RPh);
        cst.setString(7, Relshp);
        cst.setString(8, tDate.toString());
        cst.setString(9, PayCur);
        cst.setBigDecimal(10, paymentAmt);
        cst.setString(11, Pcode);
        cst.registerOutParameter(12, Types.VARCHAR);
        cst.executeUpdate();  
        conn.commit();

    } catch (SQLException e) {
        System.out.println("Error:::" + e.getMessage());
    } finally {
        if (cst != null) {
            try {
                cst.close();
            } catch (Exception ex) {
                System.out.println("Error:" + ex.getMessage());
            }
        }
    }
}
%>

Here, my problem is that the jsp method "callProcedure()" execute before clicking the button or before executing the javascript method. Whats the exact problem and the solution for this problem. Please reply as soon as possible.

Thanks In Advance...

3
  • All JSP is run on the server so you cant call a jsp method on a javascript event. Commented May 17, 2013 at 6:38
  • what should I do to call the jsp method on jsp button click? Commented May 17, 2013 at 6:42
  • Take a look at my edited answer. Commented May 17, 2013 at 6:48

3 Answers 3

2

All JSP is run on the server before even loading the page. What you see is the output of the jsp page. So you cant call a jsp method on a javascript event. Here in your case the jsp method is executing on the server and the result is shown to you.

You can put the code of the callProcedure() method in some servlet and then using the input button you can submit the form to the servlet, which will call the callProcedure() method and show the result n the page

One more thing I haven't seen or heard about the JSP button as you have specified in the post.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot. But can the value of jsp page used in procedure (like TransID, RemitNo) be passed to the servlet?
@Suniel: If these attributes are unique for each user you can store them as session attributes and then the servlet can get these attributes from the session.
I tried but it gives null values. Can i call the java method of jsp page in servlet?
As NK123: has suggested its bad to right java code in your jsp, so try to move your code to either a servlet or a bean (simple java class). Before calling any method, you need to have it defined or accessible to where you are calling it. So, I would say move the JSP code to the servlet. Also for the null value that you are getting, make sure you add the objects to the session and you are using the same session to access those variables.
1

Avoid direct calling methods in javascript redirect to servlets and call your callProcedure() method.

 location.href=/packagename/ServletName'

my suggestion writting java code in jsp not a good practice.

Comments

1

My suggestion is call the servlet by clicking on button. Inside that servlet call your procedure and return the result(if any result exist) by session to the same page.

Change the submit button to only button .

<input class="submit_button" type="button" name="Payment" 
     value="Payment"          
     style="position: absolute; left: 350px; top: 130px;" 
     onclick="javascript:payment();">

The javascript function like below :-

<script language="javascript">

 function payment(){
   document.formname.action="<%=request.getContextPath()%>/servletpath/myservlet";
   document.formname.submit();
}
</script>

Hope it will help you.

2 Comments

Can I call the method of jsp page in the servlet?
Better , you define that jsp method inside servlet and return the result to you JSP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.