Link to home
Start Free TrialLog in
Avatar of domoaarongato
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(DimensionsSizeCode,feetDropDown,metersDropDown,feetLength,inchesLength,feetWidth,inchesWidth,feetDecimalLength,feetDecimalWidth,metersLength,centimetersLength,metersWidth,centimetersWidth,metersDecimalLength,metersDecimalWidth);


//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?




Avatar of Mayank S
Mayank S
Flag of India image

You can return them as a List of Strings, like an ArrayList or something else.
Avatar of domoaarongato
domoaarongato

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 ;
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'

}
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.
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
}
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.
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 what it's worth the for loop throws this when i compile

C:\tomcat_55\webapps\WEB-INF\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
list is what the servlet receives from the back end.

ArrayList list = uc.convert ( .... ) ;
thank you for your help this far.

i get a compile error and warning that reads.

savedata/unitConverterModel.java:70: incompatible types
found   : java.util.ArrayList
required: java.lang.String
                   return list;
                          ^
Note: savedata/unitConverterModel.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
Your convert () method has to be declared as

public ArrayList convert ( .... )
ok, that worked. aside from the warning i still recieve when i copile.

my servlet is throwing this err now.

savedata/freeholdMandatoryServlet.java: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
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial