title: Calling Servlet from JavaScript
body: <p>I intend to call a function in JavaScript which then calls a Servlet after an <code>&lt;input type="image"&gt;</code> is clicked.</p>
  
  <p><strong>JSP:</strong></p>
  
  <pre><code>&lt;head&gt;
      &lt;script type="text/javascript"&gt;
          function callServlet() {
              document.location.href="test-servlet.jsp";
          }
      &lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
      &lt;form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post"&gt;
      ...
      &lt;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!"&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p><strong>Servlet (<code>test-servlet.jsp</code>):</strong></p>
  
  <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      response.setContentType("text/html");
  
      PrintWriter out = response.getWriter();
  
      out.println("&lt;h1&gt;TestServlet called successfully!&lt;/h1&gt;");
  }
  </code></pre>
  
  <p><strong>Context Root:</strong>
  <code>http://localhost:8080/mysite/test-servlet.jsp</code><br><br>
  However, nothing happens when I click the image button. I am new to JavaScript.</p>