Ajax und JSon

With Ajax one can create a dynamic HTML-Page, without load the complete page.
  1. globale Variable im JavaScript-Abschnitt
    • var xmlhttp = new XMLHttpRequest();
    • xmlhttp.onreadystatechange = readajax;
  2. Init-Methode, die von einem Event aufgerufen wird
    • function testajax( form ) {..}
  3. Callback-Function, die vom XMLHttpRequest-Object aufgerufen wird
    • status hat die Werte 0 bis 4
    • status=4: Nachricht von Server ist angekommen


1. PHP Sample

2. PHP Sample

1. JSP Sample

2. JSP Sample

The NEW JSon-Object


PHP Samples

JSP Samples

The NEW JSon-Object

If you want to use the nirmal JSon-Object, you must insert a jar-file. Then you use the put methods you create the JSon-String.
But this is not necessary. You better use my Json-Object-family new JSon-Object.

This methods used the reflection in java. With the reflection, one can print the name of the an object, all methods, all variables and you can get or set all values of the attribs. My Json-Object-family ist very powerful and very easy to use.

You need at least one method:
private String getJSon(Object obj)

This method create with one call the complete String!

If you have an Array or an ArrayList, you can use one og the following methods:
· private String getJSonArray(Object[] objs)
· private String getJSonArraylist(ArrayList objs)

These methods get all objects in the arrays and create the JSon-String:
[ {"name":"Miller", "mnr":12345},{"name":"Smith", "mnr":12346} ]

private String getJSon(Object obj) {
     StringBuilder sb = new StringBuilder();
     try {     
       Class c = obj.getClass();  // create a "class" from the object
       Field[] fields; 
       fields = c.getDeclaredFields();  // get the attribs
        // create the JSon-String: {  {"name": "Meier59", "mnr":12345} 
       int n=fields.length-1;  // need for the last comma
       for(int i=0; i<n; i++) {
         Field field = fields[i];
         sb.append("\": ");  // if a String: the value has two 'double quotation marks'
         String s=field.getType().toString();  // language.String....
         if (s.indexOf("String")>0) {
            sb.append(field.get(obj));  // get the value
         else {
         if (i<n-1) {
            sb.append(", ");   // get the value
     catch ( IllegalAccessException e) {
     return sb.toString();

Literatur AJax und JSon

AJAX. schnell + kompakt Taschenbuch
von Christian Wenz
ISBN-10: 3935042922
ISBN-13: 978-3935042925

Klaus Zeppenfeld, Hassan El Moussaoui

Ajax: Grundlagen, Frameworks und Praxislösungen
von Stefan Mintert,‎ Christoph Leisegang
Verlag: dpunkt;
ISBN-10: 3898644049
ISBN-13: 978-3898644044

Druckbare Version

1. PHP Sample