0

I have a servlet which makes a database query. Now I need to forward the final resultset to my jsp page and then display the rows using display-tag. How to do this? How to transfer the resultset from servlet to jsp. Can't forward resultset directly as it is not serializable.

What if I have to transfer two resultsets from servelet to jsp?

kunal18
  • 1,935
  • 5
  • 33
  • 58

1 Answers1

0

An object doesn't have to be serializable to be stored in a request attribute. The HttpServletRequest object and its attributes live in memory.

And displaytag uses a collection (a List, most of the time) of objects which should respect JavaBeans conventions.

So the process is simple:

  1. The servlet executes a database query
  2. It iterates through the ResultSet, and creates a List<Foo> containing the data retrived by the query
  3. It stored this list as an attribute of the request: request.setAttribute("foos", fooList);
  4. It forwards the request and response to the JSP, using the RequestDispatcher
  5. The JSP uses the displaytag to display the content of the ${foos} as a table.

If you have to transfer two resultsets, execute 2 requests, build two lists, store them in 2 request attributes, and use the displaytag twice in the JSP.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255