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?
this is my login controller code snippet
@RequestMapping(value = "/login", method = RequestMethod.POST,produces = "application/json")
public @ResponseBody Map<String, String> 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);
This is my ProfileController code snippet
@Controller
public class ProfileController {
@RequestMapping(value = "/getUserData", method = RequestMethod.GET,produces = "application/json")
public @ResponseBody Map<String, String> getUser(HttpServletRequest req) throws Exception {
try{
HttpSession session=req.getSession();
String loggedInUser=(String)session.getAttribute("username");
System.out.println("UserName is "+ loggedInUser);
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.