title: Maintaining session state across two http different requests in an application
body: <p>I have a scenario where I want to store session information across multiple sessions in Application # 2. We have two applications deployed on a tomcat server. Our use case is as follows:</p>
  
  <p>A. Web Application # 1 makes a HTTP Post request to Application # 2 using a HTTP Rest  Client. POST request contains a JSON http request body encapsulating the data to be send to Application # 2. The code block is as follows:</p>
  
  <pre><code>RestTemplate template = new RestTemplate();
  final SearchCustomer customer = new SearchCustomer();
  restTemplate.execute(
      SEND_CUSTOMER_PROFILE, HttpMethod.POST,
      new SearchRequestCallback(searchCustomer), null);
  </code></pre>
  
  <p>The request callback function is</p>
  
  <pre><code>static class SearchRequestCallback implements RequestCallback {
  
  
    /**
     * Write a JSON response to the request body.
     */
      @Override
      public void doWithRequest(ClientHttpRequest request) throws IOException {
        HttpHeaders httpHeaders = request.getHeaders();
        List&lt;MediaType&gt; acceptableMediaTypes = new LinkedList&lt;&gt;();
        acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
        httpHeaders.setAccept(acceptableMediaTypes);
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        request.getBody().write(
            new Gson().toJson(this.searchCustomer).getBytes(StandardCharsets.UTF_8.displayName()));
      }
    }
  </code></pre>
  
  <p>The second application has a Spring controller with the following set up</p>
  
  <pre><code>@Controller
  public class SearchCustomerController {
  
    /**
     * Builds customer profile knowledge graph.
     * 
     * &lt;p&gt;This is invoked as an synchronous request.
     */
    @RequestMapping(value="/searchProfilePayload.go", method=RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    public void constructSearchCustomerProfileKnowledgeGraph(
        @RequestBody final SearchCustomer customer, HttpServletRequest request) {
      UserContext userContext =
          (UserContext) request.getSession().getAttribute("userContext");
      if (userContext == null) {
        // Perform heavy operation to fetch user session.
        userContext = UserContextHelper.getUserContext(request);
        request.getSession("userContext", userContext)
      }
      userContext.setCustomerProfile(customer);
    }
  }
  </code></pre>
  
  <p>When I make a call to another URI within the application # 2 say via browser, I want it done in such as way that the session attributes are retained when making this call. Is there a way to do that?</p>
  
  <p>I know about <a href="http://www.journaldev.com/1907/java-servlet-session-management-tutorial-with-examples-of-cookies-httpsession-and-url-rewriting#servlet-url-rewriting" rel="nofollow noreferrer">URL rewriting</a> that stores <code>JSESSIONID</code>in the cookie, but I don't think how you can set the value when making a rest call, and using the same <code>JESSIONID</code> to maintain session attributes.</p>
  
  <p>Is there a better way to do this? These have no answers. I have looked at these links, but none seem to answer my question.</p>
  
  <p><a href="https://stackoverflow.com/questions/13965701/http-and-sessions">HTTP and Sessions</a><br>
  <a href="https://stackoverflow.com/questions/95655/comparison-of-ways-to-maintain-state">comparison of ways to maintain state</a></p>