Link to home
Start Free TrialLog in
Avatar of skylar1
skylar1

asked on

sendRedirect() to a servlet?

Is it possible for a servlet to use the sendRedirect() method to redirect a request to another servlet, preserving the parameter information?  I have been able to use this method to redirect a request to a static html page, but when I try to redirect to a servlet, I get error messages.

If this is not possible, what is the easiest way to have a servlet send form information to another servlet?

Thanks,

John
ASKER CERTIFIED SOLUTION
Avatar of Ravindra76
Ravindra76

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of heyhey_
heyhey_

you need something like this

getServletContext().getRequestDispatcher("/servlet/NextServlet").forward
 (req, res);

http://www.esperanto.org.nz/jsp/jspfaq.html

....
11) How do you pass data (including beans) to a JSP from a servlet?
TOC There are actually three different ways to do it, depending on how long the reference should last, and which JSP pages (and servlets, for that matter) should be able to see it. In each of the cases, assume that "myBean" is a reference to the bean you want to send, and that "theBean" is the key I'm going to use to store the bean under (from the servlet perspective), and use as the identity of the bean in the JSP page.
These techniques are portable to any environment compliant with the servlet API 2.1 and JSP 1.0 specifications. In each case, the passing
works from servlet->JSP, servlet->servlet, JSP->JSP, or JSP->servlet transitions.
(1) Request Lifetime
Use this technique to pass beans that are relevant to this particular request to a bean you are calling through a request dispatcher (using either "include" or "forward"). This bean will disappear after processing this request has been completed.
SERVLET:
    request.setAttribute("theBean", myBean);
    RequestDispatcher rd =
        getServletContext().getRequestDispatcher('/thepage.jsp");
    rd.forward(request, response);
JSP PAGE:
    <jsp:useBean id="theBean" scope="request" class="....." />
(2) Session Lifetime
Use this technique to pass beans that are relevant to a particular session (such as in individual user login) over a number of requests. This bean will disappear when the session is invalidated or it times out, or when you remove it.
SERVLET:
    HttpSession session = request.getSession(true);
    session.putValue("theBean", myBean);
    /* You can do a request dispatcher here,
        or just let the bean be visible on the
        next request */
JSP PAGE:
    <jsp:useBean id="theBean" scope="session" class="..." />
(3) Application Lifetime
Use this technique to pass beans that are relevant to all servlets and JSP pages in a particular app, for all users. For example, I use this to make a JDBC connection pool object available to the various servlets and JSP pages in my apps. This bean will disappear when the servlet engine is shut down, or when you remove it.
SERVLET:
    getServletContext().setAttribute("theBean", myBean);
JSP PAGE:
    <jsp:useBean id="theBean" scope="application" class="..." />