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.isMultipart Content(re quest);
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(1 000*1024);
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory) ;
try {
List fileItems = upload.parseRequest(reques t);
.....
}
........
}
}// 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?
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.isMultipart
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(1
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory)
try {
List fileItems = upload.parseRequest(reques
.....
}
........
}
}// 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?
No and no
ASKER
can you explain the reasons
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
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(1 000*1024);
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory) ;
try {
List fileItems = upload.parseRequest(reques t);
firstName ="Simth";
lastName ="John";
.....
}
-------------------------- --
then System.out.println( "last name is: + " lastName);
should the last name is "John"?
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(1
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory)
try {
List fileItems = upload.parseRequest(reques
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
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(1 000*1024);
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory) ;
try {
List fileItems = upload.parseRequest(reques t);
firstName ="Simth";
lastName ="John";
.....
}
return lastName;
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(1
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory)
try {
List fileItems = upload.parseRequest(reques
firstName ="Simth";
lastName ="John";
.....
}
return lastName;
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?
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
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
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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-da ta"
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
in my question, i try to get the form data which are passed from a http request. the form is enctype="multipart/form-da
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
Name name = new Name();
name.setFirstName(.....
name.setLastName(.....
...
return name;
}
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.isMultipart Content(re quest);
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(1 000*1024);
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory) ;
try {
List fileItems = upload.parseRequest(reques t);
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("first Name")){
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 ?
--------------------------
public class myclassA {
publicv doPost(HttpServletRequest request, HttpServletResponse response){
String firstName ="", midName="", lastName="", title="", fileName="";
boolean isMultipart = DiskFileUpload.isMultipart
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(1
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory)
try {
List fileItems = upload.parseRequest(reques
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("first
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
may you explain:
why you use Map map = new HashMap();
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
depends on nthe details of your requirements which is better