0

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

  }
}
Nishant
  • 222
  • 6
  • 21

2 Answers2

0

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.

Amr Gawish
  • 2,015
  • 1
  • 17
  • 29
  • Thanks for you reply. But I am not using FacesServlet my servlet Extends simple HTTPServlet Interface. Please find the code updated above – Nishant Sep 30 '13 at 08:29
  • It doesn't have to, FacesServlet is the one that is responsible of creating the FacesContext object, so if you want the FacesContext to not be null you will have to follow Faces Servlet Servlet Mapping URL Pattern – Amr Gawish Sep 30 '13 at 09:08
  • Please see my Answer above. I don't think I had any Issues with mapping. my web.xml file had all the FacesServlet mapped as /faces/* and others had their normal mapping. – Nishant Oct 02 '13 at 15:27
-1

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

Nishant
  • 222
  • 6
  • 21