I have several scenarios where a servlet needs to pass data to a form in a JSP from the retrieved records from the database. Currently, I'm storing this information in the request, using a RequestDispatcher to forward to the page, and all is well.
However, this is not fitting with the PRG pattern (AFAIK), and of course means that refreshing of the resulting JSP means re-running of the servlet, which is undesirable.
I could of course store these values in the session, but that would mean clearing them afterwards, and even using the session seems like a bit of a hack for displaying a record from the database.
I am simply wondering what would be the best practice in this situation? Should I continue using the request, use the session, or some other technique?
Thanks in advance.
Edit: After reading several articles and stack overflow answers, I can find nowhere that presents any other option than using the request and a dispatcher when passing data from a servlet to a JSP. It doesn't seem right to me, but neither does the session. Can anybody shed some light on this?
I'm not sure I fully understand the problem, but two patterns are best practices:
So what it means is that you should have:
/product?id=<generatedId>
or /product/<generatedId>
Of course, you could choose to redirect to some other page, like the list of products for example.
If what bothers you is to use the request to store data when forwarding from the servlet to the JSP, then that shouldn't bother you: it's the only clean way to do it. The data will be scoped to the request only, and be garbage-collected when the request has been processed.