1

I intend to call a function in JavaScript which then calls a Servlet after an <input type="image"> is clicked.

JSP:

<head>
    <script type="text/javascript">
        function callServlet() {
            document.location.href="test-servlet.jsp";
        }
    </script>
</head>

<body>
    <form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
    ...
    <input type="image" name="submit"
        src="https://www.paypalobjects.com/webstatic/en_US/btn/btn_buynow_pp_142x27.png"
        onclick="callServlet()" alt="PayPal - The safer, easier way to pay online!">
    </form>
</body>

Servlet (test-servlet.jsp):

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");

    PrintWriter out = response.getWriter();

    out.println("<h1>TestServlet called successfully!</h1>");
}

Context Root: http://localhost:8080/mysite/test-servlet.jsp

However, nothing happens when I click the image button. I am new to JavaScript.

k_rollo
  • 5,304
  • 16
  • 63
  • 95
  • you can use jquery instead of using javascript. Is there any specific requirement that bounds you to use javascript only? – Rockstar Nov 03 '14 at 11:32
  • 1
    @Rockstar How would jQuery help with the question :)? http://meta.stackexchange.com/questions/45176/when-is-use-jquery-not-a-valid-answer-to-a-javascript-question – Pavel Horal Nov 03 '14 at 11:34
  • 2
    What are your intentions? The form is submitting on PayPal and you are trying to modify location when submit is clicked (**race condition**). Also changing the location would generate GET request, but your servlet is handling POST... so there are multiple issues with the code you presented. – Pavel Horal Nov 03 '14 at 11:36
  • so why not @PavelHoral ?. I think the OP wants to make a servlet call on click. – Rockstar Nov 03 '14 at 11:39
  • I'm sorry if i have taken it in a wrong way. Might be my poor understanding of the intention of the OP. – Rockstar Nov 03 '14 at 11:40

2 Answers2

1

Try this code

<a href="#" onclick="callServlet()"><img
    src="https://www.paypalobjects.com/webstatic/en_US/btn/btn_buynow_pp_142x27.png"
    alt="PayPal - The safer, easier way to pay online!"></a>

EDIT:

Finally we discovered that a servlet should be mapped without extension and doGet method is used to get the request from javascript.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • I guess he is trying to access the url `test-servlet` not the one in the action attribute – Santhosh Nov 03 '14 at 11:36
  • @SanKrish He wants an image button to go to a jsp page – Roman C Nov 03 '14 at 11:38
  • but his title suggests _Calling Servlet from JavaScript_ :( – Santhosh Nov 03 '14 at 11:39
  • @SanKrish Read below, *nothing happens when I click the image button*. – Roman C Nov 03 '14 at 11:40
  • @ohtph servlet or jsp ? it should be as `test-servlet` alone . try my answer if you need to be done using javascript – Santhosh Nov 03 '14 at 11:42
  • This is an image button that call a javascript function on click event. When it's executed you will be redirected to jsp page – Roman C Nov 03 '14 at 11:42
  • Sorry for causing confusion, my servlet is just mapped as `test-servlet.jsp`, it is not actually a jsp. – k_rollo Nov 03 '14 at 11:44
  • @ohtph poor mapping . dont try with those kinds of mapping it causes confusion. as jsp is a standard , you can go for some other patterns like .htm , .do like that – Santhosh Nov 03 '14 at 11:46
  • 1
    @RomanC the next line _I am new to JavaScript._ and even it wont accept the post request as he's trying to pass it in the post – Santhosh Nov 03 '14 at 11:47
  • 1
    You should map a servlet without extension and use `doGet` method. – Roman C Nov 03 '14 at 11:49
  • @RomanC, what I did was remove doPost(), put a sysout and a response.sendRedirect() to PayPal Sandbox in the TestServlet doGet(). I also mapped it as `testservlet` without extension. If you can post your comment as an answer, I will mark it accepted. Thanks. :) – k_rollo Nov 03 '14 at 12:07
  • @SanKrish, thank you also for your comment, it was also a clue so I upvoted it. I will also upvote your answer. Thanks again. – k_rollo Nov 03 '14 at 12:15
1

I Could see multiple errors in your jsp .

First of all ,

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">

And also use the img tag with button as Roman says

the url in the action is called , when your form is being submitted

so try replacing it with ,

<form action="./test-servlet" method="post">

and using your JavaScript now,

You cant use window.location.href to make a POST request . check pass post data with window.location.href

<script type="text/javascript">
    function callServlet() {
        document.forms[0].submit;
    }
</script>
Community
  • 1
  • 1
Santhosh
  • 8,181
  • 4
  • 29
  • 56
  • Hi, the `https://www.sandbox.paypal.com/cgi-bin/webscr` redirects to the PayPal Sandbox site which is required. The reason I want to call another test-servlet is for it to set a boolean `isPaypalButtonClicked`. Is that possible? – k_rollo Nov 03 '14 at 11:48
  • so you want it to stay on the same page ?if so use ajax to do the work for you – Santhosh Nov 03 '14 at 11:49
  • Sadly, we are not allowed to touch AJAX as of yet. jQuery and JS is allowed (but minimal). – k_rollo Nov 03 '14 at 11:53
  • 1
    so you can call the servlet , set the values to the request and re-direct back to the jsp. – Santhosh Nov 03 '14 at 11:54