Sample 32153: How to Use a JSP Radio Custom Tag with a JDBC Query as the Model
Overview
This example demonstrates how to use the sas:Radio custom tag that is available with SAS® AppDevStudioTM. The example also demonstrates how to use a JDBC query as a model for the sas:Radio custom tag by utilizing the JDBCToComboBoxModelAdapter class that is also provided with SAS AppDev Studio.
Additional Documentation
- For a list of samples for common tasks, see SAS Note 32218.
- The SAS AppDev Studio Developer's Site contains detailed information that will assist you when developing Web applications with SAS AppDev Studio, including an API reference and the custom tag reference for the sas tagset.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
Instructions
- First, build a new web applicaton project in SAS AppDev Studio, using the following template
JDBC Default Servlet
If you are not familiar with building a web application project, instructions can be found in the following sample
- The template will generate a servlet and a jsp file, which by default are called JDBCDefaultExampleControllerServlet.java, and JDBCDefaultExampleViewer.jsp. Replace the contents of these files with the code below.
- Execute the servlet on the test Tomcat server. If you are not familiar with testing a web application, detailed instructions may be found in this sample
Code for JDBCDefaultExampleViewer.jsp
<%@ taglib uri="http://www.sas.com/taglib/sas" prefix="sas" %>
<%@ page pageEncoding="UTF-8"%>
<html>
<head>
<link href="styles/sasComponents.css" rel="STYLESHEET" type="text/css">
</head>
<body>
<%
// This code will check to see if the form has been submitted.
// If so, the selected item will be stored so that the radio box will display it
// We will also print out the selected value, for demonstration purposes
String selectedItem = request.getParameter("radioBox1");
if( null != selectedItem ){
out.write( "<h3>You selected: " + request.getParameter( "radioBox1" ) + "</h3>");
}
%>
<sas:Form action="JDBCDefaultExampleViewer.jsp">
<sas:Radio id="radioBox1" name="radioBox1" model="sas_model_JDBCDefaultExample"
selectedItem="<%=selectedItem%>" />
<sas:PushButton text="submit"/>
</sas:Form>
</body>
</html>
|
Code for JDBCDefaultExampleControllerServlet.java
Required changes
- Edit the value of the JDBC_DATABASE_URL field, to point to your server. The code generated by the template will have the values you chose during the wizard,
so reuse that value when you replace the code.
private static final String JDBC_DATABASE_URL = "jdbc:sasiom://t1972.na.sas.com:8591";
- Edit the value of the jdbcQuery variable, with a valid query. The code generated by the template will have the query you chose during the wizard,
so reuse that value if you would like.
String jdbcQuery = "select distinct country from sashelp.prdsale";
- If you did not use the default values during the generation of the project, you may need to update the following values.
- The package for the servlet
package servlets;
- The servlet name
public class JDBCDefaultExampleControllerServlet ...
- The url mapping for the action provider. It must match the servlet mapping for the servlet
sas_actionProvider.setControllerURL(request.getContextPath()+ "/JDBCDefaultExample");
package servlets;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import listeners.ExamplesSessionBindingListener;
import com.sas.actionprovider.HttpActionProvider;
import com.sas.servlet.util.BaseUtil;
import com.sas.storage.jdbc.JDBCConnection;
import com.sas.storage.jdbc.JDBCToComboBoxModelAdapter;
import com.sas.util.SasPasswordString;
import com.sas.web.keys.ComponentKeys;
public class JDBCDefaultExampleControllerServlet extends javax.servlet.http.HttpServlet
{
// Declare a default version ID since parent class implements java.io.Serializable
private static final long serialVersionUID = 1L;
//Global webapp Strings
private static final String ACTION_PROVIDER = "sas_actionProvider_JDBCDefaultExample";
// Global webapp JDBC variables
private static final String SAS_MODEL = "sas_model_JDBCDefaultExample";
private static final String JDBC_CONNECTION = "sas_JDBCConnection_JDBCDefaultExample";
private static final String JDBC_DRIVER_NAME = "com.sas.rio.MVADriver";
private static final String JDBC_DATABASE_URL = "jdbc:sasiom://t1972.na.sas.com:8591";
private static final Properties staticJDBCConnectionProperties = new Properties();
static { // Specify JDBC connection properties here
// Use: staticJDBCConnectionProperties.put("", "");
}
/*
* 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();
// Ensure character set is specified before calling response.getWriter().
String charset = BaseUtil.getOutputCharacterEncoding(request);
response.setContentType("text/html; charset=" + charset);
// 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 HttpActionProvider();
sas_actionProvider.setLocale(request.getLocale());
sas_actionProvider.setControllerURL(request.getContextPath()
+ "/JDBCDefaultExample");
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();
Properties connectionProperties = new Properties();
// Add static JDBC connection properties
connectionProperties.putAll(staticJDBCConnectionProperties);
// Add additional JDBC connection properties
String username = sc.getInitParameter("username");
if (username != null && username.length() > 0) {
connectionProperties.put("user", username);
String password = sc.getInitParameter("password");
if (password != null && password.length() > 0) {
// Add password property, decode if SAS password encoded
connectionProperties.put("password", SasPasswordString.decode(password));
}
}
sas_JDBCConnection.setConnectionInfo(connectionProperties);
session.setAttribute(JDBC_CONNECTION, sas_JDBCConnection);
}
catch(Exception e){
throw new ServletException(e);
}
}
// TODO Setup the query for the connection, such as "select * from sashelp.class"
String jdbcQuery = "select distinct country from sashelp.prdsale";
//Setup the JDBC model adapter
JDBCToComboBoxModelAdapter adapter = null;
if (session != null){
adapter = (JDBCToComboBoxModelAdapter)session.getAttribute(SAS_MODEL);
}
if (adapter == null){
try{
adapter = new JDBCToComboBoxModelAdapter(sas_JDBCConnection, jdbcQuery);
//Set it on the session
if (session != null){
session.setAttribute(SAS_MODEL, adapter);
ExamplesSessionBindingListener.getInstance(session).addAdapter(adapter);
}
}
catch(Exception e){
throw new ServletException(e);
}
}
}
//Forward the request to the JSP for display
String sas_forwardLocation = request.getParameter(ComponentKeys.FORWARD_LOCATION);
if (sas_forwardLocation == null)
{
sas_forwardLocation = "/JDBCDefaultExampleViewer.jsp";
}
RequestDispatcher rd = getServletContext().getRequestDispatcher(sas_forwardLocation);
rd.forward(request, response);
}
}
|
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
The following results will be displayed after running the sample code and submitting the form.

This example demonstrates how to use a JDBC query as a model for the sas:Radio custom tag.
| Date Modified: | 2008-05-27 14:39:22 |
| Date Created: | 2008-05-19 13:52:33 |
Operating System and Release Information
| SAS System | SAS AppDev Studio | Microsoft® Windows® for x64 | 3.2 | | 9.1 TS1M3 SP4 | |
| Microsoft Windows 2000 Advanced Server | 3.2 | | 9.1 TS1M3 SP4 | |
| Microsoft Windows 2000 Datacenter Server | 3.2 | | 9.1 TS1M3 SP4 | |
| Microsoft Windows 2000 Server | 3.2 | | 9.1 TS1M3 SP4 | |
| Microsoft Windows 2000 Professional | 3.2 | | 9.1 TS1M3 SP4 | |
| Microsoft Windows NT Workstation | 3.2 | | 9.1 TS1M3 SP4 | |
| Microsoft Windows Server 2003 Datacenter Edition | 3.2 | | 9.1 TS1M3 SP4 | |
| Microsoft Windows Server 2003 Enterprise Edition | 3.2 | | 9.1 TS1M3 SP4 | |
| Microsoft Windows Server 2003 Standard Edition | 3.2 | | 9.1 TS1M3 SP4 | |
| Microsoft Windows XP Professional | 3.2 | | 9.1 TS1M3 SP4 | |
| Windows Vista | 3.2 | | 9.1 TS1M3 SP4 | |