最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Calling Servlet from JavaScript - Stack Overflow

programmeradmin1浏览0评论

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="" method="post">
    ...
    <input type="image" name="submit"
        src=".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.

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./cgi-bin/webscr" method="post">
    ...
    <input type="image" name="submit"
        src="https://www.paypalobjects./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.

Share Improve this question edited May 8, 2015 at 1:23 k_rollo asked Nov 3, 2014 at 11:26 k_rollok_rollo 5,47217 gold badges68 silver badges97 bronze badges 5
  • you can use jquery instead of using javascript. Is there any specific requirement that bounds you to use javascript only? – Rockstar Commented Nov 3, 2014 at 11:32
  • 1 @Rockstar How would jQuery help with the question :)? meta.stackexchange./questions/45176/… – Pavel Horal Commented Nov 3, 2014 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 Commented Nov 3, 2014 at 11:36
  • so why not @PavelHoral ?. I think the OP wants to make a servlet call on click. – Rockstar Commented Nov 3, 2014 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 Commented Nov 3, 2014 at 11:40
Add a ment  | 

2 Answers 2

Reset to default 1

Try this code

<a href="#" onclick="callServlet()"><img
    src="https://www.paypalobjects./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.

I Could see multiple errors in your jsp .

First of all ,

<form action="https://www.sandbox.paypal./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>
发布评论

评论列表(0)

  1. 暂无评论