domoaarongato
asked on
returning multiple values from the model back to the servlet: only know how to do one.
I only know how to pass one value from the model back to the servlet. But i need to retreive about 5 or 6 of them.
can someone give me a hand?
//servlet
unitConverterModel uc = new unitConverterModel();
String pass = uc.convert(DimensionsSizeC ode,feetDr opDown,met ersDropDow n,feetLeng th,inchesL ength,feet Width,inch esWidth,fe etDecimalL ength,feet DecimalWid th,metersL ength,cent imetersLen gth,meters Width,cent imetersWid th,metersD ecimalLeng th,metersD ecimalWidt h);
//model
public String convert(String DimensionsSizeCode,String feetDropDown,String metersDropDown,String feetLength,String inchesLength,String feetWidth,String inchesWidth,String feetDecimalLength,String feetDecimalWidth,String metersLength,String centimetersLength,String metersWidth,String centimetersWidth,String metersDecimalLength,String metersDecimalWidth) {
String hello = "hello"
String goodbye = "goodbye";
return hello;
//
So i know if i do System.out.print(pass); in my servlet it will print"hello". But how do i access more than one value? What if i wanted to return both String hello;, String goodbye; and more? How could it be done?
can someone give me a hand?
//servlet
unitConverterModel uc = new unitConverterModel();
String pass = uc.convert(DimensionsSizeC
//model
public String convert(String DimensionsSizeCode,String feetDropDown,String metersDropDown,String feetLength,String inchesLength,String feetWidth,String inchesWidth,String feetDecimalLength,String feetDecimalWidth,String metersLength,String centimetersLength,String metersWidth,String centimetersWidth,String metersDecimalLength,String
String hello = "hello"
String goodbye = "goodbye";
return hello;
//
So i know if i do System.out.print(pass); in my servlet it will print"hello". But how do i access more than one value? What if i wanted to return both String hello;, String goodbye; and more? How could it be done?
You can return them as a List of Strings, like an ArrayList or something else.
ASKER
Do you have an example?
>> String hello = "hello"
>> String goodbye = "goodbye";
>> return hello;
ArrayList list = new ArrayList () ;
list.add ( hello ) ;
list.add ( goodbye ) ;
return list ;
>> String goodbye = "goodbye";
>> return hello;
ArrayList list = new ArrayList () ;
list.add ( hello ) ;
list.add ( goodbye ) ;
return list ;
ASKER
and how do i pull or seperate specific values in my controller?
for ( int i = 0, count = list.size () ; i < count ; i ++ )
{
String s = ( String ) list.get ( i ) ;
System.out.println ( s ) ;
// do processing with 's'
}
{
String s = ( String ) list.get ( i ) ;
System.out.println ( s ) ;
// do processing with 's'
}
ASKER
For instance what if i only wanted to get the goodbye value and ingnore hello?
Then you have to do that kind of processing using if/ else statements inside the loop depending upon what your conditions are for accepting or ignoring the values.
ASKER
Is this the best approach to this scenerio?
If you want to return multiple values - yes. You need to enclose them in an array or a list or a hashtable or something of that sort depending upon what kind of objects they are.
You can even enclose the response in a class like:
public final class Response
{
private String ID ;
private String message ;
private int type ;
private int code ;
private String exceptionDetails ;
private List data ;
// public get/ set methods for all
}
public final class Response
{
private String ID ;
private String message ;
private int type ;
private int code ;
private String exceptionDetails ;
private List data ;
// public get/ set methods for all
}
and return an object of the Response class from the data/ business layer to the controller/ UI layer. If the communication is asynchronous, the ID can be used to identify for which request this response has been received. The type can tell if the processing was a success or failure. Success/ failure messages can be stored in message. The code can contain any error code and exceptionDetails can contain more exception details if an exception occured. The data list can contain the actual data retrieved if it was successful.
ASKER
im a little confused. what should .size() and .get(i) be?
for ( int i = 0, count = list.size () ; i < count ; i ++ )
{
String s = ( String ) list.get ( i ) ;
System.out.println ( s ) ;
// do processing with 's'
}
for ( int i = 0, count = list.size () ; i < count ; i ++ )
{
String s = ( String ) list.get ( i ) ;
System.out.println ( s ) ;
// do processing with 's'
}
ASKER
for what it's worth the for loop throws this when i compile
C:\tomcat_55\webapps\WEB-I NF\classes >javac savedata/MandatorySer
vlet.java
savedata/MandatoryServlet. java:144: cannot find symbol
symbol : variable list
location: class savedata.MandatoryServlet
for ( int i = 0, count = list.size () ;
i < count ; i ++ )
^
savedata/MandatoryServlet. java:146: cannot find symbol
symbol : variable list
location: class savedata.MandatoryServlet
String s = ( String ) list.get (
i ) ;
^
2 errors
C:\tomcat_55\webapps\WEB-I
vlet.java
savedata/MandatoryServlet.
symbol : variable list
location: class savedata.MandatoryServlet
for ( int i = 0, count = list.size () ;
i < count ; i ++ )
^
savedata/MandatoryServlet.
symbol : variable list
location: class savedata.MandatoryServlet
String s = ( String ) list.get (
i ) ;
^
2 errors
list is what the servlet receives from the back end.
ArrayList list = uc.convert ( .... ) ;
ArrayList list = uc.convert ( .... ) ;
ASKER
thank you for your help this far.
i get a compile error and warning that reads.
savedata/unitConverterMode l.java:70: incompatible types
found : java.util.ArrayList
required: java.lang.String
return list;
^
Note: savedata/unitConverterMode l.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
i get a compile error and warning that reads.
savedata/unitConverterMode
found : java.util.ArrayList
required: java.lang.String
return list;
^
Note: savedata/unitConverterMode
Note: Recompile with -Xlint:unchecked for details.
1 error
Your convert () method has to be declared as
public ArrayList convert ( .... )
public ArrayList convert ( .... )
ASKER
ok, that worked. aside from the warning i still recieve when i copile.
my servlet is throwing this err now.
savedata/freeholdMandatory Servlet.ja va:127: incompatible types
found : java.util.ArrayList
required: java.lang.String
String pass = uc.convert(DimensionsSi
zeCode, feetDropDown, metersDropDown, feetLength, inchesLength, feetWidth, inche
sWidth, feetDecimalLength, feetDecimalWidth, metersLength, centimetersLength, me
tersWidth, centimetersWidth, metersDecimalLength, metersDecimalWidth);
^
savedata/MandatoryServlet. java:128: cannot find symbol
symbol : method size()
location: class java.lang.String
for (int i = 0, count = pass.siz
e(); i < count ; i ++ )
^
savedata/MandatoryServlet. java:130: cannot find symbol
symbol : method get(int)
location: class java.lang.String
String s = (String)pass.
get(i) ;
^
3 errors
my servlet is throwing this err now.
savedata/freeholdMandatory
found : java.util.ArrayList
required: java.lang.String
String pass = uc.convert(DimensionsSi
zeCode, feetDropDown, metersDropDown, feetLength, inchesLength, feetWidth, inche
sWidth, feetDecimalLength, feetDecimalWidth, metersLength, centimetersLength, me
tersWidth, centimetersWidth, metersDecimalLength, metersDecimalWidth);
^
savedata/MandatoryServlet.
symbol : method size()
location: class java.lang.String
for (int i = 0, count = pass.siz
e(); i < count ; i ++ )
^
savedata/MandatoryServlet.
symbol : method get(int)
location: class java.lang.String
String s = (String)pass.
get(i) ;
^
3 errors
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.