Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams public class PasswordController implements ErrorController { private static final String ERROR_PATH = "/error"; public static final String IS_USER_LOGGED_IN = "isUserLoggedIn"; public static final String BAD_REQUEST_MESSAGE = "Bad Request ¯\\_(ツ)_/¯"; @Autowired private PasswordService passwordService; @Autowired private HttpSession httpSession; @RequestMapping(value = "/user/create/{name}/{password}", method = RequestMethod.GET) public String createNewUserAndPassword(@PathVariable final String name, @PathVariable final String password) { try { return passwordService.createNewUserAndPassword(name, password); } catch (UnsupportedEncodingException | NoSuchAlgorithmException e) { return ERROR_PATH; @RequestMapping(value = "user/login/{name}/{password}", method = RequestMethod.GET) public String loginUser(@PathVariable final String name, @PathVariable final String password) throws UnsupportedEncodingException, NoSuchAlgorithmException { // return passwordService.loginUser(name, password); final String result = passwordService.loginUser(name, password); if (result.contains(PasswordService.WELCOME_MESSAGE)) { this.httpSession.setAttribute("name", name); return result; @Override public String getErrorPath() { return ERROR_PATH; @RequestMapping("/error") public String error() { return BAD_REQUEST_MESSAGE; @RequestMapping(value = "getEnemies", method = RequestMethod.GET) public String getEnemies() { if (this.httpSession.getAttribute("name") != null) { return "enemies List...."; } else { return "(ง'̀-'́)ง";

It is quite simple as you can see, but the String I am saving here:

this.httpSession.setAttribute("name", name)

Can never be retrieved here as long as it is always null:

this.httpSession.getAttribute("name") != null

I debugged the code, and you can retrieve the string inmediately afterwards of the set. However, when it reaches the other method, it won't work at HttpSession object changes. (It even has another ID)

What can be the problem? Thanks

Assuming other handler(getEnemies()) when you meant other method

Yes the object changes since they both are different rest calls which run on different threads. I'm not sure what could be the root of HttpSession object which you have injected but what ever you are storing in it might not be persistent( probably local to that Thread). That is what making you to get null value in the other method.

stackoverflow.com/questions/41151804/… this might help you. I'm just a beginner so sorry if I couldn't get you right solution – Rakesh May 31, 2017 at 12:27 and you have made it a restcontroller but still you are sending a string(assuming it as view) .What is your intention? – Rakesh May 31, 2017 at 12:29

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.