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?


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi skylar1,

You Can.

1.But parameters to be passed in querystring.

No posted parameters.

You compulsory has to extend HttpServlet

Assume that your HttpServletResponse is res.


2.Another way of interservlet communication is


will return a servlet.

Call the necessary methods.

Note: Before redirecting, you won't write any data in the current servlet
by getting stream or writer from curretn servlet res object.

Best of lcuk.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
you need something like this

 (req, res);

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.
    request.setAttribute("theBean", myBean);
    RequestDispatcher rd =
    rd.forward(request, response);
    <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.
    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: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.
    getServletContext().setAttribute("theBean", myBean);
    <jsp:useBean id="theBean" scope="application" class="..." />
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.