Pergunta

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

  }
}
Foi útil?

Solução 2

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!!!)

Outras dicas

Your servlet can't access FacesContext if you don't follow the Faces servlet mapping URL.

For instance in any oracle ADF application the default JSF Servlet URL Mapping is /faces/* So if you put your servlet to have the same mapping, the Faces Context will not be null.

for instance, make your Servlet URL Mapping be like the following /faces/myCoolServlet and it'll be working fine.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top