This sample demonstrates a better experience about file upload support in JavaEE 6, if a request is of type multipart/form-data and if the servlet handling the request is annotated using the @MultipartConfig, the HttpServletRequest can make available the various parts of the multipart request via getParts or getPart Method.
Also this sample introduces the annotation way to define servlets, filters and listeners for web application. A “metadata-complete” attribute which defines whether the web descriptor is complete, or whether the class files of the jar file should be examined for annotations and web fragments at deployment time. If the “metadata-complete” attribute in web application deployment descriptor web.xml is not specified or is set to “false”, the deploy tool will scan the class files of the application for annotations.
fileupload-javaee6 application consists of following list of packages and classes.
The list of web application files in the application is depicted in the following.
web.xml defines the welcome-file of the web application. The “metadata-complete” attribute is set to false in this sample. So during deploying time, the deploy tool will scan all class files for annotations which may define servlets, filters or web listeners.
geronimo-web.xml describes information about the project (e.g. module's unique identification, any dependencies) inside the <sys:environment> tag. It is a good idea to give this module some sort of unique identification, so that it can later be referenced by some other deployable application. The path specified in the <context-root> tag will be the first segment of the URL used to access this web application. So to access this web application the url will be http://<hostname>:<port>/ fileupload-javaee6.
selectFile.jsp provides a file upload widget, including a browsing button and a text box. The type of the request is “multipart/form-data”.The file you submitted will be in stream .
OnlinePeopleListener.java uses annotation @WebListener() to define a web listener. It implements javax.servlet.ServletContextListener and javax.servlet.http.HttpSessionListener.
This web listener will detect current online people. Each time a new session is created, onlineNumber increases.OnlineNumber decreases when the session is destroyed.
MessageFilter.java implements javax.servlet.Filter and uses @WebFilter to define a filter in web application. The annotation also contains metadata about the filter being declared. The name of the filter is “MessageFilter”, the same as class name. The urlPatterns “/showServlet” attribute indicates all URLs to be filtered by this filter.Use getPart method to get access to each uploaded file.
MessageFilter.java will filter files larger than 10kb. As with the qualified files, part provides access to the headers, content type related with it and also the content via the
showServlet.java implements javax.servlet.http.HttpServlet and uses @WebServlet annotation to define a servlet in the web application.
This servlet displays the detail information of the file as followed:
Get Source Code
Please reference Samples General Information for information on obtaining and building the source for this and other samples.
Build the web application
Once all the sources get checked out the next step is to build fileupload-javaee6 sample.It requires Maven 2 for building the binaries.
From the < fileupload-javaee6_home> directory run the following command.
This process will take a couple of minutes. The binaries will be generated in the corresponding target directory .
Deploy the web application
Deploying sample application is pretty straight forward as we are going to use the Geronimo Console.
- Scroll down to Deploy New from the Console Navigation panel.
- Load fileupload-javaee6-war-3.0-M1.war from fileupload-javaee6-war\target folder in to the Archive input box.
- Press Install button to deploy application in the server.
Test the web application
The app is visible at http://localhost:8080/fileupload-javaee6.
Select a file to be uploaded,note that the file should be less than 10kb,otherwise it will be filtered.
It displays current online people number and detail information of the file you submitted.