Edit Edit

Java Servlet FAQ

How do you pass information from a servlet to a redirected servlet?

1) Pass the information as request parameters via the URL's query string. For example, to pass a name string to another servlet:

String querystring = "?nameparam=" + name;
res.sendRedirect("/servlet/MyServlet" + querystring);

2) To pass complex data to a redirected servlet, store the data in a HttpSession object:

public class SomeComplexData
    // some complex data to pass to another servlet

In the servlet's doGet() or doPost() method:

// create the data to pass
SomeComplexData data = new SomeComplexData;

// store the data in the session
HttpSession session = req.getSession(true);
session.putValue("my complex data", data);
res.sendRedirect("/servlet/MyServlet" );

In the redirected servlet's doGet() or doPost() method:

// get the data out of the session
SomeComplexData data;
HttpSession session = req.getSession(false);
if (session != null) {
     data = (SomeComplexData) session.getValue("my complex data");

Last updated 12/26/1999 07:37:40 PM