open an external page from a servlet

S

Sigi

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.
 
S

steve.chernyak

Sigi said:
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
 
L

Lew

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/a...t.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/a...t.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
 
A

Andrew Thompson

Lew said:
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.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Andrew said:
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
 
C

ck

Sigi said:
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
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Sigi said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top