Tuesday, 15 January 2013

Access JSP Map Field Values Without Using Names

The purpose of this short article is to access the JSP map field vars automatically without using their names.  I will first show how to access vars without using their names in JAVA, namely using JAVA pointers.  Then, in the next blog articles I will put this solution into use with JSP examples.

Cheers.

Ali R+

accessMapFieldValuesWithoutNames.java
-------------------------------------
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Vector;
/*
 * Purpose: To access JSP map field values automatically without using their names.
 *          We would like to access the values of the map fields by using pointers.
 *
 * Implementation :The key kere is to keep the field values in an object.  We put
 *               the object in a hashmap.
 */
/**
 *
 * @author Ali Riza SARAL
 */
public class accessMapFieldValuesWithoutNames {
    public static void main(String[] args) {
        Map<String, Object> hmVarHolder = new HashMap<String, Object>();  // create a hashmap to hold vars

        varClass var1 = new varClass();  // create an instance of var object
        var1.value = "var1Val";  // put a value into the new var
        System.out.println("initial var1 value=" + var1.value);
        hmVarHolder.put("var1", var1); //put the new var into the var holder
       
        //------------------------------------------------------
       
        varClass var2 = (varClass) hmVarHolder.get("var1"); // create a pointer to the var1
        System.out.println("var1.value copied to var2.value=" + var2.value); // var2 points at var1
        var2.value = "newVar2value";     // change the value of var2 /change the (pointed) value of the var2
        System.out.println("\nThe newly assigned var2.value=" + var2.value);
        System.out.println("The original value is changed by var2 ---> var1.name=" + var1.value); // the value of var1 changes also
        //------------------------------------------------------
       
        varClass var3 = (varClass) hmVarHolder.get("var1");
        System.out.println("var1.value copied to var2.value=" + var3.value); // var3 points at var1
        var3.value = "newVar3value";     // change the value of var3
        System.out.println("\nThe newly assigned var3.value=" + var3.value);
        System.out.println("The original value is changed by var3 ---> var1.name=" + var1.value); // the value of var1 changes also
        System.out.println("The var2 value is changed by var3 ---> var2.name=" + var2.value); // the value of var2 changes also
    }
    public static class varClass {
        String value = "";
    }
}
The output:
-----------
run:
initial var1 value=var1Val
var1.value copied to var2.value=var1Val
The newly assigned var2.value=newVar2value
The original value is changed by var2 ---> var1.name=newVar2value
var1.value copied to var2.value=newVar2value
The newly assigned var3.value=newVar3value
The original value is changed by var3 ---> var1.name=newVar3value
The var2 value is changed by var3 ---> var2.name=newVar3value
BUILD SUCCESSFUL (total time: 0 seconds)