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?




Java

Avatar of undefined
Last Comment
Mayank S
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?
Avatar of Mayank S
Mayank S
Flag of India image

>> String hello = "hello"
>> String goodbye = "goodbye";
>> return hello;

ArrayList list = new ArrayList () ;
list.add ( hello ) ;
list.add ( goodbye ) ;
return list ;
Avatar of domoaarongato

ASKER

and how do i pull or seperate specific values in my controller?
Avatar of Mayank S
Mayank S
Flag of India image

for ( int i = 0, count = list.size () ; i < count ; i ++ )
{
  String s = ( String ) list.get ( i ) ;
  System.out.println ( s ) ;
  // do processing with 's'

}
Avatar of domoaarongato

ASKER

For instance what if i only wanted to get the goodbye value and ingnore hello?
Avatar of Mayank S
Mayank S
Flag of India image

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.
Avatar of domoaarongato

ASKER

Is this the best approach to this scenerio?
Avatar of Mayank S
Mayank S
Flag of India image

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.
Avatar of Mayank S
Mayank S
Flag of India image

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
}
Avatar of Mayank S
Mayank S
Flag of India image

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.
Avatar of domoaarongato

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'

}
Avatar of domoaarongato

ASKER

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
Avatar of Mayank S
Mayank S
Flag of India image

list is what the servlet receives from the back end.

ArrayList list = uc.convert ( .... ) ;
Avatar of domoaarongato

ASKER

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
Avatar of Mayank S
Mayank S
Flag of India image

Your convert () method has to be declared as

public ArrayList convert ( .... )
Avatar of domoaarongato

ASKER

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo