1

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

4

2 回答 2

1

您不能使用 RequestDispatcher 将请求转发到不同的 URL。这仅允许您将请求转发到同一容器上的同一 Web 应用程序。但是,您可以使用response.sendRedirect()将浏览器重定向到另一个站点/URL。但是,这样做,您将无法传递任何对象——您将不得不依赖参数参数。

我不确定我是否理解您使用 XML 所做的事情。您的第一条语句似乎暗示您要将响应输出为 XML,这当然很容易,只需使用:

response.setContentType("text/xml;charset=utf-8");
于 2010-07-01T16:36:19.077 回答
1

我希望我能正确理解你的问题。

可以使用远程 URL。在这种情况下,您需要通过 Web 服务样式调用 URL。您可以使用HttpClient来调用 URL。然后,该 URL 将以 XML 格式(在一个大字符串中)向您返回数据。

为了让您处理 XML,有许多库可以让您轻松地进行处理。您可以坚持使用 JDK 的 DOM 或 SAX 解析器,但在我看来这很混乱。考虑使用CastorJDomDom4J ......其中一些还允许您使用XPath查询数据。

于 2010-07-01T16:37:20.967 回答