Thursday, 17 January 2013

Access JSP Map Field Values Without Using Names - 2

This is an example that demonstartes the use of JAVA pointers in order to handle JSPs genericly with includes...

Please read the comments carefully.

Kind Regards.

Ali R+
package test;
 * This is the super class from which all
 * the other classes must be extended.
 * @author Ali R+ SARAL
public class fieldVar {
    public static String val="";
<%@page import="java.util.*,*, test.*"%>
 * This jsp is submitted by the source jsp.
 * It takes all the field name/value pairs
 * and puts them into a hashmap.
 * It puts this hashmap into the pagecontext as
 * an attribute.  It forwards to the source jsp at the end.
 * @author Ali R+ SARAL
    Map<String, Object> map = new HashMap<String, Object>();//hashmap to hold the JSP map field name/val pairs
    for (Enumeration en = request.getParameterNames(); en.hasMoreElements();) {
        String name = (String) en.nextElement();
        String value = request.getParameter(name);
        fieldVar fv= new fieldVar();
        map.put(name, fv); //put each JSP map field name and val into the hashmap
        System.err.println(name + "=" + request.getParameter(name));
    pageContext.setAttribute("hm", map, pageContext.APPLICATION_SCOPE);  //put hashmap into the pageContext
    pageContext.forward("GZTKONA03.jsp");  //call the same JSP page
<%@page import="java.util.*,*,test.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 * This is the source jsp.  In the initial run, this jsp inits the SEC field and
 * ScreenInput3.jsp is submitted.  The fields of this jsp are extracted and put into
 * a hashmap by ScreenInput3.jsp . ScreenInput3.jsp submits back to this GZTKONA03.jsp .
 * In the second and consequent runs,  the variable handle hashmap is taken and
 * manipulated without using variable names.
 * @author Ali R+ SARAL
    String[] fieldNames = new String[10];
    fieldVar fv = new fieldVar();
    String messageVar="test msg";
    HashMap fieldvarMap=new HashMap();
    fieldvarMap.put("SEC", fv);
    HashMap fieldvalmap= (HashMap)pageContext.getAttribute("hm",4);
    if (fieldvalmap!=null)
         if (fieldvalmap.size()>0){
        System.out.println("hm not null not length 0");
        Object[] fields=fieldvalmap.keySet().toArray(); //get all the field names
        Object[] values=fieldvalmap.values().toArray(); //get all the field values
        System.out.print("\naccess the field vals by sequence");
        System.out.println("first field name="+fields[0]);
        fieldVar fv2 = (fieldVar) fieldvalmap.get(fields[0]); //get the value without using field name
        System.out.println("user entered first field val="+fv2.val);
        //fv2.val = "999";  //change the value without using field name
        System.out.println("user entered at end fv.val="+fv.val);
        System.out.print("\naccess the field vals by names written into the JSP");
        System.out.println("first static JSP field name="+fieldNames[0]);
        fieldVar fv3 = (fieldVar) fieldvalmap.get(fieldNames[0]); //get the value without using field name
        System.out.println("user entered first JSP field val="+fv3.val);
        fv3.val = "999";  //change the value without using field name
        System.out.println("\nprogrammatically changed to 999 at end fv3.val="+fv.val);
        System.out.println("fv.val changed indirectly at end fv.val="+fv.val);      
       else {
    System.out.println("GZTKONA03.jsp is opened");
       } else fv.val="9";  //init the input field
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Gazete Kontrol Programı Giriş</title>
        <form name="myform" action="ScreenInput3.jsp" method="POST">
            <!--*S**     '1.CIP IRSALIYE GIRISI' /--><br>
            1.CIP IRSALIYE GIRISI<br>
            <!--*S**     '2.CIP ISTEK GIRISI' /--><br>
            2.CIP ISTEK GIRISI<br>
            <!--*S**     '3.STAND IRSALIYE GIRISI'/--><br>
            3.STAND IRSALIYE GIRISI<br>
            <!--*S**     '4.ON OFIS IRSALIYE GIRISI' ///--><br>
            4.ON OFIS IRSALIYE GIRISI<br>
            <!--*S** 'SECIM:'#SEC--><br>
            SECIM: <input type="text" name="SEC" id="SEC" value="<%=fv.val%>"/><br />
            <br />
            <br />
            <br />
            <div id='message' ><%=messageVar%> </div> 
    System.out.println("end of GZTKONA03 screen");

First screen:

Second screen user entry before submit:

Third screen output:

INFO: Grizzly Framework 1.9.31 started in: 0ms - bound to []
INFO: end of GZTKONA03 screen
INFO: hm not null not length 0
INFO: access the field vals by sequence
INFO: first field name=SEC
INFO: user entered first field val=91
INFO: user entered at end fv.val=91
INFO: access the field vals by names written into the JSP
INFO: first static JSP field name=SEC
INFO: user entered first JSP field val=91
INFO: programmatically changed to 999 at end fv3.val=999
INFO: fv.val changed indirectly at end fv.val=999
INFO: end of GZTKONA03 screen