0

I've got a app using the JavaEE 6 stack, running on TomEE. I want to inject the current HttpServletRequest & Reponse so I've been doing something along the lines of the following:

@RequestScoped
@Named("loginAction")
public class LoginAction implements Serializable {

  @Context
  private HttpServletRequest httpRequest;

  @Context
  private HttpServletResponse httpResponse;

  public void doLogin() {
    // Use httpRequest & httpResponse here
  }

}

I then have a commandButton on a JSF page calling loginAction.doLogin for it's action. The trouble is that when doLogin is called, I'm getting NPE's to do with httpRequest/httpResponse. They come back as ThreadLocalHttpServletRequest/Response objects but when I call any get method (e.g. httpRequest.getRequestURI()) those calls throw the NPE's.

I've seen many responses saying it works when it's moved onto a method. E.g.

public void doLogin(@Context HttpServletRequest httpRequest, @Context HttpServletResponse httpResponse) {
  // ...
}

But if I do that, I won't be able to access the doLogin method from the JSF page.

Can anyone shed some light onto:

  1. Why I'm seeing the NPE's?
  2. A way in which I can inject the HttpServletRequest into my bean?

Cheers for any help!

4

0 回答 0