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.

org.apache.geronimo.samples.javaee6.fileupload

  • MessageFilter.java
  • OnlinePeopleList.java
  • showServlet.java

The list of web application files in the application is depicted in the following.

|- WEB_INF

+- 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.

web.xml

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.

geronimo-web.xml

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  .

selectFile.jsp

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.

OnlinePeopleListener.java

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 getInputStream method.

MessageFilter.java

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:

showServlet.java

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.

Labels
  • No labels