servletsjsf-2primefacesjspx

PrimeFaces fileupload


I am using jsp/Servlet into a project and I have decided to migrate to JSF using PrimeFaces ,I have faced a problem while trying to upload file using PrimeFaces fileupload control then when I have configured it into web.xml it worked well , the problem now that all old jsp file upload way is not working:

 DiskFileUpload upload = new DiskFileUpload();
 List items = upload.parseRequest(request);

Solution

  • Indeed, a HTTP request can be parsed only once. The client ain't going to send it for a second time if you need to parse it twice. Your problem indicates that the PrimeFaces file upload filter is for some reason also invoked on a plain JSP/Servlet request and thus parses the upload for JSF before plain JSP/Servlet get chance to parse it for own use. This should not happen.

    You need to map the PrimeFaces file upload filter on JSF requests only, not on plain JSP/Servlet requests. You normally achieve that by mapping it to the FacesServlet.

    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>
    

    (the <servlet-name> must be exactly the same value as is been used in <servlet> definition of the FacesServlet class; the <filter-name> is obviously exactly the same value as is been used in <filter> definiton of the file upload filter class)

    Or, if that old JSP/Servlet file upload approach is been used straight in a JSF page for some unobvious reason, then you need to map the PrimeFaces file upload filter on a more specific URL pattern covering only the pages containing the PrimeFaces file upload component.

    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <url-pattern>/upload.xhtml</url-pattern>
    </filter-mapping>
    

    (if you've mapped the FacesServlet on for example *.jsf instead of *.xhtml, then you should obviously change the URL pattern to /upload.jsf)

    Note that you can specify multiple <url-pattern> entries on a single filter mapping, which is useful for the case that you've multiple pages containing the PrimeFaces file upload component.

    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <url-pattern>/upload1.xhtml</url-pattern>
        <url-pattern>/upload2.xhtml</url-pattern>
        <url-pattern>/upload3.xhtml</url-pattern>
    </filter-mapping>