JSF file download NullPointerException with View Parameter

Hope I get it straight:

I am trying to create a JSF 2.0 page optimized for mobile use on Tomcat 7. So I am using Mojarra 2.1.6 and PrimeFaces (Core 3.1.1 and Mobile 0.9.1).

My script:

  • The user visits a simplified url eg. www.someurl.eu/view.xhtml/12345678
  • ServletFilter rewrites url on www.someurl.eu/view.xhtml?id=12345678
  • URL parameter is provided for ManagedBean
  • The required data is evaluated and published to my view.xhtml
  • On my view.xhtml a button is provided to download

My problem:

By clicking the button, the server shows me the NPE when the ManagedBean is set to @RequestScoped. My guess is that a button click starts a new request, my URL parameter is lost and the download file is no longer available. I don't know if I'm right, but if so, I don't know how to solve this graceful one.

My ManagedBean:

public class ViewBean {

// Elements
private StreamedContent file;

// Getter and Setter
public StreamedContent getFile() {
    return file;
public void setFile(StreamedContent file) {
    this.file = file;

// Init
public void init() {

    //read URL-Parameter
    urlSdbac = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id");

    // validate
    if(urlSdbac == null || urlSdbac.equals("") || urlSdbac.length() != 8) {
        urlSdbac = "invalid";
    // get Data
    else {
        SdbCustomerData cd = getFileMetadata(urlSdbac);

        if(cd == null) {
            this.name = "invalid code";
            this.language = "no language options found";
        else {
            // prepare File for Download
            this.file = new Helper().getWebDavFile(cd.getCid(), cd.getName());


My view.xhtml:

  <pm:page title="Download">
            <p:commandButton  value="Download" ajax="false">
                    <p:fileDownload value="#{viewBean.file}" />


Last but not least, my filter:

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;

    String url = request.getRequestURI();

    Pattern sdbac = Pattern.compile("[A-Za-z0-9]{8}");
    Matcher fit = null;

    // check Page
    if(url.contains("view.xhtml/") && (!url.contains("?id=") && !url.endsWith("view.xhtml")) ) {
        String code = url.substring(url.indexOf("view.xhtml/")+11, url.length());
        // validate Param?
        fit = sdbac.matcher(code);
        if(fit.matches()) {
            // redirect
            response.sendRedirect("/getSDS/view.xhtml?id=" + code);
        else {
            // redirect
    else {


My filter matches any URL <url-pattern>/*</url-pattern>

I've also tried setting ManagedBean to @SessionScoped. If I do this, the download is fine, but the user is unable to change the URL due to persistent data in the session managed by the ManagedBean session.

I may be too blind to see the obvious solution.

Any help would be appreciated! Thanks in advance!

PS Exception




source to share

1 answer

Use the viewing area.



See also:



All Articles