title: How to access the session variable in one controller to the other controller in SpringMVC?
body: <p>I had one login controller in that I define one session variable, now I want to access that session variable in all my remaining controllers in my application?</p>
  
  <p>this is my login controller code snippet</p>
  
  <pre><code>@RequestMapping(value = "/login", method = RequestMethod.POST,produces = "application/json")
  public @ResponseBody Map&lt;String, String&gt; validateUser(@RequestBody String loginParameters,HttpServletRequest request) throws Exception {
      try{
          HttpSession session=request.getSession();
          JSONObject json = new JSONObject(loginParameters.trim());
          String un=json.getString("username");
          session.setAttribute("username", un);
  </code></pre>
  
  <p>This is my ProfileController code snippet </p>
  
  <pre><code>@Controller
  public class ProfileController {    
  @RequestMapping(value = "/getUserData", method = RequestMethod.GET,produces = "application/json")
      public @ResponseBody Map&lt;String, String&gt; getUser(HttpServletRequest req) throws Exception {
          try{
  
              HttpSession session=req.getSession();
              String loggedInUser=(String)session.getAttribute("username");
  
              System.out.println("UserName is  "+ loggedInUser);
  </code></pre>
  
  <p>Now I want to access this session variable(username) in my another profile controller. I tried like this but I got null pointer expection in ProfileController.</p>