open an external page from a servlet

Discussion in 'Java' started by Sigi, Jan 18, 2007.

  1. Sigi

    Sigi Guest

    Hi,
    I have a servlet which is called by a POST from a form in a html page.
    Inside this servlet, through the "request.getRequestDispatcher", I open a
    page foo.html in the same path of the servlet.

    protected void doPost(HttpServletRequest request, HttpServletResponse
    response) throws ServletException, IOException {

    // some code...

    request.getRequestDispatcher("foo.html").forward(request, response);
    }


    Instead, if I try to write an absolute URL (i.e., if I want to call a page
    on another site):

    request.getRequestDispatcher("http://www.abcde.com/foo.html").forward(request,
    response);

    it doesn't work:

    The requested resource (/myproject/http://www.abcde.com/foo.html) is not
    available.

    So it seems that it can accepts only relative URLs.
    How can I open an absolute URL?


    Thanks.
    Sigi, Jan 18, 2007
    #1
    1. Advertising

  2. Sigi

    Guest

    Sigi wrote:
    > Hi,
    > I have a servlet which is called by a POST from a form in a html page.
    > Inside this servlet, through the "request.getRequestDispatcher", I open a
    > page foo.html in the same path of the servlet.
    >
    > protected void doPost(HttpServletRequest request, HttpServletResponse
    > response) throws ServletException, IOException {
    >
    > // some code...
    >
    > request.getRequestDispatcher("foo.html").forward(request, response);
    > }
    >
    >
    > Instead, if I try to write an absolute URL (i.e., if I want to call a page
    > on another site):
    >
    > request.getRequestDispatcher("http://www.abcde.com/foo.html").forward(request,
    > response);
    >
    > it doesn't work:
    >
    > The requested resource (/myproject/http://www.abcde.com/foo.html) is not
    > available.
    >
    > So it seems that it can accepts only relative URLs.
    > How can I open an absolute URL?
    >
    >
    > Thanks.


    Try:
    response.sendRedirect("http://www.abcde.com/foo.html");
    instead of using the dispatcher
    , Jan 18, 2007
    #2
    1. Advertising

  3. Sigi

    Lew Guest

    wrote:
    > Try:
    > response.sendRedirect("http://www.abcde.com/foo.html");
    > instead of using the dispatcher


    The REDIRECT is serving its design purpose if you send the browser to another
    server. It makes no sense to forward() the request and response objects
    outside the current app server.

    You can still use the dispatcher if the other app is on the same server. It
    runs server-side, not client-side, so you can keep request attributes that
    would otherwise need to be session-level.

    In the Javadocs for ServletRequest.getRequestDispatcher()
    <http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletRequest.html#getRequestDispatcher(java.lang.String)>

    "The difference between this method and
    ServletContext.getRequestDispatcher(java.lang.String) is that this method can
    take a relative path."

    So we look up ServletContext.getRequestDispatcher(java.lang.String)
    <http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html#getRequestDispatcher(java.lang.String)>

    "The pathname must begin with a "/" and is interpreted as relative to the
    current context root. Use getContext to obtain a RequestDispatcher for
    resources in foreign contexts."

    Ok, look up getContext(), also a method of ServletContext
    <http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html#getContext(java.lang.String)>

    "Returns a ServletContext object that corresponds to a specified URL on the
    [same] server."

    So, assuming no null returns or Exceptions, you can use

    getServletContext().getContext( "/anotherAppOnSameServer" )
    .getRequestDispatcher( "/path/within/the/app" )
    .forward( request, response );

    - Lew
    Lew, Jan 19, 2007
    #3
  4. Lew wrote:
    > wrote:
    > > Try:
    > > response.sendRedirect("http://www.abcde.com/foo.html");
    > > instead of using the dispatcher

    >
    > The REDIRECT is serving its design purpose if you send the browser to another
    > server. It makes no sense to forward() the request and response objects
    > outside the current app server.


    It does if you want to attempt a DOS attack on a
    foreign server*. Just continually dump random data
    to it, until it crashes..

    * Which leads me to suspect that whatever 'spec.'
    controls this behaviour, rules out cross-domain 'post's,
    if not 'get's.

    Andrew T.
    Andrew Thompson, Jan 19, 2007
    #4
  5. Andrew Thompson wrote:
    > Lew wrote:
    >> wrote:
    >>> Try:
    >>> response.sendRedirect("http://www.abcde.com/foo.html");
    >>> instead of using the dispatcher

    >> The REDIRECT is serving its design purpose if you send the browser to another
    >> server. It makes no sense to forward() the request and response objects
    >> outside the current app server.

    >
    > It does if you want to attempt a DOS attack on a
    > foreign server*. Just continually dump random data
    > to it, until it crashes..


    Forward is a variant of a normal Java call.

    It is physical impossible to forward to
    something outside the JVM.

    Arne
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Jan 19, 2007
    #5
  6. Sigi

    Sigi Guest

    wrote:
    > Try:
    > response.sendRedirect("http://www.abcde.com/foo.html");
    > instead of using the dispatcher

    Ok, it works, thanks.

    I have another question: I also need to send some POST parameters to the
    destination URL.
    Is it possibile to send POST data to the URL passed in the sendRedirect
    method?

    Thanks.
    Sigi, Jan 19, 2007
    #6
  7. Sigi

    ck Guest

    Sigi wrote:
    > wrote:
    > > Try:
    > > response.sendRedirect("http://www.abcde.com/foo.html");
    > > instead of using the dispatcher

    > Ok, it works, thanks.
    >
    > I have another question: I also need to send some POST parameters to the
    > destination URL.
    > Is it possibile to send POST data to the URL passed in the sendRedirect
    > method?
    >
    > Thanks.


    Use Commons http client

    --
    Ck
    http://www.gfour.net
    ck, Jan 19, 2007
    #7
  8. Sigi wrote:
    > I have another question: I also need to send some POST parameters to the
    > destination URL.
    > Is it possibile to send POST data to the URL passed in the sendRedirect
    > method?


    No.

    Because the browser will do a GET.

    But you can try specifying GET parameters in the URL.

    Arne
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Jan 20, 2007
    #8
    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,407
    Andy Fish
    Dec 17, 2003
  2. Sean Clarke
    Replies:
    1
    Views:
    1,882
    Sudsy
    Jan 7, 2004
  3. circuit_breaker
    Replies:
    2
    Views:
    1,989
    Jack Jia
    Apr 4, 2004
  4. chamikara
    Replies:
    1
    Views:
    49,837
  5. news.sbcglobal.net
    Replies:
    2
    Views:
    1,459
    news.sbcglobal.net
    Oct 14, 2006
Loading...

Share This Page