HttpServletRequestListener and threading issue?

Hi,

Using Java 1.6, Jetty 6.1.22 and Vaadin 6.2.2.

I seem to be having a threading issue, perhaps someone can help. Here is the code:

public MyApplication extends Application implements HttpServletListener {
    private volatile HttpServletRequest request;
    private volatile HttpServletResponse response;

   @Override
   public void init() {
       if(loginCookie()) {
         ....
         ....
       }
   }

  @Override
   public void onRequestStart(final HttpServletRequest request, HttpServletResponse response) {
       this.request = request;
       this.response = response;
   }

  private boolean loginCookie() {
    if(request != null) {
      for(Cookie cookie : request.getCookies()) {
          ....
          ....
      }
    }
  }

What happens, is that the first time I hit the application, I get a null pointer at line:

for(Cookie cookie : request.getCookies())

Which indicates that request == null!!! However, this is strange since there is a guard check before that line

if(request != null)

Therefore, I’m lead to believe that there is a threading/timing issue going on where perhaps the init method is being called before the onRequestStart has finished (or even been called…)

When I refresh the page, it works, in the sense that the request is not null.

-=bootlaces=-

My guess would be that request != null but request.getCookies() returns null.

Ah,

but there are cookies - they persist for one week. It wouldn’t explain why on a refresh it works - nothing in my code sets this particular cookie until log in…

-=bootlaces=-

But the JSESSIONID cookie is probably set by the server on the first request so on subsequent requests this cookie is available and getCookies() no longer returns null.

EDIT: Use a debugger and see what happens. Then you should find out what really is going on.

Okay, thank you. By checking for request.getCookies() != null as well, then it works.


   if ((request != null) && (request.getCookies() != null)) {
            for (final Cookie cookie : request.getCookies()) {