Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > open an external page from a servlet

Reply
Thread Tools

open an external page from a servlet

 
 
Sigi
Guest
Posts: n/a
 
      01-18-2007
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(r equest, 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.


 
Reply With Quote
 
 
 
 
steve.chernyak@gmail.com
Guest
Posts: n/a
 
      01-18-2007

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(r equest, 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

 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      01-19-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.Stri ng) is that this method can
take a relative path."

So we look up ServletContext.getRequestDispatcher(java.lang.Stri ng)
<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
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      01-19-2007
Lew wrote:
> (E-Mail Removed) 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.

 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      01-19-2007
Andrew Thompson wrote:
> Lew wrote:
>> (E-Mail Removed) 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
 
Reply With Quote
 
Sigi
Guest
Posts: n/a
 
      01-19-2007
(E-Mail Removed) 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.


 
Reply With Quote
 
ck
Guest
Posts: n/a
 
      01-19-2007

Sigi wrote:
> (E-Mail Removed) 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

 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      01-20-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Open external web page inside contentplaceholder using master page news.sbcglobal.net ASP .Net 2 10-14-2006 01:09 PM
Offending class: javax/servlet/Servlet.class chamikara Java 1 06-19-2004 05:42 AM
Servlet question(Tomcat, web.xml, servlet-class, servlet-name) circuit_breaker Java 2 04-04-2004 03:26 AM
Tomcat: POST from one servlet or internal class to another servlet Sean Clarke Java 1 01-07-2004 02:22 PM
how to get my servlet configuration before the servlet is initialised Andy Fish Java 4 12-17-2003 09:47 PM



Advertisments