princehyderabad
asked on
Logic Correction - Servlet
hi,
MY Servlet
{
DiskFileUpload upload = new DiskFileUpload();
List items = upload.parseRequest(req);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField())
{
System.out.println("its a field");
}
else { System.out.println("its a field"); }
.......<some DB query here>.................
<<now again I wanna utilize that upload, items, iter, item ???????>>
<< so I created again with new names upload1, items1, iter1, item1 ????>>
DiskFileUpload upload1 = new DiskFileUpload();
List items1 = upload1.parseRequest(req);
Iterator iter1 = items1.iterator();
while (ite1r.hasNext()) {
FileItem item1 = (FileItem) iter1.next();
if (item1.isFormField())
{
System.out.println("its a field");
}
else { System.out.println("its a field"); }
.......................... ..
}
my frst part wroked, but my second part didnt worked.
Let me know 1st is it correct way I did or any other logic ???
Idea is on my 1st part I'm inserting data into DB, then doing query and getting some results based on that results again I hv to insert other data into DB.
Thanks
MY Servlet
{
DiskFileUpload upload = new DiskFileUpload();
List items = upload.parseRequest(req);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField())
{
System.out.println("its a field");
}
else { System.out.println("its a field"); }
.......<some DB query here>.................
<<now again I wanna utilize that upload, items, iter, item ???????>>
<< so I created again with new names upload1, items1, iter1, item1 ????>>
DiskFileUpload upload1 = new DiskFileUpload();
List items1 = upload1.parseRequest(req);
Iterator iter1 = items1.iterator();
while (ite1r.hasNext()) {
FileItem item1 = (FileItem) iter1.next();
if (item1.isFormField())
{
System.out.println("its a field");
}
else { System.out.println("its a field"); }
..........................
}
my frst part wroked, but my second part didnt worked.
Let me know 1st is it correct way I did or any other logic ???
Idea is on my 1st part I'm inserting data into DB, then doing query and getting some results based on that results again I hv to insert other data into DB.
Thanks
ASKER
There was no error or exception !!
The code looks ok though DiskFileUpload is deprecated in favour of ServletFileUpload
Not sure why it is a problem to create and apply DiskFileUpload parsing again. (one reason might be because that class
is saving the files in temporary storage on the disk as opose to FileUpload that keeps it in memory).
You can try using the other class but why not reusing the first FileItem list, items, in your second part?
Not sure why it is a problem to create and apply DiskFileUpload parsing again. (one reason might be because that class
is saving the files in temporary storage on the disk as opose to FileUpload that keeps it in memory).
You can try using the other class but why not reusing the first FileItem list, items, in your second part?
ASKER
Okay I changed and trying to uisng in frist, but having porblem here...
if (item.isFormField())
{
String name = item.getFieldName();
String value = item.getString();
System.out.println("Name:" +name+ " Value:" +value);
String n = name;
String v = value;
<< I GET HERE 3 VALUES A=Hello, B=Hi, C=Hai Question is everytime while loop go next step, n and v values changes ...>>>
}
else
{
String storedProcCall1 = "{call sp (?,?,?)}";
CallableStatement cs1 = connection.prepareCall(sto redProcCal l1);
File cfile=new File(item.getName());
cs1.setString(1,<<HOW TO PASS VALUE 'A' HERE'>>);
cs1.setString(2,<<HOW TO PASS VALUE 'B' HERE'>>);
cs1.setString(3,<<HOW TO PASS VALUE 'C' HERE'>>);
}
if (item.isFormField())
{
String name = item.getFieldName();
String value = item.getString();
System.out.println("Name:"
String n = name;
String v = value;
<< I GET HERE 3 VALUES A=Hello, B=Hi, C=Hai Question is everytime while loop go next step, n and v values changes ...>>>
}
else
{
String storedProcCall1 = "{call sp (?,?,?)}";
CallableStatement cs1 = connection.prepareCall(sto
File cfile=new File(item.getName());
cs1.setString(1,<<HOW TO PASS VALUE 'A' HERE'>>);
cs1.setString(2,<<HOW TO PASS VALUE 'B' HERE'>>);
cs1.setString(3,<<HOW TO PASS VALUE 'C' HERE'>>);
}
Not sure what the problem.
You are in a loop and n and v are member variables inside a loop therefore they will get different value each iteration.
Where do you have the problem?
You are in a loop and n and v are member variables inside a loop therefore they will get different value each iteration.
Where do you have the problem?
ASKER
What is need is how to pass A, B, C values in else part. If you see that cs1.setString(1, ...
You will need to take the DB code outside of the loop.
inside the loop save n / v values in a map. (which you delcare before the loop. Map map = new HashMap(); )
So after:
String n = name;
String v = value;
add map.put(n, v);
Then after the loop in your DB code call map.get("A") to get "Hello", map.get("B) to get "Hi" and map.get("C") to get "Hai"
inside the loop save n / v values in a map. (which you delcare before the loop. Map map = new HashMap(); )
So after:
String n = name;
String v = value;
add map.put(n, v);
Then after the loop in your DB code call map.get("A") to get "Hello", map.get("B) to get "Hi" and map.get("C") to get "Hai"
ASKER
cs1.setString(3,map.get("A "));
NOT accepting ...............
NOT accepting ...............
Did you define map before the loop?
what is your compilation error?
Also can you paste that whole segment of code.
what is your compilation error?
Also can you paste that whole segment of code.
ASKER
yes I did all that.
Its says something:
"cannot resolve method setString (int, java.lang.Object)"
Its says something:
"cannot resolve method setString (int, java.lang.Object)"
change it to:
s1.setString(3, (String) map.get("A"));
or to
s1.setString(3, map.get("A").toString());
s1.setString(3, (String) map.get("A"));
or to
s1.setString(3, map.get("A").toString());
ASKER
Yeah I tried your above 1st option its working. But a very minor problem ...
My Code-----
....
System.out.println("Name:" +name+ " Value:" +value+":End");
map.put(name,value);
......
Due to some reason or may be "map" does that. That it is returning the 'Carrier Return' after value.
eg: One of data is that my Name is 'Lang' and Value is 'English'
so it should display like this:
Name: Lang Value: English: End
but it is display like this:
Name: Lang Value: English
: End
Thought its minor stuff but my DB insert query only if "English" but not "English "
Any idea how to solve this
My Code-----
....
System.out.println("Name:"
map.put(name,value);
......
Due to some reason or may be "map" does that. That it is returning the 'Carrier Return' after value.
eg: One of data is that my Name is 'Lang' and Value is 'English'
so it should display like this:
Name: Lang Value: English: End
but it is display like this:
Name: Lang Value: English
: End
Thought its minor stuff but my DB insert query only if "English" but not "English "
Any idea how to solve this
ASKER
I'm sorry I dont think using map its creating problem, but it is creating problem with once emtpy space "Egnlish " instead of "English"
Some where here in my code there is that problem:
DiskFileUpload upload = new DiskFileUpload();
// Parse the request
List items = upload.parseRequest(req);
// give u the list of items in the form
Iterator iter = items.iterator();
Map map = new HashMap();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField())
{
String name = item.getFieldName();
String value = item.getString();
map.put(name,value);
System.out.println("Name:" +name+ " Value:" +value+":End");
}
}
Some where here in my code there is that problem:
DiskFileUpload upload = new DiskFileUpload();
// Parse the request
List items = upload.parseRequest(req);
// give u the list of items in the form
Iterator iter = items.iterator();
Map map = new HashMap();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField())
{
String name = item.getFieldName();
String value = item.getString();
map.put(name,value);
System.out.println("Name:"
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hello,
File cfile=new File(item.getName());
Using this I'm getting the File Name. How to get the file size ???
I'm using this....
File cfile=new File(item.getName());
<Writing to DB by passing below variable)>>
cs1.setString(1,cfile.getN ame());
cs1.setString(2,cfile.gets ize());
I tried getSize() and getsize() for both its error is: cannot resolve variable getsize()
File cfile=new File(item.getName());
Using this I'm getting the File Name. How to get the file size ???
I'm using this....
File cfile=new File(item.getName());
<Writing to DB by passing below variable)>>
cs1.setString(1,cfile.getN
cs1.setString(2,cfile.gets
I tried getSize() and getsize() for both its error is: cannot resolve variable getsize()
You should call getSize on the FileItem.
There is a length() method on File that will return you the size of the file (but this will work only
after you saved the content of FileItem localy into that File).
There is a length() method on File that will return you the size of the file (but this will work only
after you saved the content of FileItem localy into that File).
ASKER
I understood but unable to do it. Well guess time for new thread ;o)
Wait with that..
Are you sure calling item.getSize() doesn't return you the size you are looking for?
Are you sure calling item.getSize() doesn't return you the size you are looking for?
ASKER
cs1.setString(2,item.getsi ze());
Error: cannot resolve method setString(int,long)
Please follow on new thread which I already created:
https://www.experts-exchange.com/questions/21404671/Get-file-size-Servlet-Help.html
Error: cannot resolve method setString(int,long)
Please follow on new thread which I already created:
https://www.experts-exchange.com/questions/21404671/Get-file-size-Servlet-Help.html
I did :-)
What was the error or exception you got?
Dorothy