0

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.

SHIVA
  • 646
  • 1
  • 8
  • 13
  • 3
    use `sesssion.getAttribute("key")`. – Jobin Jan 22 '18 at 12:06
  • session.getAttribute("username") – Ori Marko Jan 22 '18 at 12:06
  • Within the same controller, we can access session.getAttribute("key") but outside of this controller how can we get the same session reference? – SHIVA Jan 22 '18 at 12:18
  • 1
    Did you try it? That's why it is called session, it will be available through out the user session. – Jobin Jan 22 '18 at 12:25
  • Yes, I tried, I got null pointer exception. – SHIVA Jan 22 '18 at 12:28
  • You need to annotate your controller class @SessionAttribute("your_session_variable_name") to store variable in Session in Spring MVC, then when we try to get fetch it from session, it won't be null – Yogi Jan 22 '18 at 12:56
  • 1
    [Following link contains the complete steps of how to manage sessions in spring mvc](https://stackoverflow.com/questions/18791645/how-to-use-session-attributes-in-spring-mvc) – Kirat Kumar Jan 22 '18 at 12:59

2 Answers2

0

I found the solution to my requirement. actually, my requirement is to access the session value from the login controller to profile controller. So What I did is instead of accessing the session variable in profile controller, just I am calling a method in login controller that will return my required value.

@Controller
public class LoginController {
private HttpSession session=null;
    @RequestMapping(value = "/login", method = RequestMethod.POST,produces = "application/json")
public @ResponseBody Map<String, String> validateUser(@RequestBody String loginParameters,HttpServletRequest request) throws Exception {
try{
    session=request.getSession();
    JSONObject json = new JSONObject(loginParameters.trim());
    String un=json.getString("username");
    session.setAttribute("username", un);
}catch(Exception e)
{
}

}
public String getUserName()
{
return session.getAttribute("username");
}
}

ProfileController

@Controller
public class ProfileController {
@Autowired
private LoginController loginControllerObj;
@RequestMapping(value = "/getUserData", method = RequestMethod.GET,produces = "application/json")
public @ResponseBody Map<String, String> getUser(HttpServletRequest req) throws Exception {
    try{


        String loggedInUser=loginControllerObj.getUserName();

        System.out.println("UserName is  "+ loggedInUser);

As per my understanding, in my question. I got null pointer exception in Profile controller this is because of if we want to access the session then we need to call the request.getSession() the method that will return if any session is associated with the request then it will return that one if not then it creates a new session.the same concept in my profile controller also applied. Instead of accessing the existing session in will create the new session because of both are two different requests. For this reason, I follow the above code get rid of my requirement.

SHIVA
  • 646
  • 1
  • 8
  • 13
0

if it is about current logged in username then you just pass Principal parameter to controller method.

  @RequestMapping(value="/login", method = RequestMethod.GET)
  public String methodName(ModelMap model, Principal principal ) {

  String name = principal.getName(); //get logged in username
  model.addAttribute("username", name);
  return "page";

}

Mr code.
  • 307
  • 2
  • 17