0

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 Answers 2

3
  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(..);
    
Sign up to request clarification or add additional context in comments.

4 Comments

If I hadn't posted an answer at the same time, this had deserved a +1, here :)
What do you really mean by parsing the multipart requests? and how it should be done?
Background explanation and code example is available in this answer (which I by the way already linked to you more than once in your previous questions... do you follow links?).
@LGAP the "getting started" of commons-fileupload says it all. Also read BalusC's extensive answer
2
  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:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.