JSF – How To Get HttpServletRequest in JSF Component

The below shows you how to get a HttpServletRequest object in a JSF component via a FacesContext object.

Here is a trick how to do this.

MyComponent.java
package com.bytenota.web.jsf;

import java.io.IOException;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

public class MyComponent extends UIComponentBase {
    
    @Override
    public void encodeEnd(FacesContext context) throws IOException {
        // get a HttpServletRequest from FacesContext object
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
    }

    @Override
    public String getFamily() {
        return "com.bytenota.web.jsf.myComponent";
    }
}

0 0 vote
Article Rating
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x