Cybersecurity
DevOps Cloud
IT Operations Cloud
/** react on the changes in the text fields and call Ajax
*/
function registerChange( form, field )
{
try
{
var fldValue = field.getValue();
if ( fldValue == "" ) return;
ajax_call_jquery( form, '/ncsAJQ/ajax/test.jsp' );
}
catch( e )
{
form.showError( e );
}
}
/** collect the data that we want to send to the service, then call Ajax
*/
function ajax_call_jquery( form, url )
{
try
{
var inputData = new Object();
inputData[ "data1" ] = form.getValue( "fldText1" );
inputData[ "data2" ] = form.getValue( "fldText2" );
// call to jQuery Ajax
$.ajax( {
type: 'GET',
url: url,
data: inputData,
success: function( data )
{
ajax_display( form, url, data );
},
error: function( xhr, status )
{
ajax_display( form, url, xhr " / " status );
},
timeout: 9000
} )
}
catch( e )
{
form.showError( e );
}
}
<%@page import="java.io.StringWriter"%>
<%@ page language="java"
contentType="application/json;"
pageEncoding="ISO-8859-1"
import="java.net.*, java.io.*, java.util.*, org.json.simple.*"
%><%
try
{
// for JSON encoding/decoding see: http://code.google.com/p/json-simple/wiki/EncodingExamples
// read data1 sent from the client
String data1 = request.getParameter( "data1" );
System.out.print( "NCS: data1=" data1 );
// read data2 sent from the client
String data2 = request.getParameter( "data2" );
System.out.print( "NCS: data2=" data2 );
// return some dummy data in various formats
Map obj = new LinkedHashMap();
obj.put( "jsp", "test2.jsp" );
obj.put( "time", ( new Date()).toString() );
obj.put( "data1", data1 );
obj.put( "data2", data2 );
obj.put( "test1", "foo" );
obj.put( "test2", new Integer( 100 ) );
obj.put( "test3", new Double( 1000.21 ) );
obj.put( "test4", new Boolean( true ) );
obj.put( "test5", null );
// add a more complex result type (list)
JSONArray list1 = new JSONArray();
list1.add( "foo" );
list1.add( new Integer( 100 ) );
list1.add( new Double( 1000.21 ) );
obj.put( "test6", list1 );
// convert all data to JSON
StringWriter stringWriter = new StringWriter();
JSONValue.writeJSONString( obj, stringWriter );
String jsonText = stringWriter.toString();
// return the JSON string
System.out.print( "NCS: result=" jsonText );
out.write( jsonText );
}
catch ( Exception e )
{
System.out.print( "NCS: e=" e.toString() );
out.write( "{[ " e.toString() " ]}" );
}
%>
/** this event handler is called on return from the Ajax call
*
* evaluate/display server response
*/
function ajax_display( form, url, data )
{
if ( data )
{
form.setValues( "fldResult", dojo.toJson( data ) );
if ( data.time ) alert( "Server date/time: " data.time );
}
}