Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass variables from servlet to jsp

Tags:

jsp

servlets

How can I pass variable from servlet to jsp? setAttribute and getAttribute didn't work for me :-(

like image 829
Developer Avatar asked Aug 31 '10 12:08

Developer


People also ask

How do you pass attributes between servlets and JSP?

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.

How pass data from servlet to HTML?

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>...

Can we call servlet from JSP?

Invoking a JSP Page from a Servlet. You can invoke a JSP page from a servlet through functionality of the standard javax. servlet.


1 Answers

It will fail to work when:

  1. 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); 
  2. 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} 
like image 110
BalusC Avatar answered Sep 23 '22 10:09

BalusC