Child pages
  • fileupload-javaee6 - Annotation for servlets, filters,listeners and better file upload support in JavaEE 6
Skip to end of metadata
Go to start of metadata

Application Overview

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.

Application Content

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

+- Geronimo-web.xml

|- index.html

|- header.html

|- selectFile.jsp

Application Implementation

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.

xmlweb.xml fileupload-javaee6 Servlet Sample index.html ]]>

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.

xmlgeronimo-web.xml web-app xmlns="" xmlns:naming="" xmlns:sec="" xmlns:sys=""> ${pom.groupId} fileupload-javaee6 ${version} car /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  .

… ]]> 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. 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. 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 getInputStream method." + "Http Servlet Request content type: "+ httpServletRequest.getContentType() + "

"; } Collection parts = httpServletRequest.getParts(); if (!parts.isEmpty()) { int i = 0; for (Part apart : parts) { message += "

"+ (++i) + ". Name=" + apart.getName()+ ";ContentType=" + apart.getContentType() + ";Size="+ apart.getSize() + "

"; } } else { message += "

HttpServletRequest.getParts() returns an empty collection!

"; } Throwable problem = null; try { Part p = ((HttpServletRequest) request).getPart("testFile"); if (null != p) { String part = p.toString(); String pname = p.getName(); long size = p.getSize(); String contentType = p.getContentType(); if (size > 10000) { message += "The file size is "+ size+ "b, it's filterd because the file size is limited to 10 kb"; } else { message += "Part: "+ part + "
Part Name: "+ pname + "
Size: "+ size + "
ContentType: "+ contentType + "
HeadNames: "; for (String name : p.getHeaderNames()) { message += name + ";"; } in = new p.getInputStream()); String content = ""; int c =; while (c != -1) { if (c == '\n') { content += "
"; } content += (char) c; c =; } if (content.equals("")) { message += "
Sorry, this is not a plain text, so we can not display it."; } else { message += "
The text file content is:
"+ content; message += "
"; } } } else { message += "

HttpServletRequest.getPart(String name) returns null!

"; } request.setAttribute("message", message); chain.doFilter(request, response); } catch (Throwable t) { problem = t; t.printStackTrace(); } } ... } ]]> 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:"); out.println(""); out.println("File Upload System"); out.println(""); out.println(""); out.println("

A listener is dectecting online person number.

"); out.println("

Currently,there are " + "" + getServletContext().getAttribute("onLineNumber") + " people visiting this file upload system\!

"); String message = request.getAttribute("message").toString(); if (message.indexOf("returns null\!") < 0) { out.println("

Attributes and content of the file:

"); } out.println(message + "
"); out.println(""); out.println(""); } finally { out.close(); } } ... } ]]>

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.

mvn clean install

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.

  1. Scroll down to Deploy New from the Console Navigation panel.
  2. Load fileupload-javaee6-war-3.0-M1.war from fileupload-javaee6-war\target folder in to the Archive input box.
  3. 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.

  • No labels