Regarding Usage of Servlet in my application
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,
Which method开发者_StackOverflow中文版 must be used in the servlet program to receiving the file and processing? Such as doGet, doPost or doPut?
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.
doPost
. And remember theenctype="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.If you add the jar or class to the classpath (a jar goes to
WEB-INF/lib
, a class - toWEB-INF/classes
), then you can use it from your servlet directly, like :ExcelDatabaseComparator comparator = new ExcelDatabaseComparator(); comparator.compare(..);
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 ondoPost()
method in the Servlet.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
精彩评论