Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing resources out of context  RSS feed

 
Tamer Badr
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to access resources out of the servlet context, I know that RequestDispacher can only access resources in the same context.
This can easily be done using PHP or ASP, I just need to do it in servlets, I need to open a web page providing the page URL and get the contents of this page from inside the servlet (Server side include).
 
Tamer Badr
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to access a web page that is located at another server in the web, I just know the page URL.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the URL class to read content from another server. It could be as simple as:

 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your using JSP, this can be done with JSTL's c:import tag.
 
Tamer Badr
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I did the URL method.
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add you can get a reference to the Servlet-
Context of the other web application using getServletContext().
getContext(uripath). Using this servlet context reference,
you can retrieve an appropriate RequestDispatcher object as usual to the resource you want to use.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Satya Maheshwari:
Using this servlet context reference, you can retrieve an appropriate RequestDispatcher object as usual to the resource you want to use.

The OP said "located at another server in the web", in which case the RequestDispatcher approach doesn't work.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your case your servlet has to act like a client to the other URL.

Use URLConnection or Apache HTTPClient for establishing connection with the other URL and fetch the response.

RequestDispatcher would certainly not work.
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for correcting. I did'nt realize that here we are talking about a different server itself . One related question I had was, could we use response.sendRedirect to redirect to the URL for this.
 
praveen sharma
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i think try this it will help you

Praveen
 
ruchi thakur
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Thread hi-jack removed.]
[ July 16, 2007: Message edited by: Bear Bibeault ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!