Why isn't getSession() returning the same session in subsequent requests distanced in short time periods?

后端 未结 3 1973
萌比男神i
萌比男神i 2021-02-02 16:47

I am sending a $.getJSON (HTTP GET) request twice (with different data), one after another (lets say we have request1 and request2). I can see in the developer tool

3条回答
  •  萌比男神i
    2021-02-02 17:06

    Just store your cookie(with JESSIONID) in Client , when your send subsequent request to Server , put the stored cookie in your request header field and send, then you will get the same session in the server end.

    CLIENT(IOS) store your cookie from response:

        NSHTTPURLResponse* httpURLReqponse = (NSHTTPURLResponse*) response;
        NSDictionary* allHeaders = [httpURLReqponse allHeaderFields];
        NSLog(@"Response Headers : %@ ", allHeaders);
        NSString* cookie = [allHeaders objectForKey: @"Set-Cookie"];
        DATA.cookies = cookie;      // Store the cookie
    

    CLIENT(IOS) send your subsequent request with cookie:

    // Put the stored cookie in your request header
    [(NSMutableURLRequest*)request addValue: DATA.cookies forHTTPHeaderField:@"cookie"];
    [NSURLConnection sendAsynchronousRequest: request queue:[NSOperationQueue mainQueue] completionHandler:nil];
    

    Not only for IOS client . Then , in the server end , you will get the same session:

    Server(JavaEE) GET HttpSession:

    // Get the session, print its hashCode. You will find out that it's same as previous.
    HttpSession session = ServletActionContext.getRequest().getSession();
    

提交回复
热议问题