ramani081399
asked on
Populating Values dynamically in drop down boxes
I have a web application a JSP page that has two drop down boxes with following labels:
Drop down box 1: Choice1
Choice2
Choice3
Choice4
Drop down box 2: ChoiceA
ChoiceB
ChoiceC
ChoiceD
Currently I have hard coded these in the JSP page. I need to be able to do this more generically such that latter when more choices need to be added the user should not have the need to go and make changes in the JSP page but in some other external file from where these needs to be read and populeted in the two drop down boxes. How do i do this using server side implementation.
Also
When the page loads the above options appear in the drop down box.
But when, the user selects Choice1 from Drop down box 1, I want to be able to present the user with ChoiceA OR ChoiceB in the drop down box2. Similarly If the user selects Choice2 from the Drop down box 1, I need to be able to present ChoiceC OR ChoiceD.
How do i do this on the server side.A code to achieve both would be appreciated.
Thanks
Ramani
Drop down box 1: Choice1
Choice2
Choice3
Choice4
Drop down box 2: ChoiceA
ChoiceB
ChoiceC
ChoiceD
Currently I have hard coded these in the JSP page. I need to be able to do this more generically such that latter when more choices need to be added the user should not have the need to go and make changes in the JSP page but in some other external file from where these needs to be read and populeted in the two drop down boxes. How do i do this using server side implementation.
Also
When the page loads the above options appear in the drop down box.
But when, the user selects Choice1 from Drop down box 1, I want to be able to present the user with ChoiceA OR ChoiceB in the drop down box2. Similarly If the user selects Choice2 from the Drop down box 1, I need to be able to present ChoiceC OR ChoiceD.
How do i do this on the server side.A code to achieve both would be appreciated.
Thanks
Ramani
ASKER
kennethxu,
I would prefer the file system.
Thanks
Ramani
I would prefer the file system.
Thanks
Ramani
ok, let's say use an xml file:
<my-choice-file>
<box1 choice="Choice1">
<box2 choice="ChoiceA"/>
<box2 choice="ChoiceB"/>
</box1>
<box1 choice="Choice2">
<box2 choice="ChoiceC"/>
<box2 choice="ChoiceD"/>
</box1>
......
</my-choice-file>
or
<my-choice-file>
<choice id="1" desc="Choice1"/>
<choice id="2" desc="Choice2"/>
<choice id="3" desc="Choice3"/>
<choice id="A" desc="ChoiceA"/>
<choice id="B" desc="ChoiceB"/>
<choice id="C" desc="ChoiceC"/>
<choice id="D" desc="ChoiceD"/>
<box1 choice="1">
<box2 choice="A"/>
<box2 choice="B"/>
</box1>
<box1 choice="2">
<box2 choice="C"/>
<box2 choice="D"/>
</box1>
......
</my-choice-file>
which one make more sense to you or you think it will be easier to maintain? actually, if you have long description and box2 items are been reused, 2nd is better, otherwise go for 1st.
let me know
<my-choice-file>
<box1 choice="Choice1">
<box2 choice="ChoiceA"/>
<box2 choice="ChoiceB"/>
</box1>
<box1 choice="Choice2">
<box2 choice="ChoiceC"/>
<box2 choice="ChoiceD"/>
</box1>
......
</my-choice-file>
or
<my-choice-file>
<choice id="1" desc="Choice1"/>
<choice id="2" desc="Choice2"/>
<choice id="3" desc="Choice3"/>
<choice id="A" desc="ChoiceA"/>
<choice id="B" desc="ChoiceB"/>
<choice id="C" desc="ChoiceC"/>
<choice id="D" desc="ChoiceD"/>
<box1 choice="1">
<box2 choice="A"/>
<box2 choice="B"/>
</box1>
<box1 choice="2">
<box2 choice="C"/>
<box2 choice="D"/>
</box1>
......
</my-choice-file>
which one make more sense to you or you think it will be easier to maintain? actually, if you have long description and box2 items are been reused, 2nd is better, otherwise go for 1st.
let me know
ASKER
kennethxu,
I think i would go with the first choice.
Ramani
I think i would go with the first choice.
Ramani
ok, once you have your xml file ready, you can use xml parser to read them into a map and store it as session or application attribute.
do you have experiece of parsing xml?
do you have experiece of parsing xml?
ASKER
kennethxu,
I know xml from a very theoretical point, but havent implemented parsing. I would appreciate if you can show the code for doing the parsing and and dsiplaying the way i decribed in a jsp page.
Ramani
I know xml from a very theoretical point, but havent implemented parsing. I would appreciate if you can show the code for doing the parsing and and dsiplaying the way i decribed in a jsp page.
Ramani
here is a sample code to process the xml file into a Map, it is a excutable class, but you can also used it in your jsp:
import javax.xml.parsers.*;
import org.xml.sax.*;
import java.util.*;
public class MyOptions extends org.xml.sax.helpers.Defaul tHandler {
private Map options= new HashMap();
private List suboptions = null;
public MyOptions()
throws ParserConfigurationExcepti on, SAXException, java.io.IOException
{
SAXParserFactory factory = SAXParserFactory.newInstan ce();
SAXParser parser = factory.newSAXParser();
parser.parse( new java.io.File("myoptions.xm l"), this );
}
public Map getOptions() {
return options;
}
public void startElement(String uri, String localName, String qName, Attributes attrs) {
if(qName.equals("box1")){
suboptions = new ArrayList();
options.put( attrs.getValue( "choice" ), suboptions );
} else if( qName.equals("box2") ) {
suboptions.add( attrs.getValue( "choice" ) );
}
}
public static void main( String[] args ) throws Exception
{
Map options = new MyOptions().getOptions();
Iterator box1 = options.keySet().iterator( );
while( box1.hasNext() ) {
String option = (String) box1.next();
System.out.println( option );
Iterator box2 = ((List)options.get(option) ).iterator ();
while( box2.hasNext() ) {
System.out.println( "-" + box2.next() );
}
}
}
}
import javax.xml.parsers.*;
import org.xml.sax.*;
import java.util.*;
public class MyOptions extends org.xml.sax.helpers.Defaul
private Map options= new HashMap();
private List suboptions = null;
public MyOptions()
throws ParserConfigurationExcepti
{
SAXParserFactory factory = SAXParserFactory.newInstan
SAXParser parser = factory.newSAXParser();
parser.parse( new java.io.File("myoptions.xm
}
public Map getOptions() {
return options;
}
public void startElement(String uri, String localName, String qName, Attributes attrs) {
if(qName.equals("box1")){
suboptions = new ArrayList();
options.put( attrs.getValue( "choice" ), suboptions );
} else if( qName.equals("box2") ) {
suboptions.add( attrs.getValue( "choice" ) );
}
}
public static void main( String[] args ) throws Exception
{
Map options = new MyOptions().getOptions();
Iterator box1 = options.keySet().iterator(
while( box1.hasNext() ) {
String option = (String) box1.next();
System.out.println( option );
Iterator box2 = ((List)options.get(option)
while( box2.hasNext() ) {
System.out.println( "-" + box2.next() );
}
}
}
}
let me know if you have question on how to get above work.
ASKER
kennethxu,
So, the class MyOptions would be a javabean?? How do i use this in the jsp page?? Normally we would do a bean.get() Or bean.write. I am not sure how to use this in the jsp page. Appreciate your help.
Thanks
ramani
So, the class MyOptions would be a javabean?? How do i use this in the jsp page?? Normally we would do a bean.get() Or bean.write. I am not sure how to use this in the jsp page. Appreciate your help.
Thanks
ramani
it is a javabean, it is also executable so you can test like java MyOptions, it will print out the option list.
I have already provide getOptions method, you can add more get or write method when you see a need.
in you jsp page, you get the current selection of box1 as:
String selected = request.getParameter( "box1" );
you can get all box1 options from
Iterator box1 = options.keySet().iterator( );
and related box2 options from:
Iterator box2 = ((List)options.get(selecte d)).iterat or();
see example code in main method about how to use Iterator.
I have already provide getOptions method, you can add more get or write method when you see a need.
in you jsp page, you get the current selection of box1 as:
String selected = request.getParameter( "box1" );
you can get all box1 options from
Iterator box1 = options.keySet().iterator(
and related box2 options from:
Iterator box2 = ((List)options.get(selecte
see example code in main method about how to use Iterator.
ASKER
kennethxu,
The java class compiles and dsiplays the values to the console. Itried running this by pullingthe values into the Jsp page but seems not to get it working corectly
I am sending what i wrote.
<%@ page contentType="text/xml" %>
<%@ page import="MyOptions.*" %>
<jsp:useBean id="dropdownoptions"
class="dropdownoptions.MyO ptions" />
<%
Map options= new HashMap();
options = dropdownoptions.getOptions ();
Enumeration enu1 = options.keys();
Object obj = null;
%>
<% while (enu1.hasNext()) { %>
<% obj = enu1.nextElement();%>
<%Iterator box2 = ((List)options.get(selecte d)).iterat or();%>
<tr>
<td>View </td>
<td>Type</td>
<td>
<select>
<option>obj</option>
</select>
</td>
</tr>
<tr>
<td>Category</td>
<td>
<select>
<option>box2</option>
</select>
</td>
</tr>
<% } %>
Tell me if this is the way to display all the optioons in the top drop down and based on the selections in the drop down the relevant drop downs appear in the box below the first one.
Thanks
Ramani
The java class compiles and dsiplays the values to the console. Itried running this by pullingthe values into the Jsp page but seems not to get it working corectly
I am sending what i wrote.
<%@ page contentType="text/xml" %>
<%@ page import="MyOptions.*" %>
<jsp:useBean id="dropdownoptions"
class="dropdownoptions.MyO
<%
Map options= new HashMap();
options = dropdownoptions.getOptions
Enumeration enu1 = options.keys();
Object obj = null;
%>
<% while (enu1.hasNext()) { %>
<% obj = enu1.nextElement();%>
<%Iterator box2 = ((List)options.get(selecte
<tr>
<td>View </td>
<td>Type</td>
<td>
<select>
<option>obj</option>
</select>
</td>
</tr>
<tr>
<td>Category</td>
<td>
<select>
<option>box2</option>
</select>
</td>
</tr>
<% } %>
Tell me if this is the way to display all the optioons in the top drop down and based on the selections in the drop down the relevant drop downs appear in the box below the first one.
Thanks
Ramani
ASKER
kennethxu,
Sorry I changed the JSP , but i dont get the output with only two drop down boxes
<%@ page contentType="text/xml" %>
<%@ page import="ThirdJSP.MyOptions " %>
<jsp:useBean id="dropdownoptions" scope="page"
class="ThirdJSP.MyOptions" />
<%
Map options= new HashMap();
options = dropdownoptions.getOptions ();
Iterator box1 = options.keySet().iterator( );
%>
<% while (box1.hasNext() ) { %>
<%String selected = (String) box1.next();%>
<tr>
<td>View </td>
<td>Type</td>
<td>
<select>
<option><%= selected %></option>
</select>
</td>
</tr>
<%Iterator box2 = ((List)options.get(selecte d)).iterat or();%>
<%while( box2.hasNext() ) {%>
<tr>
<td>Category</td>
<td>
<select>
<option><%= box2 %></option>
</select>
</td>
</tr>
<%}%><%}%>
Ramani
Sorry I changed the JSP , but i dont get the output with only two drop down boxes
<%@ page contentType="text/xml" %>
<%@ page import="ThirdJSP.MyOptions
<jsp:useBean id="dropdownoptions" scope="page"
class="ThirdJSP.MyOptions"
<%
Map options= new HashMap();
options = dropdownoptions.getOptions
Iterator box1 = options.keySet().iterator(
%>
<% while (box1.hasNext() ) { %>
<%String selected = (String) box1.next();%>
<tr>
<td>View </td>
<td>Type</td>
<td>
<select>
<option><%= selected %></option>
</select>
</td>
</tr>
<%Iterator box2 = ((List)options.get(selecte
<%while( box2.hasNext() ) {%>
<tr>
<td>Category</td>
<td>
<select>
<option><%= box2 %></option>
</select>
</td>
</tr>
<%}%><%}%>
Ramani
not good coding style, but this test code works for me and it demos what you need to do. please notice that i don't put the bean into use, instead I hardcode the data, you'll have to chage something in order to get it work with you bean.
<%@ page import="java.util.*" %>
<%
Map opts = new HashMap();
String[] sub;
opts.put( "Choice1", new String[]{ "ChoiceA", "ChoiceB" } );
opts.put( "Choice2", new String[]{ "ChoiceC", "ChoiceD" } );
%>
<script language="JavaScript">
<!--
function update() {
var action = null;
for (i=0;i<form1.box1.length;i ++) {
if (form1.box1.options[i].sel ected) {
action = "?box1="+form1.box1.option s[i].text;
}
}
location.replace(action);
}
// -->
</script>
<form name="form1">
box1:<p>
<select name="box1" onchange="javascript:updat e()">
<%
String sel = request.getParameter( "box1" );
if( sel == null ) sel = "Choice1"; //default choice
Iterator box1 = opts.keySet().iterator();
while( box1.hasNext() ) {
String option = (String) box1.next();
%>
<option <%=option.equals(sel)?"sel ected":""% >><%=optio n%></optio n>
<%
}
%>
</select><p>
box2:<p>
<select name="box2">
<%
String[] box2 = (String[])opts.get(sel);
for( int ii = 0; ii < box2.length; ii++ ) {
%>
<option><%=box2[ii]%></opt ion>
<% } %>
</select>
</form>
<%@ page import="java.util.*" %>
<%
Map opts = new HashMap();
String[] sub;
opts.put( "Choice1", new String[]{ "ChoiceA", "ChoiceB" } );
opts.put( "Choice2", new String[]{ "ChoiceC", "ChoiceD" } );
%>
<script language="JavaScript">
<!--
function update() {
var action = null;
for (i=0;i<form1.box1.length;i
if (form1.box1.options[i].sel
action = "?box1="+form1.box1.option
}
}
location.replace(action);
}
// -->
</script>
<form name="form1">
box1:<p>
<select name="box1" onchange="javascript:updat
<%
String sel = request.getParameter( "box1" );
if( sel == null ) sel = "Choice1"; //default choice
Iterator box1 = opts.keySet().iterator();
while( box1.hasNext() ) {
String option = (String) box1.next();
%>
<option <%=option.equals(sel)?"sel
<%
}
%>
</select><p>
box2:<p>
<select name="box2">
<%
String[] box2 = (String[])opts.get(sel);
for( int ii = 0; ii < box2.length; ii++ ) {
%>
<option><%=box2[ii]%></opt
<% } %>
</select>
</form>
ASKER
kennethxu,
I tried using the bean and the code u suggested. The JSP page loads with two drop down boxes. But when change my choice to in the second drop down i get error saying
Only one top level element is allowed in an XML document. Line 22, Position 2
<form name="form1">
-^
<%@ page import="java.util.*" %>
<%@ page contentType="text/xml" %>
<%@ page import="ThirdJSP.MyOptions " %>
<jsp:useBean id="dropdownoptions" scope="page"
class="ThirdJSP.MyOptions" />
<%
Map options= new HashMap();
options = dropdownoptions.getOptions ();
Iterator box1 = options.keySet().iterator( );
%>
<script language="JavaScript">
<!--
function update() {
var action = null;
for (i=0;i<form1.box1.length;i ++) {
if (form1.box1.options[i].sel ected) {
action = "?box1="+form1.box1.option s[i].text;
}
}
location.replace(action);
}
// -->
</script>
<form name="form1">
box1:<p>
<select name="box1" onchange="javascript:updat e()">
<%
String sel = request.getParameter( "box1" );
if( sel == null ) sel = "Choice1"; //default choice
while( box1.hasNext() ) {
String option = (String) box1.next();
%>
<option <%=option.equals(sel)?"sel ected":""% >><%=optio n%></optio n>
<%
}
%>
</select><p>
box2:<p>
<select name="box2">
<%
Iterator box2 = ((List)options.get(sel)).i terator();
while(box2.hasNext()) {
%>
<option><%=box2.next()%></ option>
<% } %>
</select>
</form>
I tried using the bean and the code u suggested. The JSP page loads with two drop down boxes. But when change my choice to in the second drop down i get error saying
Only one top level element is allowed in an XML document. Line 22, Position 2
<form name="form1">
-^
<%@ page import="java.util.*" %>
<%@ page contentType="text/xml" %>
<%@ page import="ThirdJSP.MyOptions
<jsp:useBean id="dropdownoptions" scope="page"
class="ThirdJSP.MyOptions"
<%
Map options= new HashMap();
options = dropdownoptions.getOptions
Iterator box1 = options.keySet().iterator(
%>
<script language="JavaScript">
<!--
function update() {
var action = null;
for (i=0;i<form1.box1.length;i
if (form1.box1.options[i].sel
action = "?box1="+form1.box1.option
}
}
location.replace(action);
}
// -->
</script>
<form name="form1">
box1:<p>
<select name="box1" onchange="javascript:updat
<%
String sel = request.getParameter( "box1" );
if( sel == null ) sel = "Choice1"; //default choice
while( box1.hasNext() ) {
String option = (String) box1.next();
%>
<option <%=option.equals(sel)?"sel
<%
}
%>
</select><p>
box2:<p>
<select name="box2">
<%
Iterator box2 = ((List)options.get(sel)).i
while(box2.hasNext()) {
%>
<option><%=box2.next()%></
<% } %>
</select>
</form>
should be:
<%@ page contentType="text/html" %>
better to be:
Map options= dropdownoptions.getOptions ();
you might want to use "application" or "session" scope for your bean if you need better performance
<%@ page contentType="text/html" %>
better to be:
Map options= dropdownoptions.getOptions
you might want to use "application" or "session" scope for your bean if you need better performance
ASKER
I changed the page type to be text/html and still i get the error Only one top level element is allowed in an XML document. Line 34, Position 2
<form name="form1">
Why is this.
Ramani
<form name="form1">
Why is this.
Ramani
ASKER
Now the jsp page displays the two drop downs. But in the first drop down if i select choice1 it goes back to choice2 and does not show the corresponding ones in the second box
Ramani
<%@ page import="java.util.*"%>
<%@ page contentType="text/html"%>
<%@ page import="ThirdJSP.MyOptions "%>
<jsp:useBean id="dropdownoptions" scope="page"
class="ThirdJSP.MyOptions" />
<script language="JavaScript">
<!--
function update() {
var action = null;
for (i=0;i<form1.box1.length;i ++) {
if (form1.box1.options[i].sel ected) {
action = "?box1="+form1.box1.option s[i].text;
}
}
location.replace(action);
}
//-->
</script>
<%
Map options= new HashMap();
options = dropdownoptions.getOptions ();
Iterator box1 = options.keySet().iterator( );
%>
<form name="form1">
box1:<p></p>
<select name="box1" onchange="javascript:updat e()">
<%
String sel = request.getParameter( "box1" );
if( sel == null ) sel = "Choice1"; //default choice
while( box1.hasNext() ) {
String option = (String) box1.next();
%>
<option> <%=option%></option>
<%
}
%>
</select><p></p>
box2:<p></p>
<select name="box2">
<%
Iterator box2 = ((List)options.get(sel)).i terator();
while(box2.hasNext()) {
%>
<option><%=(String)box2.ne xt()%></op tion>
<% } %>
</select>
</form>
Ramani
<%@ page import="java.util.*"%>
<%@ page contentType="text/html"%>
<%@ page import="ThirdJSP.MyOptions
<jsp:useBean id="dropdownoptions" scope="page"
class="ThirdJSP.MyOptions"
<script language="JavaScript">
<!--
function update() {
var action = null;
for (i=0;i<form1.box1.length;i
if (form1.box1.options[i].sel
action = "?box1="+form1.box1.option
}
}
location.replace(action);
}
//-->
</script>
<%
Map options= new HashMap();
options = dropdownoptions.getOptions
Iterator box1 = options.keySet().iterator(
%>
<form name="form1">
box1:<p></p>
<select name="box1" onchange="javascript:updat
<%
String sel = request.getParameter( "box1" );
if( sel == null ) sel = "Choice1"; //default choice
while( box1.hasNext() ) {
String option = (String) box1.next();
%>
<option> <%=option%></option>
<%
}
%>
</select><p></p>
box2:<p></p>
<select name="box2">
<%
Iterator box2 = ((List)options.get(sel)).i
while(box2.hasNext()) {
%>
<option><%=(String)box2.ne
<% } %>
</select>
</form>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
kennethxu,
Thanks
Ramani
Thanks
Ramani
of cause you can do it with filesystem files, but a may be a litter harder.
can you tell me in which way you want to go?