Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
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=-
Boot Laces: 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
My guess would be that request != null but request.getCookies() returns null.
HttpServletRequest.getCookies
public Cookie[] getCookies()
Returns an array containing all of the Cookie objects the client sent with this request. This method returns null if no cookies were sent.
Returns:
an array of all the Cookies included with this request, or null if the request has no cookies
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=-
Boot Laces: 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.
Artur Signell:
Boot Laces: 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()) {