javajakarta-eeservletsoracle-adfsessioncontext

Null Pointer Exception in JSFUtils.resolveExpression("#{sessioncontext}") in a Servlet


I am working with Servlets and I am trying to retrieve Session Context using the JSFUtils.resolveExpression("#{sessioncontext}") method in ADF but it is giving me a Null Pointer Exception. What is wrong with the above used method and Is there another way to retrieve sessioncontext in my Servlet?

Thanks,

Edit: Please find the Code below,

public class MyServlet extends HttpServlet {


public final void init(final ServletConfig config) throws ServletException {
    super.init(config);
}

public void doGet(HttpServletRequest request,
                HttpServletResponse response)
        throws ServletException, IOException
{
    response.setContentType("text/html");
  PrintWriter out = response.getWriter(); 
 SessionContext session = (SessionContext) JSFUtils.resolveExpression("#{sessioncontext}");
  //more code below

  }
}

Solution

  • I replaced the above line of code with the code below and It works fine :) I was not having acesss to FacesContext which was throwing a Null Pointer

    FacesContext facesContext = FacesContext.getCurrentInstance();
        if (facesContext == null) {
            FacesContextFactory contextFactory =
                (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
            LifecycleFactory lifecycleFactory =
                (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
            Lifecycle lifecycle =
                lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
            facesContext =
                    contextFactory.getFacesContext(request.getSession().getServletContext(),
                                                   request, response,
                                                   lifecycle);
            // Below is an Inner Class which extends FacesContext to use its below protected method
            AbstractInnerFacesContext.setFacesContextAsCurrentInstance(facesContext);
        }
    

    Now Since I have FacesContext I can easily retrive SessionContext from this using the same logic used by resolveExpression Method :) (Yay!!!)