views:

31

answers:

2

I have a JSP form which is made of <input type="file"/> tag alone for allowing the user to browse and select the excel sheet.

Am going to write a servlet program for uploading the file that is selected to the server.

My questions here are,

  1. Which method must be used in the servlet program to receiving the file and processing? Such as doGet, doPost or doPut?

  2. I have written a java program to read the excel file and compare the contents with the database. Whether I need to integrate the java program inside the servlet program itself, or should I have to just call the java program alone from Servlet?

Please advise.

+2  A: 
  1. As stated in the HTML specification you have to use the POST method and the enctype attribute of the form have to be set to "multipart/form-data".

    <form action="upload" method="post" enctype="multipart/form-data">
        <input type="file" name="file" />
        <input type="submit" />
    </form>
    

    Since the request method is POST, you need to hook on doPost() method in the Servlet.

  2. You can just call the Java code from inside the Servlet the usual Java way. Import package/class, instantiate/access it, use methods. Nothing different than in all other Java classes.

See also:

BalusC
+3  A: 
  1. doPost. And remember the enctype="multipart/form-data" of the <form>. Also, you'll need a special utility to handle that enctype. commons-fileupload gives you the ability to parse multipart requests.

  2. If you add the jar or class to the classpath (a jar goes to WEB-INF/lib, a class - to WEB-INF/classes), then you can use it from your servlet directly, like :

    ExcelDatabaseComparator comparator = new ExcelDatabaseComparator();
    comparator.compare(..);
    
Bozho
If I hadn't posted an answer at the same time, this had deserved a +1, here :)
BalusC
same the other way around :)
Bozho
What do you really mean by parsing the multipart requests? and how it should be done?
LGAP
Background explanation and code example is available in [this answer](http://stackoverflow.com/questions/2422468/how-to-upload-files-in-jsp-servlet/2424824#2424824) (which I by the way already linked to you more than once in your previous questions... do you follow links?).
BalusC
Sorry Balus.. Will read it carefully now
LGAP
@LGAP the "getting started" of commons-fileupload says it all. Also read BalusC's extensive answer
Bozho