Link to home
Start Free TrialLog in
Avatar of lilyyan
lilyyan

asked on

pass variableA which is declared method A in method B

hello, every1,

got a question about variable access in a java class

public class myclassA {

publicv doPost(HttpServletRequest request, HttpServletResponse response){

String firstName ="", midName="", lastName="", title="", fileName="";

boolean isMultipart = DiskFileUpload.isMultipartContent(request);

if (isMultipart ){

getFormdata(firstName, midName, lastName, title, fileName);

......
}


} // end of doPost

public void getFormdata(String firstName, String midName, String lastName, String title, String fileName){

                    // Create a factory for disk-based file items
                    DiskFileItemFactory factory = new DiskFileItemFactory();
                    
                    //maximum size that will be stored in memory
                    factory.setSizeThreshold(1000*1024);
                    
                    // Create a new file upload handler
                   ServletFileUpload upload = new ServletFileUpload(factory);

try {
List fileItems = upload.parseRequest(request);

.....
}

........
}

}// end of myclassA

--------------------------------------
my questions are:

q1. in the doPost method, it get the http request and servlet
when i call getFormdata() in doPost(), do i need pass the request or respose reference to getFormdata()

q2: when i pass firstName , midName ... to getFormdata(), the values of firstName and midName have been changed in getFormdata().

will the values of firstName and midName in doPost() are automatically chenged as well?



Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

No and no
Avatar of lilyyan
lilyyan

ASKER

can you explain the reasons
Avatar of lilyyan

ASKER

in java, the value is passed by reference in a method, so for q2, the value should be changed as well
All parameters are passed by value actually but String is also immutable
Avatar of lilyyan

ASKER

if in the classA

public void getFormdata(String firstName, String midName, String lastName, String title, String fileName){

                 // Create a factory for disk-based file items
                 DiskFileItemFactory factory = new DiskFileItemFactory();
                 
                 //maximum size that will be stored in memory
                 factory.setSizeThreshold(1000*1024);
                 
                 // Create a new file upload handler
                ServletFileUpload upload = new ServletFileUpload(factory);

try {
List fileItems = upload.parseRequest(request);

firstName ="Simth";
lastName ="John";

.....
}

----------------------------
then System.out.println( "last name is: + " lastName);

should the last name is "John"?
>>should the last name is "John"?

Only if you print it in the same method in which you changed it. If you print it from the calling method after you change it, you will see no change, since arguments are passed by value
have a look at this link, it gives an explanation

http://mindprod.com/jgloss/callbyvalue.html

> should the last name is "John"?

no, it will be what is was originally, if you want to change it you'd do something like:

lastName = getFormdata(firstName, midName, lastName, title, fileName);;


public String getFormdata(String firstName, String midName, String lastName, String title, String fileName){

                 // Create a factory for disk-based file items
                 DiskFileItemFactory factory = new DiskFileItemFactory();
                 
                 //maximum size that will be stored in memory
                 factory.setSizeThreshold(1000*1024);
                 
                 // Create a new file upload handler
                ServletFileUpload upload = new ServletFileUpload(factory);

try {
List fileItems = upload.parseRequest(request);

firstName ="Simth";
lastName ="John";

.....
}

return lastName;
Avatar of lilyyan

ASKER

but how about more that one values need to be passed back ?

i'm totally lost. java parameter is passed by value ? why in some function, the value of some object varivales can be changed by calling a function?
> why in some function, the value of some object varivales can be changed by calling a function?

the contents of the object can be change. but not the contents of string.
i'd suggest creating a class storing the details you need set and pass that

public class Name
{
   String firstName;
   String lastName;

   // add getters and setters
Avatar of lilyyan

ASKER

hi, thanks for your reply.

well, this is a question i asked few weeks ago, the answer is different for from the answer in this posting:

https://www.experts-exchange.com/questions/22050750/java-function-for-pass-values-back.html
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia 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
Avatar of lilyyan

ASKER

well, i basically can understand this example.

in my question, i try to get the form data which are passed from a http request. the form is enctype="multipart/form-data"

and i want to get the form data in a seperate method, any suggestion ?

appreciate your reply, i will get back to this question tomorrow


public Name getName(HttpServletRequest request) {

   Name name = new Name();
   
   name.setFirstName(.....
   name.setLastName(.....
   ...

   return name;
}
Avatar of lilyyan

ASKER

well, my program structure is something like:
--------------------------------------------------
public class myclassA {

publicv doPost(HttpServletRequest request, HttpServletResponse response){

String firstName ="", midName="", lastName="", title="", fileName="";

boolean isMultipart = DiskFileUpload.isMultipartContent(request);

if (isMultipart ){

getFormdata(firstName, midName, lastName, title, fileName);

......
}


} // end of doPost

public void getFormdata(String firstName, String midName, String lastName, String title, String fileName){

                 // Create a factory for disk-based file items
                 DiskFileItemFactory factory = new DiskFileItemFactory();
                 
                 //maximum size that will be stored in memory
                 factory.setSizeThreshold(1000*1024);
                 
                 // Create a new file upload handler
                ServletFileUpload upload = new ServletFileUpload(factory);

try {
        List fileItems = upload.parseRequest(request);
       Iterator iter =fileItems.iterator();
                        String fieldName = "";
                         while (iter.hasNext())                               
                         {
                         //      System.out.println("enter the while loop" );
                               FileItem item = (FileItem)iter.next();
                               if (item.isFormField())
                              {
                                     fieldName = item.getFieldName();
                                     if(fieldName.equals("firstName")){
                                           firstName =item.getString();
                                               
                                               }
                                       }// end of if

                                     // then mid name , last name, title, etc
                           .....................    
                            }// end of while

.....
} // end of try

........
} // end of getFormdata()

}// end of myclassA

------------------------------------------------------------------------------------------

how to make it situable to your suggestion ?
SOLUTION
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
Avatar of lilyyan

ASKER

may you explain:

why you use Map map = new HashMap();

saves you creating an additional class to store the required details.
depends on nthe details of your requirements which is better