How can I pass variable from servlet to jsp? setAttribute
and getAttribute
didn't work for me :-(
Besides using an attribute to pass information from a servlet to a JSP page, one can also pass a parameter. That is done simply by redirecting to a URL that specifies the JSP page in question, and adding the normal parameter-passing-through-the-URL mechanism.
First create a PrintWriter object, which will produce the output on HTML page. Here response is HttpServletResponse object from doGet or doPost method. out. println("<html><body><table>...
Invoking a JSP Page from a Servlet. You can invoke a JSP page from a servlet through functionality of the standard javax. servlet.
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}
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