How can I pass variable from servlet to jsp?
setAttribute
and getAttribute
didn't work for me :-(
views:
40answers:
3You could set all the values into the response object before forwaring the request to the jsp. Or you can put your values into a session bean and access it in the jsp.
Use request.setAttribute(..)
and then getServletContext().getRequestDispatcher("/file.jsp").forward()
. Then it will be accessible in the JSP.
As a sidenote - in your jsp avoid using java code. Use JSTL.
It will fail to work when:
You are redirecting the response to a new request by
response.sendRedirect("page.jsp")
. The newly created request object will of course not contain the attributes anymore and they will not be accessible in the redirected JSP. You need to forward rather than redirect. E.g.request.setAttribute("name", "value"); request.getRequestDispatcher("page.jsp").forward(request, response);
You are accessing it the wrong way or using the wrong name. Assuming that you have set it using the name
"name"
, then you should be able to access it in the forwarded JSP page as follows:${name}