java - Why would I use a session over a private member in a Response Servlet? -
i building small poker app work on java skills. creating in texas hold'em format.
basically want maintain hand state in servlet handles ajax responses. guessing member variable in servlet not safe, whereas session should be. please explain correct implementation of solution?
public class pokerclientresponse extends httpservlet { private static logger log = logger.getlogger(pokerclientresponse.class); private static final long serialversionuid = 1l; private handstate handstate = null; public pokerclientresponse() { super(); } protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { //which best way maintain hand state? //get state session, increment httpsession session = request.getsession(); object obj = session.getattribute("handstate"); //advance hand state gethandstate().gotonextstate();
a servlet instance variable (unless it's threadlocal indeed not safe, because there single servlet instance serving request made mapping.
all need save/load hand state session, unique per user conversation1. remove hand state instance variable.
remove hand state getter. deal hand state inside request processing call, , pass hand state other methods need (if any).
your code closer this:
protected void doget(etc) { handstate state = (handstate) request.getsession().getattribute("handstate"); state.nextstate(); } although recommend using constant session attribute key.
1 different unique per browser window or tab; if have multiple windows or tabs open, may sharing same session.
Comments
Post a Comment