开发者

Regarding Usage of Servlet in my application

开发者 https://www.devze.com 2023-01-16 10:59 出处:网络
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.

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开发者_StackOverflow中文版 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.


  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(..);
    


  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:

  • How to upload files in JSP/Servlet
0

精彩评论

暂无评论...
验证码 换一张
取 消