Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Custom FacesServlet delegation

843844Nov 29 2007 — edited Nov 29 2007
Hi all.

For error/exception handling in my application, I have developed a CustomFacesServlet which delegates the request to the real FacesServlet. If any exception occurs during that operation it redirects to an error page.

My question is if it is possible do to a forward instead of a redirect ?
When I try to forward, I faces the problem that the FacesContext is null (found by a decompile)?

Console output from trying to render errorPage.jsp
07-11-29 09:31:37:015 CET] 00000030 SystemErr     R javax.servlet.jsp.JspException: Assertion Failed
[07-11-29 09:31:37:031 CET] 00000030 SystemErr     R 	at com.sun.faces.taglib.jsf_core.ViewTag.doStartTag(ViewTag.java:116)
[07-11-29 09:31:37:031 CET] 00000030 SystemErr     R 	at com.ibm._jsp._errorPage._jspService(_errorPage.java:81)
[07-11-29 09:31:37:031 CET] 00000030 SystemErr     R 	at com.ibm.ws.jsp.runtime.HttpJspBase.service(HttpJspBase.java:85)
[07-11-29 09:31:37:031 CET] 00000030 SystemErr     R 	at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
...........
Thanks in advance.

My code is as follows
public class CustomFacesServlet2 extends HttpServlet {
    
    private static final String INIT_PARAM_ERROR_PAGE = "errorPage";
    
    private FacesServlet delegate;
    
    private String errorPage;
    
    public void init(ServletConfig servletConfig) throws ServletException {
        getDelegate().init(servletConfig);
        errorPage = servletConfig.getInitParameter(INIT_PARAM_ERROR_PAGE);
    }
    
    public void destroy() {
        getDelegate().destroy();
    }
    
    public ServletConfig getServletConfig() {
        return getDelegate().getServletConfig();
    }
    
    public String getServletInfo() {
        return getDelegate().getServletInfo();
    }
    
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
        try {
            getDelegate().service(request,response);
        } catch(Throwable e) {
            ((HttpServletRequest) request).getSession().setAttribute("exception",e);
            redirectToErrorPage((HttpServletRequest) request, (HttpServletResponse) response);
        }
    }
    
    private void redirectToErrorPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        if (!"".equals(errorPage))  {
            response.sendRedirect(request.getContextPath() + errorPage);
        }
    }
    
    public FacesServlet getDelegate() {
        if (delegate == null){
            setDelegate(new FacesServlet());
        }
        return delegate;
    }
    
    public void setDelegate(FacesServlet delegate) {
        this.delegate = delegate;
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 27 2007
Added on Nov 29 2007
4 comments
178 views