0

Trying to build an authenticated page with components that request data about the authenticated user from a backend service. The idea is that the authN response from the backend be as light as possible (e.g. a userID and authToken) and the components on the page would use those to make subsequent calls to retrieve further details, but I'm not quite sure the best way to share that data/context for all components on the page to access.

What's the best way to have that context shared?

daitienshi
  • 181
  • 1
  • 3
  • 11

1 Answers1

0

You have a few options. You can set variables in components that scope them to apply beyond just that JSP. See http://www.java2s.com/Code/Java/JSTL/JSTLSetVariablesScope.htm

With scoping you can make variables accessible with session scope or request scope so that once set any other components that are processed during the same request will be able to access the variables.

So you could make your authentication component set variables with request scope for the userID and authToken, and then subsequent components would be able to access those variables and use them for their needs. An example is shown in https://stackoverflow.com/a/21352909/230055

Community
  • 1
  • 1
Shawn
  • 8,374
  • 5
  • 37
  • 60
  • What will be the best way if there is a dispatcher in the setup ? Making all such pages as non-cacheable ? – Abhishek Apr 20 '16 at 18:45
  • 1
    Yes, if your pages are accessing user-specific data, you will won't want to dispatcher cache them. You can specify this in dispatcher config. Or, you can also add `response.setHeader("Dispatcher", "no-cache");` server side to prevent dispatcher caching. Or you could redesign the pages to use AJAX calls or personalization features to fetch the user-specific pieces (via client side JavaScript), while still dispatcher-caching the pieces that are generic to everyone. – Shawn Apr 21 '16 at 18:29
  • Using this option, like the comments in the example mention, there is a dependency on the load order of the components. If there is a container component with other components inside, is it safe to say that the container will always load first, i.e. set the variables and scope in the container for the other components to access? – daitienshi Apr 27 '16 at 17:14
  • JSPs are processed top to bottom, so you just need to ensure the code (tag) you need to run first comes first. So make the container run the code before any references to other components. – Shawn Apr 27 '16 at 20:25