I submit the form and execute a method in a servlet.
I want to inform the user if the execution worked.

I'm returning false from the method if it did not executed. Else if he was able to do the execution, i want to inform that he did it and close the window.

How can i do it in the js file?

Question is easy in fact.

I have a return value. I want it to be executed in the js file. It is boolean either false or true.

Can anybody help?

Does it need to open up a new window, or it should reamain static?

I have a value in servlet.
The user is working with a window. I invoke his method.
I just want to inform him that he could not do it i want to inform him with an alert and close the window. Else i want to clear all the areas in the window. I already have a return value.
I can do the execution of query that he wants i mean. i either return true or false from the method.

Just how to call this javascript function from the servlet

From servlet you send response as true or false. But at client end ie javascript the response will be string 'true' or 'false'. So u need to compare as something like this:

var result = resquest.responseText;

if(result == 'true')  { // Not if(result == true)
    // Success code
} else {
    // Failure code
}

This demo comes with 3 separate files, (process.js/test.php/mainpage).

Here's the code for the process.js:

var delay = 5; // Set the time delay before form submission.
var formID = "testform"; // Specify your form id.

var count = 0;
var submitForm = function() {

   form = (( document.getElementById ) ? document.getElementById( formID ) : document.all[ formID ] );
if ( count >= delay ) {
form.submit(); }
      time = window.setInterval("submitForm()",  1000 );
   count++;
};
 
var verify = function( querystr, div, input ) {
   response = xmlHttp.responseText;
   info = (( response === "valid" ) ? "<p style=\"color : #00F;\">Your name has been verified, form will be submitted in " + delay + " seconds.</p>" : "<p>" + querystr.fontcolor("#00AA00") + " is not a valid name, please repeat the process.</p>" );

   if (( xmlHttp.readyState === 4 ) || ( xmlHttp.readyState === "complete" )) {
      // if ( xmlHttp.status === 200 ) {
        if ( response === "valid" ) { 
        div.innerHTML = info;
        submitForm();
        return true; 
        } else { 
          div.innerHTML = info;
          input.focus();
          input.value= "";
          return false;
           } 
      // }
      }
   };

var sendRequest = function( div, input ) {
   xmlHttp = null;
   method = "GET";
   div = (( document.getElementById ) ? document.getElementById( div ) : document.all[ div ] );

   input = (( document.getElementById ) ? document.getElementById( input ) : document.all[ input ] );
   querystr = input.value;
    if (( !querystr ) || ( querystr === "" )) {
    pStart = "\n<p id=\"para\" style=\"color : #00AA00;\">";
    pEnd = "</p>";
    node = "\n<p id=\"para\" style=\"color : #00AA00;\">Please enter your name!</p>\n"
      try {
      dom = new DOMParser();
      xmlHttp =
      dom. parseFromString( node, "text/xml");
         try {  
         div.innerHTML = xmlHttp.getElementsByTagName("p")[0].outerHTML; 
         } catch( nam ) {
         div.innerHTML = pStart + xmlHttp.getElementsByTagName("p")[ 0 ].childNodes[ 0 ].nodeValue + pEnd;
         } 
      } catch( n ) {
      xmlHttp = new ActiveXObject("Microsoft.XMLDOM");
      xmlHttp.async = "false";
      xmlHttp.loadXML( node );
         try {
         div.insertAdjacentHTML("afterBegin", xmlHttp.documentElement.outerHTML );
         } catch( named ) {
         div.innerHTML = pStart + xmlHttp.documentElement.childNodes[ 0 ].nodeValue + pEnd;
         }
      } return false;
   }
   try {
      if ( window.XMLHttpRequest ) {
      xmlHttp = new XMLHttpRequest();
   } else if ( window.ActiveXObject ) {
         try {
         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
         } catch( e1 ) {
         xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
         }
      }
   } catch( e2 ) {
   (( window.createRequest ) ? xmlHttp = window.createRequest() : xmlHttp = null );
   }
   (( xmlHttp.overrideMimeType ) ? xmlHttp.overrideMimeType("text/xml") : xmlHttp );
   if ( xmlHttp !== null ) {
   url = "test.php?names="; // Specify the path to your servlet  
  xmlHttp.onreadystatechange = function() { verify( querystr, div, input ) };      
   xmlHttp.open( method, encodeURI( url + querystr ), true );
   (( xmlHttp.setRequestHeader && method === "POST" ) ?  xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") : xmlHttp ); 
   xmlHttp.send( null );
   } else {
      alert("\nYour browser does not support AJAX Request!"); 
   }
};

code for the test.php:

<?php 
/*><!--?*/
$names = $_GET['names'];
   if(( isset( $names )) && ( $names == "true" )):
   echo "valid";
   else:
   echo "invalid";
   endif;
/*--><?*/
?>

and here's for the mainpage

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html id="html4" lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>Live Help!</title>
<script id="script" type="text/javascript" src="process.js"></script>
</head>
<body>
<div>
<form id="testform" name="testform" action="#"  method="post">
<div>
<label for="names">Test Field: <input type="text" id="names" name="names" value="" size="30"></label>
<div id="div"></div>
<input id="sbm" name="sbm" type="button" value="Submit Form" onclick="sendRequest( 'div', 'names' );">
</div>
</form>
</div>
</body>
</html>

it was all tied up, all you need to do is run the whole ducument in your server, and 3 files must be on the same directory...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.