redirect a servlet

Discussion in 'Java' started by autogoor@yahoo.com, Apr 23, 2005.

  1. Guest

    We have a couple of online resources (cgi, servlet...) on different
    machine and even different domains. For example, user can call

    http://www.domain1.com/app1&name="smith"
    or
    http://www.domain2.com/cgi-bin/app2&zip="94000"

    and get response.

    We would like to provide a central location which can take user's
    request and forward them to appropriate destinations. Thus, if user
    call:

    http://www.centeral.com/mainapp?app=app1&name="smith"

    The request will be forwarded to the first service.

    I am writing a servlet which basically check user's authorization and
    they forward the request with user's original parameters to other
    resources.

    Here is my code for centeral/mainapp:

    public class MainAppServlet extends HttpServlet{
    private static final String s1="http://www.domain1.com/app1";
    private static final String
    s2="http://www.domain2.com/cgi-bin/app2";
    private static final String s3="http://www.domain3.com/app2";

    public void doGet(HttpServletRequest request, HttpServletResponse
    response)
    throws ServletException, IOException {

    //check if user loged in
    ...

    //forward
    String app = request.getParameter("app");
    String des;
    if ("app1".equals(app)){
    des = s1;
    }
    if ("app2".equals(app)){
    des = s2;
    }

    if ("app3".equals(app)){
    des = s3;
    }

    response.senRequestDispatcher dispatcher =
    request.getRequestDispatcher(des);
    dispatcher.forward(request, response);
    }
    }
    We found the code did not work. I think it is because "forward" can
    only handle request to the same server. How can I fix it? I am thinking
    about sendRedirect, but how can I forward parameters with it?

    Thanks,

    Autogoor
     
    , Apr 23, 2005
    #1
    1. Advertising

  2. Wendy S Guest

    <> wrote

    > We found the code did not work. I think it is because "forward" can
    > only handle request to the same server. How can I fix it? I am thinking
    > about sendRedirect, but how can I forward parameters with it?


    Yes, forward only works within the container. You send parameters with a
    redirect the same way you got them-- in the URL.

    http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServletRequest.html
    See if request.getQueryString(...) is helpful.

    Otherwise, request.getParameter(...) will work if you know the parameter
    name you're looking for, or getParameterNames if you need all of them.

    --
    Wendy
     
    Wendy S, Apr 24, 2005
    #2
    1. Advertising

  3. Ross Bamford Guest

    On Sat, 2005-04-23 at 17:18 -0700, Wendy S wrote:
    > <> wrote
    >
    > > We found the code did not work. I think it is because "forward" can
    > > only handle request to the same server. How can I fix it? I am thinking
    > > about sendRedirect, but how can I forward parameters with it?

    >
    > Yes, forward only works within the container. You send parameters with a
    > redirect the same way you got them-- in the URL.
    >
    > http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServletRequest.html
    > See if request.getQueryString(...) is helpful.
    >
    > Otherwise, request.getParameter(...) will work if you know the parameter
    > name you're looking for, or getParameterNames if you need all of them.


    An easy way to do this would be to write a REFRESH header to the
    response:

    response.addHeader("REFRESH","5;url=http://www.whatever.com/");

    I think that should work, if you just want to punt the client someplace
    else :)

    +-= Ross =-+
    --
    [Ross A. Bamford] [9.co.uk]
    Roscopeco Open Tech ++ Open Source + Java + Apache + CMF
    http://www.roscopec0.f9.co.uk/ + 9.co.uk
     
    Ross Bamford, Apr 24, 2005
    #3
  4. Minh Tran-Le Guest

    Ross Bamford wrote:
    > On Sat, 2005-04-23 at 17:18 -0700, Wendy S wrote:
    >
    >><> wrote
    >>
    >>
    >>>We found the code did not work. I think it is because "forward" can
    >>>only handle request to the same server. How can I fix it? I am thinking
    >>>about sendRedirect, but how can I forward parameters with it?

    >>
    >>Yes, forward only works within the container. You send parameters with a
    >>redirect the same way you got them-- in the URL.
    >>
    >>http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServletRequest.html
    >>See if request.getQueryString(...) is helpful.
    >>
    >>Otherwise, request.getParameter(...) will work if you know the parameter
    >>name you're looking for, or getParameterNames if you need all of them.

    >
    >
    > An easy way to do this would be to write a REFRESH header to the
    > response:
    >
    > response.addHeader("REFRESH","5;url=http://www.whatever.com/");
    >
    > I think that should work, if you just want to punt the client someplace
    > else :)
    >
    > +-= Ross =-+


    You could also do:
    response.sendRedirect("http://www.whatever.com/xxx?other=args");

    One thing that I don't know for sure is if you could redirect an HTTP POST.

    Minh Tran-Le.
     
    Minh Tran-Le, Apr 24, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Fish
    Replies:
    4
    Views:
    2,441
    Andy Fish
    Dec 17, 2003
  2. Sean Clarke
    Replies:
    1
    Views:
    1,934
    Sudsy
    Jan 7, 2004
  3. circuit_breaker
    Replies:
    2
    Views:
    2,061
    Jack Jia
    Apr 4, 2004
  4. chamikara
    Replies:
    1
    Views:
    49,944
  5. Sal
    Replies:
    1
    Views:
    904
Loading...

Share This Page