CONTROLLER : Spring MVC Controller & VIEW : JSP

What is Spring MVC Controller?

It helps to:

  1. process user requests based on the requested URL
  2. call Service Manager => DAO for some database actions, such as insert/update/delete/query
  3. 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?

  1. We can define the user login Java bean (most likely we will store the user login as Java bean) as:

@Scope(value=”session”, proxyMode=ScopedProxyMode.TARGET_CLASS)

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.

 

  1. 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.

 

  1. 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.

 

  1. 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:

  1. render the model data
  2. generate HTML output, it can also generate other formats, such as CSV.
  3. include CSS and JS files

 

Please download here to get the controllers, JSP and resources files.

Please rate this