what's the difference between get/setAttribute() when you call them from request and from getServletContext().
I noticed that you need
RequestDispatcher rd = request.getRequestDispatcher("/view.jsp");
rd.forward(request, response);
for the request to work, but you just need to navigate to another jsp or servlet in the application to use getServletContext().getAttribute().
But i don't understand what is going on behind.
The request.setAttribute() sets an attribute in the request scope and is thus only available within the same request/response cycle. The servletContext.setAttribute() sets an attribute in the application scope and is thus shared among all other requests/sessions. You don't want to do this when it concerns request-specific data, otherwise visitor Y would be able to see data of visitor X.
If you want some attribute to survive a redirect by response.sendRedirect() then the request scope is not suitable since a redirect basically instructs the client (webbrowser) to create a brand new HTTP request. You need to put the data in the session scope by session.setAttribute() rather than in the application scope (and preferably remove it in the subsequent request if it doen't need to be persistent during the whole session).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With