What is Spring MVC Controller?
It helps to:
- process user requests based on the requested URL
- call Service Manager => DAO for some database actions, such as insert/update/delete/query
- output the query result to valid view (JSP)
What if my application need to be multiple users?
By default, scope of Spring controller is Singaton, only one object (one controller) will be created no matter how many user requests. So, if the controller stores the user login information as controller’s variables, every requested users will use and see the same login information.
How to fix it?
- We can define the user login Java bean (most likely we will store the user login as Java bean) as:
like our Service Manager Java classes. Please note that it is not a must to define all service managers as session scope with ScopedProxyMode. If the data is shared and will be the same to all users, you can ignore it for this data.
In this case, scope of the controllers can still be Singaton. But, the service managers is defined as session scope with ScopedProxyMode.
- You can simply define the controller as session scope. But, a new controller object will be created for every single user session, it will consume memory.
- Define your controller as request scope and service manager as session scope. Similar to approach 2, a new controller object will be created for every single user session, it will consume memory as well.
- Old school way, you can simply store the login information into HttpSession. It is not suggested to use this approach, because you need to code more such as checking NULL value.
What is View:JSP?
It helps to:
- render the model data
- generate HTML output, it can also generate other formats, such as CSV.
- include CSS and JS files
Please download here to get the controllers, JSP and resources files.