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!

Lee Theobald
  • 8,461
  • 12
  • 49
  • 58
  • 1
    See this answer http://stackoverflow.com/questions/18189337/inject-httpservletrequest-in-cdi-sessionscoped-bean. – rdcrng Aug 15 '13 at 13:56
  • That's pretty much what I was looking for. I'm on JavaEE6 so I'll have to do it the long winded way. – Lee Theobald Aug 15 '13 at 14:03

0 Answers0