What is the alternative of file browse in JSF?

We have been developing a web application in JSF using Netbeans 6.9.1. It is about shopping. Therefore, many a times we have to deal with images. Images need to be uploaded into MySql database. Doing so, requires some mechanism to browse image files but we found that JSF does not support file browse directly. we could have used HTML file browse <input type="file".../> but for that we need to obtain the external context from Servlet to access it's value in JSF managed bean from request parameters.

HttpServletRequest request=(HttpServletRequest)FacesContext .getCurrentInstance().getExternalContext().getRequest();

which may not be one of the convenient, suggested and best methods I think [ and also, it may not be the approach to mix HTML with JSF components] and if we were to use HTML file browse, we would have to maintain it's view state which is utmost important in JSF and HTML file browse by nature doesn't maintain it's view state and also, we can only obtain the file name from HTML file browse. In many circumstances, it is essential to obtain the absolute file path. Is it possible to retrieve the absolute file path from HTML file browse and make it maintain it's view state? Which approach should we follow?


It's indeed not the best practice to fiddle with raw Servlet API in your JSF code. You should try to minimize the javax.servlet imports/dependencies as much as possible. An ideal managed bean has none of those imports. So forget that part.

The standard JSF component library indeed doesn't ship with a component which represents a <input type"file">, for the simple reason that the standard Servlet API, which JSF is based on top of, didn't provide any facilities to parse multipart/form-data requests. Only since Servlet version 3.0 this is supported with the new HttpServletRequest#getParts() method. But at that point JSF 2.0 was already designed and finished. We're currently at JSF 2.1, which finally officially requires a minimum of Servlet 3.0, so a file upload component should be possible with just the standard APIs. Right now there are two ongoing spec requests open to include a file upload component in JSF 2.2:

    <li>Add h:inputFile component</li> <li>Ajax fileupload capabilities</li> </ul>

    Until then, your best bet is to grab a 3rd party component library. If you are looking for a really barebones component which doesn't render any additional JS/CSS fanciness, then I'd recommend to pick Tomahawk's <t:inputFileUpload> component. You can find in the following articles how to install/configure it (it's unclear what JSF version you're using, so I'm mentioning it for both JSF 1.x and 2.x):

      <li>Uploading files in JSF 1.x with Tomahawk</li> <li>Uploading files in JSF 2.x with Tomahawk</li> </ul>

      Please note that your question as in how to retrieve the absolute path made my neck hairs to raise somewhat. You should <strong>not</strong> be interested in the file's absolute path, but in the file's contents. For a more detailed explanation, see also How to get the file path from HTML input form in Firefox 3.


      Without a framework you could use the Apache commons package, for example. Or Tomahawk...Just Google search for those two as there are easy tutorial online to get you started:

      tomahawk OR commons-fileupload

      By the way the method you have listed to obtain a request in JSF is very popular and i also use it in my JSF application for several different things. So, give it a try. Hope this helps!


  • What is the Difference between system initialization and update in hybris?
  • Issue declaring extern class object
  • Force a controller to always act as a proxy to a model in Ember
  • What could cause numpy.nanstd() to return nan?
  • C# dynamic support?
  • applying background to first, and every third row in a dynamically generated table
  • Connection String in a WIX Custom Action Parameter
  • Passing matrices from function to function in MATLAB
  • Strange multiple assignment error in C++
  • LDA: Why sampling for inference of a new document?
  • Using recursion to search all combinations of elements in an array of integers
  • jParallax trouble
  • Symfony2 redirect to https route fails (uses wrong port)
  • Allocating a 2D contiguous array within a function
  • Jquery resizable reposition handle after scroll
  • PostgreSQL 9.x - pg_read_binary_file & inserting files into bytea
  • IE11 textarea loses focus if another textarea is disabled
  • Refactoring advice: maps to POJOs
  • HighCharts - Show tooltip on column where value is 0 or null
  • Making header act like google chrome mobile app address bar
  • C++ Single function pointer for all template instances
  • Can I have a variable number of URI parameters or key-value pairs in Laravel 4?
  • DIV instruction jumping to random location?
  • Diff between two dataframes in pandas
  • JBoss External Properties Files in Classpath
  • Connect .sks to skscene.h
  • Moving Android View and preventing onDraw to be called over and over again
  • xtable package: Skipping some rows in the output
  • Assign variable to the value in HTML
  • Declaring variable dynamically in VB.net
  • Test if a set exists before trying to drop it
  • Why does access(2) check for real and not effective UID?
  • Checking free space on FTP server
  • Django: Count of Group Elements
  • script to move all files from one location to another location
  • ILMerge & Keep Assembly Name
  • Join two tables and save into third-sql
  • Symfony2: How to get request parameter
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • SQL merge duplicate rows and join values that are different