Session is null on first request -



Session is null on first request -

i'm using spring-session , it. think i'm missing something. in application flow goes this: 1) user requests homepagecontroller , controller tries set attribute in request:

httpservletrequest request = ((servletrequestattributes) requestcontextholder.currentrequestattributes()).getrequest(); final string sessionids = sessionstrategy.getrequestedsessionid(request); if (sessionids != null) { final expiringsession session = sessionrepository.getsession(sessionids); if (session != null) { session.setattribute("attr", "value"); sessionrepository.save(session); model.addattribute("session", session); } }

as can see seek sessionid request-cookie, , if there's session id in repository utilize (add attribute). perfect, after second request. why? because if restart server cookie left old value, , first request not find session in repository. after response committed though cookie updated, second request correct. , here's question: wrong logic , how should 1 develop application in order back upwards first request too?

btw, here's sample application demonstrates problem:

https://github.com/paranoiabla/spring-session-issue

if wanting obtain session, should not utilize requested session id. requested session id that...what browser requests. problems using requested session (some of outlined):

if clear cookies , create request, no session requested browser. as pointed out if info store restarted , not persistent, requested session id invalid if session expires, requested session invalid

instead, should utilize session id:

final string sessionids = request.getsession().getid();

this utilize requested session id if valid, otherwise create new session , ensure session written response (i.e. included in response cookie).

spring-session

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -