Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First of all, this (old) question and answer assumes JSF 2.0/2.1. Since JSF 2.2 there's a native <code>&lt;h:inputFile&gt;</code> component without the need for 3rd party component libraries. See also <a href="https://stackoverflow.com/questions/27677397/how-to-upload-file-using-jsf-2-2-hinputfile-where-is-the-saved-file">How to upload file using JSF 2.2 &lt;h:inputFile&gt;? Where is the saved File?</a></p> <hr> <p>The easiest way would be using <a href="http://myfaces.apache.org/tomahawk-project/tomahawk20/index.html" rel="nofollow noreferrer">Tomahawk for JSF 2.0</a>. It offers a <a href="http://myfaces.apache.org/tomahawk-project/tomahawk20/tagdoc/t_inputFileUpload.html" rel="nofollow noreferrer"><code>&lt;t:inputFileUpload&gt;</code></a> component. </p> <p>Here's a step-by-step tutorial:</p> <ul> <li><p>Create a blank dynamic web project for Servlet 3.0 and JSF 2.0. The <code>web.xml</code> must comply Servlet 3.0 spec and already contain the JSF servlet:</p> <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="YourProjectName" version="3.0"&gt; &lt;display-name&gt;Your Project Name&lt;/display-name&gt; &lt;servlet&gt; &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt; &lt;servlet-class&gt;javax.faces.webapp.FacesServlet&lt;/servlet-class&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt; &lt;url-pattern&gt;*.xhtml&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;/web-app&gt; </code></pre> <p>The <code>faces-config.xml</code> must comply JSF 2.0 spec:</p> <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;faces-config xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd" version="2.0"&gt; &lt;/faces-config&gt; </code></pre> <hr></li> <li><p>Download <a href="http://www.apache.org/dyn/closer.cgi/myfaces/binaries/tomahawk20-1.1.14-bin.zip" rel="nofollow noreferrer">Tomahawk 1.1.10 for JSF 2.0</a>. Extract the zip file, go to the <code>/lib</code> folder and copy all <code>*.jar</code> files into your <code>/WEB-INF/lib</code>. </p> <p>It are 18 files, of which <code>batik*.jar</code> and <code>xml*.jar</code> are unnecessary for using alone the <code>t:inputFileUpload</code> component. You could leave them away.</p> <hr></li> <li><p>Configure the Tomahawk extensions filter in <code>web.xml</code>. It's the one who's responsible for handling <code>multipart/form-data</code> requests which is required to be able to send files over HTTP.</p> <pre class="lang-xml prettyprint-override"><code>&lt;filter&gt; &lt;filter-name&gt;MyFacesExtensionsFilter&lt;/filter-name&gt; &lt;filter-class&gt;org.apache.myfaces.webapp.filter.ExtensionsFilter&lt;/filter-class&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;MyFacesExtensionsFilter&lt;/filter-name&gt; &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt; &lt;/filter-mapping&gt; </code></pre> <p>Note that the <code>&lt;servlet-name&gt;</code> must match the exact <code>&lt;servlet-name&gt;</code> of the <code>FacesServlet</code> as you've definied in <code>web.xml</code>.</p> <hr></li> <li><p>Create a simple Facelet, <code>upload.xhtml</code>:</p> <pre class="lang-xml prettyprint-override"><code>&lt;!DOCTYPE html&gt; &lt;html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:t="http://myfaces.apache.org/tomahawk" xmlns:ui="http://java.sun.com/jsf/facelets"&gt; &lt;h:head&gt; &lt;title&gt;Tomahawk file upload demo&lt;/title&gt; &lt;/h:head&gt; &lt;h:body&gt; &lt;h:form enctype="multipart/form-data"&gt; &lt;t:inputFileUpload value="#{bean.uploadedFile}" /&gt; &lt;h:commandButton value="submit" action="#{bean.submit}" /&gt; &lt;h:messages /&gt; &lt;/h:form&gt; &lt;/h:body&gt; &lt;/html&gt; </code></pre> <p>Note the <code>enctype="multipart/form-data"</code> attribute on <code>&lt;h:form&gt;</code>, this is very important in order to be able to send files with HTTP.</p> <hr></li> <li><p>Create a simple managed bean, <code>com.example.Bean</code>:</p> <pre><code>package com.example; import java.io.IOException; import javax.faces.application.FacesMessage; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; import javax.faces.context.FacesContext; import org.apache.commons.io.FilenameUtils; import org.apache.myfaces.custom.fileupload.UploadedFile; @ManagedBean @RequestScoped public class Bean { private UploadedFile uploadedFile; public void submit() throws IOException { String fileName = FilenameUtils.getName(uploadedFile.getName()); String contentType = uploadedFile.getContentType(); byte[] bytes = uploadedFile.getBytes(); // Now you can save bytes in DB (and also content type?) FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(String.format("File '%s' of type '%s' successfully uploaded!", fileName, contentType))); } public UploadedFile getUploadedFile() { return uploadedFile; } public void setUploadedFile(UploadedFile uploadedFile) { this.uploadedFile = uploadedFile; } } </code></pre> <hr></li> </ul> <p>That should be it. Open it by <a href="http://localhost:8080/projectname/upload.xhtml" rel="nofollow noreferrer">http://localhost:8080/projectname/upload.xhtml</a>.</p> <p>As to your concrete questions:</p> <blockquote> <p><em>what should i do to pass the file from the JSF to the managed bean and then transform it to a byte[](To be able to handle it over to the EJB)?</em></p> </blockquote> <p>This is answered above.</p> <blockquote> <p><em>How can a servlet help me?</em></p> </blockquote> <p>It is able to process and control HTTP requests/responses. In a JSF environment, the <code>FacesServlet</code> already does all the work.</p> <blockquote> <p><em>Do i need a servlet to do this?</em></p> </blockquote> <p>In a JSF environment, the <code>FacesServlet</code> is mandatory. But it's already provided by the API, you don't need to write one yourself. However, to be able to download files from a database, another servlet is definitely useful. You can find a basic example here: <a href="https://stackoverflow.com/questions/132052/servlet-for-serving-static-content/29991447#29991447">Servlet for serving static content</a>.</p> <blockquote> <p><em>Also i found that in some blog it mentions something about servlets 3.0, but i dont know if my working enviroment is ussing it, how can if i am ussing servlets 3.0(I am ussing JEE6)?</em></p> </blockquote> <p>If you're using Servlet 3.0 container like Glassfish 3, JBoss AS 6, Tomcat 7, etc and the <code>web.xml</code> is declared as Servlet 3.0, then you're definitely using Servlet 3.0. Servlet 3.0 is part of Java EE 6.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload