I am looking around a few blogs, to try to find how to upload files using JSF 2.0 But all the solutions kind of confuse me. I would like to know what do I exactly need to be able to successfully upload a file(MP3, PDF, video... what ever type) and store it in a database as a @Lob. This is what I have done so far:
I created an entity that has an attribute of type byte[] and it is also annotated with a @Lob annotation.
I created an EJB that will introduce the entity with with a method that has a byte[] as a parameter and inserts it into the database using the EntityManager class( persist method).
I created a JSF page with an input tag of type "file" and a submit button
I prepared a managed bean to interchange information about the file with the JSF page.
Now I am stuck, and I have lots of doubts:
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)?
How can a servlet help me?
Do I need a servlet to do this?
Also I found that in some blog it mentions something about servlets 3.0, but I don't know if my working environment is using it, how can if I am using servlets 3.0 (I am using JEE6)?
I never did file upload before and also I am not very familiar with servlets. I am confused, someone could give me some starting tips, please?
First of all, this (old) question and answer assumes JSF 2.0/2.1. Since JSF 2.2 there's a native
<h:inputFile>
component without the need for 3rd party component libraries. See also How to upload file using JSF 2.2 <h:inputFile>? Where is the saved File?The easiest way would be using Tomahawk for JSF 2.0. It offers a
<t:inputFileUpload>
component.Here's a step-by-step tutorial:
Create a blank dynamic web project for Servlet 3.0 and JSF 2.0. The
web.xml
must comply Servlet 3.0 spec and already contain the JSF servlet:The
faces-config.xml
must comply JSF 2.0 spec:Download Tomahawk 1.1.10 for JSF 2.0. Extract the zip file, go to the
/lib
folder and copy all*.jar
files into your/WEB-INF/lib
.It are 18 files, of which
batik*.jar
andxml*.jar
are unnecessary for using alone thet:inputFileUpload
component. You could leave them away.Configure the Tomahawk extensions filter in
web.xml
. It's the one who's responsible for handlingmultipart/form-data
requests which is required to be able to send files over HTTP.Note that the
<servlet-name>
must match the exact<servlet-name>
of theFacesServlet
as you've definied inweb.xml
.Create a simple Facelet,
upload.xhtml
:Note the
enctype="multipart/form-data"
attribute on<h:form>
, this is very important in order to be able to send files with HTTP.Create a simple managed bean,
com.example.Bean
:That should be it. Open it by http://localhost:8080/projectname/upload.xhtml.
As to your concrete questions:
This is answered above.
It is able to process and control HTTP requests/responses. In a JSF environment, the
FacesServlet
already does all the work.In a JSF environment, the
FacesServlet
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: Servlet for serving static content.If you're using Servlet 3.0 container like Glassfish 3, JBoss AS 6, Tomcat 7, etc and the
web.xml
is declared as Servlet 3.0, then you're definitely using Servlet 3.0. Servlet 3.0 is part of Java EE 6.You must add
commons-fileupload-1.2.1.jar
in our project Build Path1.Configure web.xml file:
2. Create ManagedBean
3.jsf file(xhtml)