How to handle multiple clients in JSP?
I'm implementing a game in JSP and Servlets. The game should support multiple players. It is obvious that each player ID is generated on the server side. But where do I store it on the client side, so I can retrieve it later (from within the servlet) when the client calls for the Servlet?开发者_JS百科
Sessions are handled automatically by the servlet framework, and you retrieve the session by calling request.getSession()
in a servlet.
The session is available in different ways once you start using a framework once you outgrow servlets (this happens quickly) and is framework-dependent.
Depends on how long you want the client to remember the player ID.
- During the session: The session is a good place
- During his subsequent visists: A (permanent) cookie is a good place
Session: request.getSession()
Cookie: request.getCookies()
and response.addCookie(cookie)
Session ids are usually stored in a cookie.
I'd be amazed if JSP doesn't have a session library that would take care of all that for you.
精彩评论