Sample Application: Upload File

 

This is an example of a full working JSP application using JSP and Servlet. This program demonstrates how to upload a file from client machine to server using JSP and servlets. It is very easy to get parameters like file type and file size. SO checking of these can be easily implemented.

Uploading a file from client machine to server is a very often requirement.Using JSP and servlets its easy to manage file uploads to your server. But be careful, allowing users to upload a file to your server opens a whole can of worms, so please be careful when enabling file uploads.

You can download the source of this Sample Application here.

This is illustrated based on the assumption that you are you're already familiar with JSP, Servlet technologies. and equipped with a Servlet-capable Web server.

Apache Tomcat is the servlet container that is used in the official Reference Implementation for the Java Servlet and JavaServer Pages technologies. You can download the latest version of Apache Tomcat from here

/webapps/

/webapps/UploadFile/

/webapps/UploadFile/web/

/webapps/UploadFile/web/index.jsp

/webapps/UploadFile/web/success.jsp

/webapps/UploadFile/web/META-INF/context.xml

/webapps/UploadFile/web/WEB-INF/web.xml

/webapps/UploadFile/src/

/webapps/UploadFile/src/java/

/webapps/UploadFile/src/java/UploadServlet.java

 

Lets go over each one:

/webapps/

 

The /webapps/ directory is our web server document root.

/webapps/UploadFile/

 

/UploadFile/ is the subdirectory where our application is accessed by the browser.

/webapps/UploadFile/web

 

/web/ is the subdirectory where our JSP files and xml files have been kept.

/webapps/UploadFile/web/index.jsp

 

index.jsp will be the entry point of our application. The web browser will be accessing this via http://yourdomain.com/fileupload. Before we use servlets to manage our uploads, first build a form that allows our users select a file to upload. Set the enctype of the form as "multipart/form-data", and provide a field of type file which allows the user to browse for the file to be uploaded.

/webapps/UploadFile/web/success.jsp

 

success.jsp is the web page which gives you information about the success or failure of the file upload process. Display appropriate messages in this page.

/webapps/UploadFile/web/META-INF/context.xml

 

context.xml will specify the context path to our project. In our case context path is "/uploadfile"

/webapps/UploadFile/web/WEB-INF/web.xml

 

web.xml acts as the deployment descriptor for our application. It is held in the application's WEB-INF directory. It defines a number of parameters that are used when the web application is deployed into the Tomcat Servlet/JSP container.

/webapps/UploadFile/src/

 

This folder holds all the source of our web application.

/webapps/UploadFile/src/java/

 

java This folder holds all the java files necessary for our application. Servlets are also stored in this directory. You can also store the files in a package structure.

/webapps/UploadFile/src/java/UploadServlet.java

 

UploadServlet.java is the servlet responsible for uploading the file to server. First it should instantiate the classes which supports file upload, like DiskFileItemFactory, ServletFileUpload etc. Then the file upload is processed. Use the function getServletContext().getRealPath("/") to get the path to which the file is to be uploaded.

[Page 1] [Page 2] [Page 3] [Page 4]

 
 
Contact Us