Search code examples
sessionjakarta-eegwtcdi

GWT & Java EE SessionScoped bean not persisting


I'm playing w/ EE and want to persist a user session state. I have the session bean here:

@Stateful(mappedName = "UserSessionState")
@Named("UserSessionState")
@SessionScoped
@StatefulTimeout(value = 5, unit = TimeUnit.MINUTES)
public class UserSessionState implements Serializable
{
    private boolean hasPlayerId = false;
    private String playerId = "";

    public void setRandomPlayerId()
    {
        playerId = UUID.uuid();
        hasPlayerId = true;
    }

    public boolean hasPlayerId()
    {
        return hasPlayerId;
    }

    public String getPlayerId()
    {
        return playerId;
    }
}

And a servlet here (GameState is an Application Scoped bean that is working as expected, CustomExtendedHttpServlet is just a simple extension of HttpServlet)

public class NewUserJoined extends CustomExtendedHttpServlet
{
    @Inject
    protected GameState gameState;

    @Inject 
    protected UserSessionState user;

    @Override
    protected String doGetImpl(HttpServletRequest request, HttpServletResponse response, UserContext userLoginContext)
    {
        if (!user.hasPlayerId())
        {
            user.setRandomPlayerId();
        }        
        String userId = user.getPlayerId();

        if (!gameState.hasUser(userId))
        {
            gameState.addUser(userId, user);
            return "Hi, your ID is: " + user.getPlayerId() + ", there are " + gameState.getUserCount() + " other players here";
        }
        else
        {
            return user.getPlayerId() + " you're already in the game, there are: " + gameState.getUserCount() + " other players here";
        }
    }
}

I'm not sure what's going on, but whenever I call the New User Joined servlet from the same HTTP session, I get this response on the first call (as expected):

"Hi, your ID is: , there are 1 other players here"

Repeating the same servlet call in the same session gives me the same message: "Hi, your ID is: , there are 2 other players here" ...

It looks like a new instance of User Session State is getting created over and over. Am I doing this correctly?

EDIT 1: Here the code I use to send a request. It appears I'm getting a new session ID with each request, what could cause that?

RequestCallback callback = new RequestCallback()
{
    @Override
    public void onResponseReceived(Request request, Response response)
    {
        log(response.getText());
    }

    @Override
    public void onError(Request request, Throwable exception)
    {
        log(
            "Response Error | "
            + "Exception: " + exception);
    }
};

RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, SERVLET_URL);
rb.setCallback(callback);

try
{
    rb.send();
}
catch (RequestException e)
{
    log("Response Error | "
        + "Exception: " + e);
}

Solution

  • Figured out the issue,

    Turns out I had an old workaround in the GWT client that was changing the host to get around a CORS issue. Because the response didn't match up to the origin, the cookie wasn't getting sent with future servlet GET calls.