There are really two solutions I can think of.
One is if you really just need a quick and dirty way to pass some data from one component to another during your request processing, you can use the setAttribute and getAttribute methods on the javax.servlet.http.HttpServletRequest object (request). This lets you assign any object to a named attribute of the request object which any component would then be able to grab. This will only last for the duration of a single request. This is really just a hack and likely indicates your components and/or data boundaries are not well defined.
The second method is to actually persist the data into a structure in the JCR. There is no single property that can do this; based on your map template you would probably need a collection of nodes, each with a string property for the key and a string property for the value. You would build your map by reading in the list of child nodes and grabbing the key and value properties from each node, and persist your map by updating the same.
Thanks alot ffor your response.
I feel the second method is structured one. I would like to go with this. I need a small help here. I haven't created any node from the jsp using create node. so can you guide me to create the collection of nodes in the JCR?
Thanks in advance.
1 person found this helpful
I would strongly think about the thread safety and security issues around having a component's default script write data to the repository. Does that work if multiple threads are accessing the component? Does it work when the anonymous user is hitting the page?
There are ways to make it work, but it is the type of thing that can work fine in local development and then blow up when you move the code into production.
Raja- to your question about the JCR API, I'd suggest reading the specification.
Depending on your data you may be able to write the data to a JSON formatted string, attach that data to the servlet request with request.setAttribute("mydata",data); then in the other component get the 'mydata' attribute and parse the JSON back to whatever you need. This at least keeps everything in memory, I'd agree with Justin that writing to the repository to solve this kind of problem does not seem the most efficient and low cost method and may indeed be error prone depending on implementation.
This has kind of a bad smell to me. You have two separate components that need to communicate some set of data? I think to do this, you want to use a method that fully decouples those components, otherwise they depend on one another to be present on a page...this is likely something that will not be enforced by the source code. I didn't get the impression there was much of a security issue with the "shared" data, so what about this approach...
Regardless of specific approach, I would think you'd want to be careful to decouple these two components, and trying to force them to share data server-side during the request resolution seems fishy.