• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

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
0
princehyderabad
Asked:
princehyderabad
  • 10
  • 9
1 Solution
 
dorothy2Commented:
"my frst part wroked, but my second part didnt worked..."

What was the error or exception you got?

Dorothy

 
0
 
princehyderabadAuthor Commented:
There was no error or exception !!
0
 
aozarovCommented:
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?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
princehyderabadAuthor Commented:
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(storedProcCall1);
                        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'>>);
                       
           }
0
 
aozarovCommented:
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?
0
 
princehyderabadAuthor Commented:
What is need is how to pass A, B, C values in else part. If you see that cs1.setString(1, ...
0
 
aozarovCommented:
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"
0
 
princehyderabadAuthor Commented:
cs1.setString(3,map.get("A"));

NOT accepting ...............
0
 
aozarovCommented:
Did you define map before the loop?
what is your compilation error?
Also can you paste that whole segment of code.
0
 
princehyderabadAuthor Commented:
yes I did all that.

Its says something:
 "cannot resolve method setString (int, java.lang.Object)"
0
 
aozarovCommented:
change it to:
s1.setString(3, (String) map.get("A"));
or to
s1.setString(3, map.get("A").toString());
0
 
princehyderabadAuthor Commented:
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
0
 
princehyderabadAuthor Commented:
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");
                                    }
                          }

0
 
aozarovCommented:
change:
String value = item.getString();
to
String value = item.getString().trim();

This should fix both new_line and empty space problems
0
 
princehyderabadAuthor Commented:
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.getName());
                        cs1.setString(2,cfile.getsize());

I tried getSize() and getsize() for both its error is: cannot resolve variable getsize()

0
 
aozarovCommented:
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).
0
 
princehyderabadAuthor Commented:
I understood but unable to do it. Well guess time for new thread ;o)
0
 
aozarovCommented:
Wait with that..
Are you sure calling item.getSize() doesn't return you the size you are looking for?
0
 
princehyderabadAuthor Commented:
cs1.setString(2,item.getsize());
Error: cannot resolve method setString(int,long)

Please follow on new thread which I already created:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21404671.html
0
 
aozarovCommented:
I did :-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 10
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now