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.
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 getInputStream
method.
"; } Collection
"+ (++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 + ";"; } java.io.InputStreamReader in = new java.io.InputStreamReader( p.getInputStream()); String content = ""; int c = in.read(); while (c != -1) { if (c == '\n') { content += "
"; } content += (char) c; c = in.read(); } 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(); } } ... } ]]>
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:
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("