views:

35

answers:

2

I have a form/calculator, which posts to itself some data, this data is then calculated by dispatching a servlet and the results are output as xml. The dispatcher code is shown below:

//create instance
ServletContext sc = this.getServletContext();
//create dispatcher
RequestDispatcher rd = sc.getRequestDispatcher("/ProCalcServlet");

rd.include(request, response);

Have a few problems with what I'm doing at the moment though. Firstly, is it possible to use a remote URL as opposed to just locally? And how do I process the data, since I'm assuming that because it's a servlet, I can't just call it an XML document and use the DOM to grab the data I want.

Quite new to this Java stuff, don't even know what to google for exactly, so I'm kind of shooting in the dark with my current methods. Any help or directions would be greatly appreciated :P cheers

+1  A: 

You cannot use a RequestDispatcher to forward a request to a different URL. This only allows you to forward requests to the same web application on the same container. You can, however, use response.sendRedirect() to send the browser a redirect to another site/URL. Doing this, however, you will not be able to pass any objects -- you'll have to rely on argument parameters.

I'm not sure I understand what you're doing with XML. Your first statement seems to imply that you want to output the response as XML, that's certainly easy enough, just use:

response.setContentType("text/xml;charset=utf-8");
desau
+1  A: 

I hope I understand your questions correctly.

It is possible to use remote URL. In that case, you need to invoke the URL through web service style. You can use HttpClient to call the URL. The URL will then return you data in XML form (in one big string).

For you to process the XML, there are many libraries that allow you to easily to do so. You can stick JDK's DOM or SAX parser, but in my opinion that's messy. Consider using Castor, JDom, or Dom4J... some of them allows you to query the data using XPath too.

limc