title: Httpsession management in springMVC
body: <p>I am new to spring MVC and started to make a sample application by doing what I learned. I am planning to implement Session management in spring MVC. I found <a href="http://www.javacodegeeks.com/2013/04/spring-mvc-session-tutorial.html" rel="nofollow noreferrer">this</a> one helpful.</p>
  
  <p>But I am not able to get it clearly. We add values to the session like</p>
  
  <pre><code>HttpSession session = request.getSession(false);
  session.setAttribute("key", value);
  session.setAttribute("key1",  value1);
  </code></pre>
  
  <p>and later on we fetch values based on the keys like </p>
  
  <pre><code>session.getAttrubute("key");
  </code></pre>
  
  <p>but in spring MVC, I could not see anything similar and it totally confuses me. </p>
  
  <pre><code>@Controller
  @SessionAttributes("thought")
  public class SingleFieldController {
  
      @RequestMapping(value="/single-field")
      public ModelAndView singleFieldPage() {
          return new ModelAndView("single-field-page");
      }
  
      @RequestMapping(value="/remember")  
      public ModelAndView rememberThought(@RequestParam String thoughtParam) {
          ModelAndView modelAndView = new ModelAndView();
          modelAndView.addObject("thought", thoughtParam);
          modelAndView.setViewName("single-field-page");
          return modelAndView;
      }
  
  }
  </code></pre>
  
  <p>In the above code  <code>@SessionAttributes("thought")</code> is totally confusing me like what is this <code>thought</code> defined also I have no need to return a <code>ModelAndView</code> since I am using <code>backbone.marionette.js</code> </p>
  
  <p>So how can I set values in a session and use them whenever required? I also have to convert the session object to my user defined object since, while returning the values to screen, I return only a List of UserDefined objects which are available in the session.</p>
  
  <p>So please help me to understand it better. Maybe I am confused with the way I used jsp / servlet.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Below is the controller I have and provided comments </p>
  
  <pre><code>package com.hexgen.puppet;
  
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.RequestBody;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.ResponseBody;
  import org.springframework.web.bind.annotation.SessionAttributes;
  
  import com.hexgen.puppet.CreatePuppet;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpSession;
  
  @Controller
  public class PuppetService {
  
      @RequestMapping(method = RequestMethod.POST, value = "/create")
      public @ResponseBody
      void createOrder(@RequestBody CreatePuppet request) {
          //logic to add/update values in session
      }
  
      @RequestMapping(method = RequestMethod.GET, value = "/list")
      public @ResponseBody
      List&lt;Puppet&gt; getGroups() {
          //logic to retrive objects from session and convert it as List and send it back
  
          return puppets;
      }
  
  
  }
  </code></pre>
  
  <p>and convert the object if needed and go on</p>