I am working on a homework project that should implement a board game between 2 clients over an HttpServlet. I have couple of questions:
1) I read that HttpServlets must be stateless, however, for the sake of the game, I have to keep a lot of state(whose turn it is, the state of the board, etc). Do I have to keep this in the clients? Does the HttpServlet indeed have to be stateless, i.e. have no fields that track state?
2) I know that the clients will talk to the servlet via the doGet/doPost methods, but how can the servlet talk to the clients(for example, if player 1 just made a move and sent it to the servlet, the servlet has to tell client 2 what the move was).
Thank you
Also, if you can point me to a useful, simple example of a similar code online, I would be very grateful
HTTP protocol is stateless and hence the HttpServlet. You can maintain session state on server side using servlet api. Read Session Management tutorial.
There are several techniques like HTML auto-refresh, technologies like Comet and HTML 5's WebSocket
Servlets need to be able to handle multiple requests--they don't need to be stateless, but they must manage that state to avoid the typical multi-threading issues. Obviously if you have two users that need to access the same world you need a mechanism for that. Presumably the server will help keep track of game state, if for no other reason than to avoid cheating or malicious clients.
There are a variety of options; clients can poll for move updates, you can use long-polling, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With