Reputation: 4028
On a project I am doing I have been forced into developing a server side part due to restrictions of the "Same Origin Policy" in browsers, that prevents the AJAX making requests to anything outside of the domain of the page.
Therefore I am building a Java Servlet that will act as the handler for the page and will retrieve XML's from external sites and return it back to the client page using AJAX
I will use GET parameters to instruct the servlet what URL to fetch XMLs from.
I am a beginner with Java, I could easily do this in PHP but sadly there are no environments availale to host PHP or Apache
Skeleton Code
public void doGet(HttpServletRequest agentRequest, HttpServletResponse agentResponse)
throws ServletException, IOException
{
agentResponse.setContentType("text/xml");
//determine if agentRequest is for templateList or templateDetails
//build URL for specific request
//if no parameters sent or invalid send error response
//fetch XML from URL
//output response XML to client
}
I do not want a full code solution, just references and ideas to get me in the right direction!
e.g. what java features to use for this, etc.
Thanks
Upvotes: 0
Views: 1235
Reputation: 9559
As requested, some high-level ideas to get you started: Firstly, get the external URL from the request parameter:
//equivalent of PHP $_GET["url"]
String externalUrl = agentRequest.getParameter("url");
Then you need to retrieve the response from the external URL. There are already various Q'a and A's on that topic, including How do I do a HTTP GET in Java?
Finally, you need to write the response into your response using the OutputStream supplied:
agentResponse.getOutputStream();
Upvotes: 1