How can I invoke a servlet from JavaScript?

Nicola Ken Barozzi

Yes, and it is quite easy. You just have to remember that a servlet is seen by the browser like a normal web page with its URL. Just call it lke you would call a web page. It is also possible to use servlet data in javascript without changing page. For example I am using Microsoft XML data islands that get XML data from a servlet.
CODE:

<HTML>

  <SCRIPT>
    function display()
    {
      data.transformNodeToObject(ss.XMLDocument, resultTree.XMLDocument);
      document.write(resultTree.xml);
    }
       </SCRIPT>
  
  <SCRIPT FOR="window" EVENT="onload">
    data.async = false;
    data.load("../servlet/SQLResult");
    ss.async = false;
    ss.load("MyStyleSheet.xsl");
    display();
  </SCRIPT>
  
  <XML id="data"></XML>
  <XML id="ss"></XML>
  <XML id="resultTree"></XML>


</HTML>
Here "../servlet/SQLResult" is a servlet URL.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact