Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4101856
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:45:04+00:00 2026-05-20T20:45:04+00:00

I am looking around a few blogs, to try to find how to upload

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T20:45:05+00:00Added an answer on May 20, 2026 at 8:45 pm

    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:

      <?xml version="1.0" encoding="UTF-8"?>
      <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">
      
          <display-name>Your Project Name</display-name>
      
          <servlet>
              <servlet-name>Faces Servlet</servlet-name>
              <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
              <load-on-startup>1</load-on-startup>
          </servlet>
          <servlet-mapping>
              <servlet-name>Faces Servlet</servlet-name>
              <url-pattern>*.xhtml</url-pattern>
          </servlet-mapping>
      
      </web-app>
      

      The faces-config.xml must comply JSF 2.0 spec:

      <?xml version="1.0" encoding="UTF-8"?>
      <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">
      
      </faces-config>
      

    • 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 and xml*.jar are unnecessary for using alone the t:inputFileUpload component. You could leave them away.


    • Configure the Tomahawk extensions filter in web.xml. It’s the one who’s responsible for handling multipart/form-data requests which is required to be able to send files over HTTP.

      <filter>
          <filter-name>MyFacesExtensionsFilter</filter-name>
          <filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
      </filter>
      <filter-mapping>
          <filter-name>MyFacesExtensionsFilter</filter-name>
          <servlet-name>Faces Servlet</servlet-name>
      </filter-mapping>
      

      Note that the <servlet-name> must match the exact <servlet-name> of the FacesServlet as you’ve definied in web.xml.


    • Create a simple Facelet, upload.xhtml:

      <!DOCTYPE html>
      <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">
          <h:head>
              <title>Tomahawk file upload demo</title>
          </h:head>
          <h:body>
              <h:form enctype="multipart/form-data">
                  <t:inputFileUpload value="#{bean.uploadedFile}" />
                  <h:commandButton value="submit" action="#{bean.submit}" />
                  <h:messages />
              </h:form>
          </h:body> 
      </html>
      

      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:

      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;
          }
      
      }
      

    That should be it. Open it by http://localhost:8080/projectname/upload.xhtml.

    As to your concrete questions:

    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)?

    This is answered above.

    How can a servlet help me?

    It is able to process and control HTTP requests/responses. In a JSF environment, the FacesServlet already does all the work.

    Do i need a servlet to do this?

    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.

    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)?

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just spent a few hours looking around here but didn't really find a
Looking around Google I can see a few people have asked this but there
I have been looking around for this but couldn't find an answer anywhere, so
Been looking around and can't find a definite way to do this... I need
Been looking around a bit and I can't seem to find any help on
Hey i have been looking around and i cant find a way to get
I've been looking around for a log4j viewer that allows for multiple log files
I've been looking for a few days around the internet for tutorials or examples
There are a few questions around that circle around this question but I feel
I've been pulling my hair out over the past few days looking around for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.