1

I am writing a spring boot application and have encountered the problem of sharing resources for different users. Simplified by example, it looks like this: there is one variable. You can assign a value to it through the form on the page. If the first user assigns the value hello java from one browser, then the second user will see the same value through another browser. I dont know how to make each user work with their own variable and their values do not overlap?

Controller:

@Controller
public class MessageController {
    private String message;

    @GetMapping(value = "/show_message")
    public String showMessage(Model model){
        model.addAttribute("message", message);
        return "message";
    }

    @PostMapping(value = "set_message")
    public String setMessage(@RequestParam(name = "newMessage") String newMessage){
        message = newMessage;
        return "redirect:/show_message";
    }
}

html page:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">

</head>
<body>
<p>Message value: <th:block th:utext="${message}"/></p>

<p>Enter a new message value</p>
<form method="POST" th:action="@{/set_message}">
    <input type="text" name="newMessage"/>
    <input type="submit" value="Send"/>
</form>
</body>
</html>
  • When i login user , i created a session , save that jwt in session, and when user login in that period , instead of creating jwt, he get that session, but now problem exists . it doesnt work for different user. Like if a user is created token, same session is sent to different user ;( – Rajanboy Jul 12 '22 at 08:34

1 Answers1

0

The rule of thumb is that controllers as well as services are not designed to maintain a state, particularly the client state.
Besides, Spring beans are by default singletons : they are shared among clients/requests. That explains the behavior that you notice.

To solve that issue you may use the in-memory HttpSession (old practice) but that is not advised any longer because it sticks the client/user to a specific server instance that has first served his request.
You should preferably either resend the data at each request (which may be cumbersome to do) or better store the data in an in-memory database such as Redis.
That is fast and it doesn't stick the client to a specific instance of your spring boot application.

With Spring, a good association with the in-memory database usage to handle the user state is using HttpSession but Spring Session backed to a database (Redis or another).
For how to use the Session with Spring MVC, you have that good post.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • 0. Thanks for the link! Really helped 1. Did I understand correctly that Spring HttpSession already uses an in-memory database like Radis? 2. And what if I simply add annotation @Scope("session") to the controller? Everything worked with her too – Alexander Tukanov Feb 17 '20 at 07:18
  • 0.You are welcome :) 1. Not exactly. Spring uses the in-memory of the Tomcat server . While you can use that annotation with Spring Session backed to a db. Using in-memory server http sessions is fine for a test or a very simple application that you (or your company) host. But if you need scalability (adding or removing an instance of the application) or failover (make an instance take the relay of another that has failed) you may be stuck. – davidxxx Feb 17 '20 at 07:50
  • ... Similarly with cloud hosting you will also have some limitations. That link may be helpful (https://www.oreilly.com/ideas/stateless-processes-in-cloud-native-apps). You have several points and one of them is "stateless processes". These are advanced principles but you will necessarily be confronted to that later. Understanding consequences of using the in-memory server session now is not a bad thing. – davidxxx Feb 17 '20 at 07:52