how to call a servlet from JavaScript?


Web tier: servlets, JSP, Web frameworks: how to call a servlet from JavaScript?

  1. how to call a servlet from JavaScript? (4 messages)

       How can I call a servlet(with two parameters) from JavaScript except using submit a form.

        Thank you!
  2. how to call a servlet from JavaScript?[ Go to top ]

    javascript lives on the browser, servlets live on the server. javascript cannot call a servlet directly, the page must be submitted to the server.
  3. You can call a servlet from client side javascript using code like this :

    function loadservlet_onclick() {
      document.location.href = "servlets/com.myproj.myServlet?param1=123&param2=abc";

    In this snipped of code the loadservlet_onclick() javascript function is calling a servlet named myServlet that is in a package com.myproj, it pass 2 parameters to servlet.

    The idea is that location.href takes a URL, so it can be a static html page, jsp or servlet.

    In a stateless web environment all resources reside on server side, html, jsp, servlet, images. You can call any resource from client side javascript without using post.
  4. Thank for your help! Based on your suggestion, it can call a servlet but it generates a new page. In my case, I need to call a servlet to clear some session INFO in the session
    object without generating any new page. So how do I handle this except using SUBMIT method. Thank you!!!
  5. how to call a servlet from JavaScript?[ Go to top ]

    In regards to being able to call a servlet from other than a SUBMIT. You can do an ONCLICK="Some Javascript Function", do some data manipulation and based on the outcome, do a document.formName.submit(); Hope that it helps....I am doing this now but I am not binding anything to the request object which is very frustrating... I'll write again if I figure it out.... Or, if anyone else could point me in the right direction that would be awesome too.... Thanks...