Resources

SAS® AppDev Studio 3.0 Developer's Site

Form Elements: Client-Side Validation   About It Build It  

Data Source: JDBC

This example has two prerequisites:

Please install the latest webAF template updates prior to building this example. For more information about the server-side example templates used by this example, see Web Application Example Templates and Built-in Web Application Templates and Options.

The following example is not meant to be a complete Web application, rather it is to show how to use a particular component(s). The example does not address the issue of immediately freeing up resources when the user navigates off the Web application or closes the Web browser. Any necessary resources created in the example will stay around until the associated HTTPSession times out. If this example is used in a multi-user environment, it is possible to exhaust the available resources until HTTPSessions time out and free up their associated resources.

Prerequisite: Create the sasuser.houses data set

In order for this example to work properly, you must create the sasuser.houses data set. Open SAS and copy the following code into the editor window. Submit the statements from within SAS to create the sasuser.houses data set.

data sasuser.houses(label = 'Residential housing for sale' );
   input style $ 1-8 sqfeet 10-13 bedrooms 15
         baths 17-19 street $ 21-36 price 40-45;
   label style    = "Style of homes"
         sqfeet   = "Square footage"
         bedrooms = "Number of bedrooms"
         baths    = "Number of bathrooms"
         street   = "Street address"
         price    = "Asking price";
   format price dollar12.;
   informat price comma12.;
   cards;

RANCH    1250 2 1   Sheppard Avenue     64000
SPLIT    1190 1 1   Rand Street         65850
CONDO    1400 2 1.5 Market Street       80050
TWOSTORY 1810 4 3   Garris Street      107250
RANCH    1500 3 3   Kemble Avenue       86650
SPLIT    1615 4 3   West Drive          94450
SPLIT    1305 3 1.5 Graham Avenue       73650
CONDO    1390 3 2.5 Hampshire Avenue    79350
TWOSTORY 1040 2 1   Sanders Road        55850
CONDO    2105 4 2.5 Jeans Avenue       127150
RANCH    1535 3 3   State Highway       89100
TWOSTORY 1240 2 1   Fairbanks Circle    69250
RANCH     720 1 1   Nicholson Drive     34550
TWOSTORY 1745 4 2.5 Highland Road      102950
CONDO    1860 2 2   Arcata Avenue      110700
run;

Prerequisite: Create the sasuser.states data set

In order for this example to work properly, you must create the sasuser.states data set. Open SAS and copy the following code into the editor window. Submit the statements from within SAS to create the sasuser.states data set.

data sasuser.states(label = 'US States');
	input name $ 1-20 code $ 22-23;
	label name = "State Name"
		  code = "State Code";
	cards;
Alabama              AL
Alaska               AK
Arizona              AZ
Arkansas             AK
California           CA
Colorado             CO
Connecticut          CT
District of Columbia DC
Delaware             DE
Florida              FL
Georgia              GA
Hawaii               HI
Iowa                 IA
Idaho                ID
Illinois             IL
Indiana              IN
Kansas               KS
Kentucky             KY
Louisiana            LA
Massachusetts        MA
Maryland             MD
Maine                ME
Michigan             MI
Minnesota            MN
Missouri             MO
Mississippi          MS
Montana              MT
Nebraska             NE
New Hampshire        NH
New Jersey           NJ
New Mexico           NM
New York             NY
Nevada               NV
North Carolina       NC
North Dakota         ND
Ohio                 OH
Oklahoma             OK
Oregon               OR
Pennsylvania         PA
Rhode Island         RI
South Carolina       SC
South Dakota         SD
Tennessee            TN
Texas                TX
Utah                 UT
Virginia             VA
Vermont              VT
Washington           WA
Wisconsin            WI
West Virginia        WV
Wyoming              WY
;
run;

Step 1: Create a project in webAF

  1. Create a new project named Form.
  2. Select Web Application from the webAF Projects list.
  3. Accept the defaults as you go through the Web Application wizard until you have reached step #4 of the wizard. At this step you will need to select Examples in the radio box titled Display list for. Choose JDBC Default Servlet from the list box titled Type of initial content.
  4. Continue accepting defaults as you complete the Web Application wizard.

Step 2: Set up access to the data source

  1. Open the JDBCDefaultExampleControllerServlet.java file from the Files Tab of the Project Navigator.
  2. Add the following imports:
         import com.sas.storage.jdbc.JDBCToComboBoxModelAdapter;
         import javax.swing.DefaultComboBoxModel;
         import javax.swing.ComboBoxModel;
         import com.sas.servlet.tbeans.models.JavaScriptNode;
         import java.sql.*;
         import java.util.Map;
    
  3. Add the following global Web application JDBC variables:
    	private static final String STATE_ADAPTER = "state_adapter";
    	private static final String STATE_MODEL = "state_model";
    	private static final String STYLE_MODEL = "style_model";
    	private static final String BATH_MODEL = "bath_model";
    	private static final String BED_MODEL = "bed_model";
    
  4. Modify the jdbcQuery string and add the following query string variables as follows:
    	String jdbcQuery = "select * from sasuser.houses";
    	String stateComboQuery = "select * from sasuser.states";
    	String jdbcStyleQuery = "select distinct style from sasuser.houses";
    	String jdbcBedQuery = "select distinct bedrooms from sasuser.houses";
    	String jdbcBathQuery = "select distinct baths from sasuser.houses";
    
  5. Replace the line "JDBCAdapter adapter = null;" that's located after the line that reads "//Setup the JDBC model adapter" with the following:
    	JDBCToComboBoxModelAdapter adapter = null;
    	DefaultComboBoxModel adapter2 = new DefaultComboBoxModel();
    	DefaultComboBoxModel newStateModel = new DefaultComboBoxModel();
    	JDBCToComboBoxModelAdapter stateComboAdapter = null;
    	JDBCToComboBoxModelAdapter styleComboAdapter = null;
    	JDBCToComboBoxModelAdapter bathComboAdapter = null;
    	JDBCToComboBoxModelAdapter bedComboAdapter = null;
    
  6. Replace the contents of the "if (session != null)" statement with the following:
    	adapter = (JDBCToComboBoxModelAdapter)session.getAttribute(SAS_MODEL);
    	stateComboAdapter = (JDBCToComboBoxModelAdapter)session.getAttribute(STATE_ADAPTER);
    	newStateModel = (DefaultComboBoxModel)session.getAttribute(STATE_MODEL);
    	styleComboAdapter = (JDBCToComboBoxModelAdapter)session.getAttribute(STYLE_MODEL);
    	bedComboAdapter = (JDBCToComboBoxModelAdapter)session.getAttribute(BED_MODEL);
    	bathComboAdapter = (JDBCToComboBoxModelAdapter)session.getAttribute(BATH_MODEL);
    
  7. Replace the "if(adapter == null)" statement with the following:
    if (adapter == null){
    	try{
    		//Add code below to create the model adapter.
                     //See the com.sas.storage.jdbc package in the API for more details.
    		//INSERT_MODEL_ADAPTER_CODE_HERE
    
    		adapter = new JDBCToComboBoxModelAdapter(sas_JDBCConnection, jdbcQuery);
    		//Set it on the session
    		if (session != null){
    			session.setAttribute(SAS_MODEL, adapter);
    		}
    	}
    	catch(Exception e){
    		throw new RuntimeException(e);
    	}
    }
    
  8. Add the following if statements after the "if (adapter == null)" statement:
    if (stateComboAdapter == null){
       try{
             //Add code below to create the model adapter.
             //See the com.sas.storage.jdbc package in the API for more details.
                         //INSERT_MODEL_ADAPTER_CODE_HERE
    		     stateComboAdapter =
                                 new JDBCToComboBoxModelAdapter(sas_JDBCConnection, stateComboQuery);
    
    			if(newStateModel == null)
    				newStateModel = new DefaultComboBoxModel();
    
    			for(int i=0; i < stateComboAdapter.getSize(); i++)
    			{
    			 	JavaScriptNode node = new JavaScriptNode();
    				node.setText((String)stateComboAdapter.getElementAt(i));
    				newStateModel.addElement(node);
    			}
             //Set it on the session
             if (session != null){
                 session.setAttribute(STATE_ADAPTER, stateComboAdapter);
    	        	session.setAttribute(STATE_MODEL, newStateModel);
             }
       }
       catch(Exception e){
          throw new RuntimeException(e);
       }
    }
    if (styleComboAdapter == null){
       try{
             //Add code below to create the model adapter.
              //See the com.sas.storage.jdbc package in the API for more details.
    			//INSERT_MODEL_ADAPTER_CODE_HERE
    			styleComboAdapter =
                                 new JDBCToComboBoxModelAdapter(sas_JDBCConnection, jdbcStyleQuery);
    
             //Set it on the session
             if (session != null){
                 session.setAttribute(STYLE_MODEL, styleComboAdapter);
    			}
       }
       catch(Exception e){
          throw new RuntimeException(e);
       }
    }
    if (bedComboAdapter == null){
       try{
             //Add code below to create the model adapter.
              //See the com.sas.storage.jdbc package in the API for more details.
    	    	//INSERT_MODEL_ADAPTER_CODE_HERE
    			bedComboAdapter =
                                 new JDBCToComboBoxModelAdapter(sas_JDBCConnection, jdbcBedQuery);
    
             //Set it on the session
             if (session != null){
                 session.setAttribute(BED_MODEL, bedComboAdapter);
    	        }
       }
       catch(Exception e){
          throw new RuntimeException(e);
       }
    }
    if (bathComboAdapter == null){
       try{
             //Add code below to create the model adapter.
              //See the com.sas.storage.jdbc package in the API for more details.
    	    	//INSERT_MODEL_ADAPTER_CODE_HERE
    			bathComboAdapter =
                                 new JDBCToComboBoxModelAdapter(sas_JDBCConnection, jdbcBathQuery);
    
             //Set it on the session
             if (session != null){
                 session.setAttribute(BATH_MODEL, bathComboAdapter);
    	        }
       }
       catch(Exception e){
          throw new RuntimeException(e);
       }
    }
    
  9. The resulting JDBCDefaultExampleControllerServlet.java should look as follows:
    package servlets;
    
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import com.sas.actionprovider.HttpActionProvider;
    import com.sas.storage.jdbc.JDBCConnection;
    import com.sas.storage.jdbc.JDBCAdapter;
    import com.sas.storage.jdbc.JDBCToComboBoxModelAdapter;
    import javax.swing.DefaultComboBoxModel;
    import javax.swing.ComboBoxModel;
    import com.sas.servlet.tbeans.models.JavaScriptNode;
    import java.sql.*;
    import java.util.Map;
    
    
    public class JDBCDefaultExampleControllerServlet
    extends javax.servlet.http.HttpServlet
    {
        //Global Web application Strings
        private static final String	ACTION_PROVIDER = "sas_actionProvider";
    
    
        //Global Web application JDBC variables
        private static final String	SAS_MODEL = "sas_model";
        private static final String	STATE_ADAPTER = "state_adapter";
        private static final String	STATE_MODEL = "state_model";
        private static final String	STYLE_MODEL = "style_model";
        private static final String	BATH_MODEL = "bath_model";
        private static final String	BED_MODEL = "bed_model";
        private static final String	JDBC_CONNECTION = "sas_JDBCConnection";
        private static final String	JDBC_DRIVER_NAME = "com.sas.rio.MVADriver";
        private static final String	JDBC_DATABASE_URL = "jdbc:sasiom://YOUR_LOCAL_MACHINE_NAME:5310";
    
    
        /*
         * doPost()
         * Respond to the Post message.
         */
        public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
        {
            // Note: Calling doGet to provide same behavior to POST and GET HTTP methods.
            doGet(request, response);
        }
    
    
      /*
       * doGet()
       * Respond to the Get message.
       */
      public void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException
      {
          // Note: Add User DO_GET code here
    	     HttpSession session = request.getSession();
    		 // Setup the ActionProvider
          HttpActionProvider sas_actionProvider = null;
          synchronized (session) {
             if (session != null){
                	sas_actionProvider = (HttpActionProvider)session.getAttribute(ACTION_PROVIDER);
             }
             //if ActionProvider is null, create one and put it on the session
             if (sas_actionProvider == null) {
                 sas_actionProvider = new com.sas.actionprovider.HttpActionProvider();
                 sas_actionProvider.setLocale(request.getLocale());
                	sas_actionProvider.setControllerURL(request.getContextPath() + "/JDBCDefaultExampleControllerServlet");
                	sas_actionProvider.setName(ACTION_PROVIDER);
                	// store object in its scope
                	if (session != null)
                		session.setAttribute(ACTION_PROVIDER, sas_actionProvider);
            }
            //else execute the ActionProvider command
            else{
                	sas_actionProvider.executeCommand(request, response, response.getWriter());
            }
          }
    
          synchronized (session) {
                //Setup the JDBC connection
                JDBCConnection sas_JDBCConnection = null;
                if (session != null){
                    sas_JDBCConnection = (JDBCConnection)session.getAttribute(JDBC_CONNECTION);
                }
    
                if (sas_JDBCConnection == null){
                    try{
                        sas_JDBCConnection = new JDBCConnection();
                        sas_JDBCConnection.setDriverName(JDBC_DRIVER_NAME);
                        sas_JDBCConnection.setDatabaseURL(JDBC_DATABASE_URL);
    
                        ServletConfig sc = getServletConfig();
                        sas_JDBCConnection.setUsername(sc.getInitParameter("metadata-userid"));
                        sas_JDBCConnection.setPassword(sc.getInitParameter("metadata-password"));
                        session.setAttribute(JDBC_CONNECTION, sas_JDBCConnection);
                    }
                    catch(Exception e){
                        throw new RuntimeException(e);
                    }
                }
    
    		      //Setup the query for the connection, such as "select * from sashelp.class"
                String jdbcQuery = "select * from sasuser.houses";
                String stateComboQuery = "select * from sasuser.states";
                String jdbcStyleQuery = "select distinct style from sasuser.houses";
                String jdbcBedQuery = "select distinct bedrooms from sasuser.houses";
                String jdbcBathQuery = "select distinct baths from sasuser.houses";
    
    
                //Setup the JDBC model adapter
                JDBCToComboBoxModelAdapter adapter = null;
                DefaultComboBoxModel adapter2 = new DefaultComboBoxModel();
                DefaultComboBoxModel newStateModel = new DefaultComboBoxModel();
                JDBCToComboBoxModelAdapter stateComboAdapter = null;
                JDBCToComboBoxModelAdapter styleComboAdapter = null;
                JDBCToComboBoxModelAdapter bathComboAdapter = null;
                JDBCToComboBoxModelAdapter bedComboAdapter = null;
    
    
                if (session != null){
                    adapter = (JDBCToComboBoxModelAdapter)session.getAttribute(SAS_MODEL);
                     stateComboAdapter = (JDBCToComboBoxModelAdapter)session.getAttribute(STATE_ADAPTER);
                     newStateModel = (DefaultComboBoxModel)session.getAttribute(STATE_MODEL);
                     styleComboAdapter = (JDBCToComboBoxModelAdapter)session.getAttribute(STYLE_MODEL);
                     bedComboAdapter = (JDBCToComboBoxModelAdapter)session.getAttribute(BED_MODEL);
                     bathComboAdapter = (JDBCToComboBoxModelAdapter)session.getAttribute(BATH_MODEL);
                }
    
                if (adapter == null){
                    try{
                        //Add code below to create the model adapter.
                         //See the com.sas.storage.jdbc package in the API for more details.
    			//INSERT_MODEL_ADAPTER_CODE_HERE
    
    			adapter = new JDBCToComboBoxModelAdapter(sas_JDBCConnection, jdbcQuery);
                        //Set it on the session
                        if (session != null){
                            session.setAttribute(SAS_MODEL, adapter);
                        }
                    }
                    catch(Exception e){
                        throw new RuntimeException(e);
                    }
              }
              if (stateComboAdapter == null){
                    try{
                        //Add code below to create the model adapter.
                         //See the com.sas.storage.jdbc package in the API for more details.
    		     //INSERT_MODEL_ADAPTER_CODE_HERE
    		     stateComboAdapter =
                             new JDBCToComboBoxModelAdapter(sas_JDBCConnection, stateComboQuery);
    
    				if(newStateModel == null)
    					newStateModel = new DefaultComboBoxModel();
    
    				for(int i=0; i < stateComboAdapter.getSize(); i++)
    				{
    					JavaScriptNode node = new JavaScriptNode();
    					node.setText((String)stateComboAdapter.getElementAt(i));
    					newStateModel.addElement(node);
    
    				}
                        //Set it on the session
                        if (session != null){
                            session.setAttribute(STATE_ADAPTER, stateComboAdapter);
    						   session.setAttribute(STATE_MODEL, newStateModel);
                        }
                    }
                    catch(Exception e){
                        throw new RuntimeException(e);
                    }
              }
              if (styleComboAdapter == null){
                    try{
                        //Add code below to create the model adapter.
                         //See the com.sas.storage.jdbc package in the API for more details.
    			//INSERT_MODEL_ADAPTER_CODE_HERE
    			styleComboAdapter =
                                  new JDBCToComboBoxModelAdapter(sas_JDBCConnection, jdbcStyleQuery);
    
                        //Set it on the session
                        if (session != null){
                            session.setAttribute(STYLE_MODEL, styleComboAdapter);
    
                        }
                    }
                    catch(Exception e){
                        throw new RuntimeException(e);
                    }
              }
              if (bedComboAdapter == null){
                    try{
                        //Add code below to create the model adapter.
                         //See the com.sas.storage.jdbc package in the API for more details.
    		     //INSERT_MODEL_ADAPTER_CODE_HERE
    		     bedComboAdapter =
                             new JDBCToComboBoxModelAdapter(sas_JDBCConnection, jdbcBedQuery);
    
                        //Set it on the session
                        if (session != null){
                            session.setAttribute(BED_MODEL, bedComboAdapter);
    
                        }
                    }
                    catch(Exception e){
                        throw new RuntimeException(e);
                    }
              }
              if (bathComboAdapter == null){
                    try{
                        //Add code below to create the model adapter.
                         //See the com.sas.storage.jdbc package in the API for more details.
    		     //INSERT_MODEL_ADAPTER_CODE_HERE
    		     bathComboAdapter =
                             new JDBCToComboBoxModelAdapter(sas_JDBCConnection, jdbcBathQuery);
    
                        //Set it on the session
                        if (session != null){
                            session.setAttribute(BATH_MODEL, bathComboAdapter);
    
                        }
                    }
                    catch(Exception e){
                        throw new RuntimeException(e);
                    }
                }
    
            }
    
            //Forward the request to the JSP for display
            RequestDispatcher rd =
    			getServletContext().getRequestDispatcher("/JDBCDefaultExampleViewer.jsp");
            rd.forward(request, response);
    
        }
    }
    

Step 3: Add components to the JSP

  1. Create a folder in your project webapp directory named myImages to hold the house logo image. From the About It page, click the right-mouse button while the pointer is over the House Logo image. Select the Save Picture As option and save the picture to the myImages folder.
  2. Add the following page imports to the JDBCDefaultExampleViewer.jsp file.
    	<%@page import="com.sas.servlet.tbeans.util.validators.RequiredStringValidator" %>
    	<%@page import="com.sas.servlet.tbeans.models.Item" %>
    	<%@page import="javax.swing.DefaultComboBoxModel" %>
    
  3. Add the following code to the body for the image and page heading:
    <center>
    <table>
    <tr>
    <td><img src="myImages/house1.gif" height="150" width="150"/></td>
    <td>
     <table>
      <tr><td align="center"><font color="saddlebrown"><h1>Hometown</h1></font></td></tr>
      <tr><td><font color="saddlebrown"><h1>Realty</h1></font></td></tr>
     </table>
    </td>
    </tr>
    </table>
    </center>
    <hr color="saddlebrown">
    
  4. Drag a Form tag from the Form Elements tag palette, drop it between the <body> tags on the JDBCDefaultExampleViewer.jsp file, and modify it as follows:
    	<sas:Form id="form" method="get" action="pushButtonSubmit.jsp" validationEnabled="true" >
    
  5. Add the following code for the section header:
    <center>
    <h2>Customer Request Form</h2>
    </center>
    
  6. Add the following scriptlet code to create a RequiredStringValidator:
    <%
       RequiredStringValidator reqValidator = new RequiredStringValidator();
    %>
    
  7. Add the following scriptlet code to create models to use for searching square footage and price:
    <%
    
       DefaultComboBoxModel sqFootModel = new DefaultComboBoxModel();
       DefaultComboBoxModel priceModel = new DefaultComboBoxModel();
    
       Item sqFoot1 = new Item();
       sqFoot1.setText("less than 1,500 sq. ft.");
       sqFoot1.setValue("< 1500");
    
       Item sqFoot2 = new Item();
       sqFoot2.setText("more than 1,500 sq. ft.");
       sqFoot2.setValue(">= 1500");
    
       Item price1 = new Item();
       price1.setText("less than $75,000");
       price1.setValue("< 75000");
    
       Item price2 = new Item();
       price2.setText("more than $75,000");
       price2.setValue("> 75000");
    
       sqFootModel.addElement(sqFoot1);
       sqFootModel.addElement(sqFoot2);
    
       priceModel.addElement(price1);
       priceModel.addElement(price2);
    
       pageContext.setAttribute("sqFootModel", sqFootModel);
       pageContext.setAttribute("priceModel", priceModel);
    %>
    
  8. Copy and paste the following code inside the form tags:
    <center>
    <table border="0">
    
    <tr>
    <td align="right" nowrap>First Name</td>
    <td><sas:TextEntry id="firstName"/>
    	<%
    		//To set a required validator on the firstName field
    		firstName.setValidator(reqValidator);
    		form.addComponent(firstName);  //Must be done for validation to work
    
    	%>
    </td>
    <td align="right" nowrap>Last Name</td>
    <td><sas:TextEntry id="lastName"/>
    	<%
    		//To set a required validator on the lastName field
    		lastName.setValidator(reqValidator);
    		form.addComponent(lastName);   //Must be done for validation to work
    
    	%>
    </td>
    <td align="right">Phone</td>
    <td><sas:TextEntry id="phone" /></td>
    </tr>
    
    <tr>
    <td align="right">Address</td>
    <td><sas:TextEntry id="address" /></td>
    <td align="right">City</td>
    <td><sas:TextEntry id="city"/></td>
    <td align="right">State</td>
    <td><sas:ComboBoxView id="state" model="state_model"/></td>
    <td>&nbsp;</td>
    </tr>
    
    <tr>
    <td align="right">Zip</td>
    <td><sas:TextEntry id="zip" size="9"/></td>
    <td nowrap>Email Address</td>
    <td><sas:TextEntry id="email" /></td>
    <td nowrap>Confirm Email:</td>
    <td><sas:TextEntry id="email2" /></td>
    </tr>
    
    </table>
    </center>
    
    <hr>
    
    <center>
    <h2>Home Search:</h2>
    </center>
    
    <center>
    <table>
    
    <tr>
    <td align="center"><h4>Style:</h4></td>
    <td><dir><h4>Bedrooms:</h4></dir></td>
    <td><dir><h4>Baths:</h4></dir></td>
    <td><dir><dir><h4>Sq. Footage:</h4></dir></dir></td>
    <td><dir><dir><h4>Price Range:</h4></dir></dir></td>
    </tr>
    
    <tr valign="top">
    <td><sas:ChoiceBox id="styleChoice" model="style_model"/></td>
    <td align="center"><dir><sas:ChoiceBox id="bedChoice" model="bed_model"/></dir></td>
    <td align="center"><dir><sas:ChoiceBox id="bathChoice" model="bath_model"/></dir></td>
    <td align="left" nowrap>
     <dir><sas:Radio id="sqFootRadio" model="sqFootModel" selectedIndex="0"/></dir></td>
    <td align="left" nowrap>
     <dir><sas:Radio id="priceRadio" model="priceModel" selectedIndex="0"/></dir></td>
    </tr>
    
    </table>
    </center>
    
    <br>
    <hr>
    
    <center>
    <sas:PushButton id="pushButton1" text="Submit Search" type="submit" onClick="validateEmail(email,email2);"/>
    </center>
    
  9. Copy and paste the following script code after the end tag for the Form:
    <script>
    		function validateEmail(email1, email2)
    		{
    
    
    		 	if (email1.value != email2.value){
    				alert("Please re-enter and confirm email address");
    				email1.focus();
    				event.returnValue = false;
    				return false;
    			}
    
    		}
    </script>
    
  10. The resulting JDBCDefaultExampleViewer.jsp file should look as follows:
    <%@ taglib uri="http://www.sas.com/taglib/sas" prefix="sas" %>
    <%@ page pageEncoding="UTF-8"%>
    <html>
    <%@page import="com.sas.servlet.tbeans.util.validators.RequiredStringValidator" %>
    <%@page import="com.sas.servlet.tbeans.models.Item" %>
    <%@page import="javax.swing.DefaultComboBoxModel" %>
    <title>HTML Form Tag Example</title>
    <head>
        <link href="styles/sasComponents.css" rel="STYLESHEET" type="text/css">
    	<LINK REL=STYLESHEET HREF="styles/myCSS.css" TYPE="text/css">
    </head>
    <body>
    <center>
    <table>
    <tr>
    <td><img src="myImages/house1.gif" height="150" width="150"/></td>
    <td>
     <table>
      <tr><td align="center"><font color="saddlebrown"><h1>Hometown</h1></font></td></tr>
      <tr><td><font color="saddlebrown"><h1>Realty</h1></font></td></tr>
     </table>
    </td>
    </tr>
    </table>
    </center>
    <hr color="saddlebrown">
    <sas:Form id="form" method="get" action="pushButtonSubmit.jsp" validationEnabled="true" >
    <center>
    <h2>Customer Request Form</h2>
    </center>
    
    <%
    
       RequiredStringValidator reqValidator = new RequiredStringValidator();
    %>
    
    <%
    
       DefaultComboBoxModel sqFootModel = new DefaultComboBoxModel();
       DefaultComboBoxModel priceModel = new DefaultComboBoxModel();
    
       Item sqFoot1 = new Item();
       sqFoot1.setText("less than 1,500 sq. ft.");
       sqFoot1.setValue("< 1500");
    
       Item sqFoot2 = new Item();
       sqFoot2.setText("more than 1,500 sq. ft.");
       sqFoot2.setValue(">= 1500");
    
       Item price1 = new Item();
       price1.setText("less than $75,000");
       price1.setValue("< 75000");
    
       Item price2 = new Item();
       price2.setText("more than $75,000");
       price2.setValue("> 75000");
    
       sqFootModel.addElement(sqFoot1);
       sqFootModel.addElement(sqFoot2);
    
       priceModel.addElement(price1);
       priceModel.addElement(price2);
    
       pageContext.setAttribute("sqFootModel", sqFootModel);
       pageContext.setAttribute("priceModel", priceModel);
    %>
    <center>
    <table border="0">
    <tr>
    	<td align="right" nowrap>First Name</td><td><sas:TextEntry id="firstName"/>
    	<%
    	    firstName.setValidator(reqValidator);
    		form.addComponent(firstName);
    
    	%>
       	</td>
    	<td align="right" nowrap>Last Name</td><td><sas:TextEntry id="lastName"/>
    	<%
    	    lastName.setValidator(reqValidator);
    		form.addComponent(lastName);
    
    	%></td>
    	<td align="right">Phone</td>
      <td><sas:TextEntry id="phone" /></td>
    </tr>
    <tr>
       <td align="right">Address</td><td><sas:TextEntry id="address" /></td>
    	<td align="right">City</td><td><sas:TextEntry id="city"/></td>
       <td align="right">State</td><td>
    		<sas:ComboBoxView id="state" model="state_model"/>
    
    	</td>
    
    	<td>&nbsp;</td>
    </tr>
    <tr>
       <td align="right">Zip</td><td><sas:TextEntry id="zip" size="9"/></td>
       <td nowrap>Email Address</td>
       <td><sas:TextEntry id="email" /></td>
       <td nowrap>Confirm Email:</td>
       <td><sas:TextEntry id="email2" /></td>
    </tr>
    </table>
    </center>
    <hr>
    <center>
    <h2>Home Search:</h2>
    </center>
    <center>
    <table>
    	<tr>
    		<td align="center"><h4>Style:</h4></td>
    		<td><dir><h4>Bedrooms:</h4></dir></td>
    		<td><dir><h4>Baths:</h4></dir></td>
    		<td><dir><dir><h4>Sq. Footage:</h4></dir></dir></td>
    		<td><dir><dir><h4>Price Range:</h4></dir></dir></td>
    
    	</tr>
    	<tr valign="top">
    		<td><sas:ChoiceBox id="styleChoice" model="style_model"/></td>
    		<td align="center"><dir><sas:ChoiceBox id="bedChoice" model="bed_model"/></dir></td>
    		<td align="center"><dir><sas:ChoiceBox id="bathChoice" model="bath_model"/></dir></td>
    		<td align="left" nowrap>
          <dir><sas:Radio id="sqFootRadio" model="sqFootModel" selectedIndex="0"/></dir></td>
    		<td align="left" nowrap>
          <dir><sas:Radio id="priceRadio" model="priceModel" selectedIndex="0"/></dir></td>
    	</tr>
    </table>
    </center>
    <br>
    <hr>
    <center>
    <sas:PushButton id="pushButton1" text="Submit Search" type="submit" onClick="validateEmail(email,email2);"/>
    </center>
    <%
       // Write the form footer
       //form.writeFooter(out);
    %>
    
    </sas:Form>
    <script>
    		function validateEmail(email1, email2)
    		{
    
    
    		 	if (email1.value != email2.value){
    				alert("Please enter re-enter and confirm email address");
    				email1.focus();
    				event.returnValue = false;
    				return false;
    			}
    
    		}
    </script>
    </body>
    </html>
    
  11. Create a new JSP file called pushButtonSubmit.jsp by selecting File [arrow] New. Choose the Tag Library JavaServer Page JSP template in the wizard.

    Copy the following code into the pushButtonSubmit.jsp file:

    <html>
    <%@page import="com.sas.storage.jdbc.JDBCToTableModelAdapter" %>
    <%@page import="com.sas.storage.jdbc.JDBCConnection" %>
    <%@page import="java.util.Enumeration" %>
    <%@page import="java.lang.String" %>
    <%@page import="javax.servlet.http.*" %>
    <%@page import="com.sas.servlet.tbeans.tableview.html.TableView" %>
    <head>
    <title>Submit PushButton Processing</title>
    </head>
    <body>
    <%
    String	JDBC_CONNECTION = "sas_JDBCConnection";
    String	JDBC_DRIVER_NAME = "com.sas.rio.MVADriver";
    String	JDBC_DATABASE_URL = "jdbc:sasiom://localhost:5310";
    JDBCConnection sas_JDBCConnection2 = null;
    JDBCToTableModelAdapter resultsAdapter = null;
    
    HttpSession session2 = request.getSession();
    
    if (session2 != null){
       	sas_JDBCConnection2 = (JDBCConnection)session2.getAttribute(JDBC_CONNECTION);
    }
    
    if (sas_JDBCConnection2 == null){
       try{
            sas_JDBCConnection2 = new JDBCConnection();
            sas_JDBCConnection2.setDriverName(JDBC_DRIVER_NAME);
            sas_JDBCConnection2.setDatabaseURL(JDBC_DATABASE_URL);
     	   ServletConfig sc2 = getServletConfig();
            sas_JDBCConnection2.setUsername(sc2.getInitParameter("metadata-userid"));
            sas_JDBCConnection2.setPassword(sc2.getInitParameter("metadata-password"));
            session2.setAttribute(JDBC_CONNECTION, sas_JDBCConnection2);
       }
       catch(Exception e){
           throw new RuntimeException(e);
       }
    }
    
    String jdbcResultsQuery = "select * from sasuser.houses where style='"+
       request.getParameter("styleChoice")+"'"+
       " and bedrooms >="+request.getParameter("bedChoice")+" and baths >="+
       request.getParameter("bathChoice") + "and sqfeet "+
       request.getParameter("sqFootRadio")+" and price "+request.getParameter("priceRadio");
    
    resultsAdapter = new JDBCToTableModelAdapter(sas_JDBCConnection2, jdbcResultsQuery);
    
    out.println("<p>Hi "+request.getParameter("firstName")+
       ".</p>  <p>Thank you for your home search.  Your search results are summarized below.</p>");
    
    TableView table = new TableView();
    table.setBorderWidth(2);
    table.setModel(resultsAdapter);
    table.write(out);
    
    %>
    </body>
    </html>
    

    For more information and options, see Web Application Example Templates.

Step 4: Finish the project

  1. Build the project.
  2. Start the IOM Spawner by selecting Start Menu [arrow] SAS AppDev Studio [arrow] Services [arrow] SAS V9.1 [arrow] Start SAS V9.1 IOM Spawner.
  3. Start the Java Web server.
  4. Execute in browser.