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
skylar1Asked:
Who is Participating?
 
Ravindra76Connect With a Mentor Commented:
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.

res.sendRedirect("http://yourserver:yourport/servlet/YourServletName?param1=value1¶m2=value2");

2.Another way of interservlet communication is

getServletContext().getName("yourservletanme");

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.
0
 
heyhey_Commented:
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="..." />
0
All Courses

From novice to tech pro — start learning today.