Using ServerXMLHTTP in a Multitiered Environment
The following example shows how ServerXMLHTTP
can be used to access a news service on a backend server and deliver it to a Web client.
Request on the Presentation Tier
A simple Web page, latestnews.asp, contains a link to the ServerXMLHTTP.asp page on the business tier server.
<html> <head> <title>Latest News</title> </head> <body> <a href="http://myserver/myproject/ServerXMLHttp.asp">Latest News</a> </body> </html>
Request on the Business Tier
The ServerXMLHTTP.asp page resides on the business or middle tier. This page contains Microsoft® JScript® that opens a connection to a NewsService.asp page on a server at the data services tier.
<%@language=JScript%> <% var srvXMLHTTP srvXMLHTTP = Server.CreateObject("Msxml2.ServerXMLHTTP.5.0"); srvXMLHTTP.open ("GET", "http://dataserver/news/NewsService.asp", false); srvXmlHttp.send(); newsElement = srvXMLHTTP.responseXML.selectSingleNode("/news/story1"); %> <html> <body> <p>Top News Story<p> <%Response.Write(newsElement.text);%> </body> </html>
Response on the Data Services Tier
The NewsService.asp page resides on the data services or middle tier. This page contains JScript that loads an XML document, news.xml, into a DOMDocument
object named Result
, and then saves the object directly to the Response object, which is returned to ServerXMLHTTP.asp on the business tier.
<%@language=JScript%> <% var Result = Server.CreateObject("Msxml2.DOMDocument.5.0"); Result.async = false; Result.load(Server.MapPath("news.xml")); Response.ContentType = "text/xml"; Result.save(Response); %>
Note A document comprised of the latest news stories collected by a news service can be quite large. For this example, the news.xml document loaded by NewsService.asp is small and simple.
<?xml version ="1.0"?> <news> <story1>Here is the top news story…</story1> <story2>News is the next news story.</story2> </news>
Response on the Business Tier
ServerXMLHTTP.asp handles the response on the business tier. In this case, ServerXMLHTTP.asp loads the response into the xmlResult
(DOMDocument
) object. The DOM selects a single node from the document using the selectSingleNode
method of DOMDocument
, setting the results to the newsElement
object. The program then writes the text property of the newsElement
object (along with HTML) to the Web page on the requesting client computer on the presentation tier.
<%@language=JScript%> <% var srvXMLHTTP; srvXMLHTTP = Server.CreateObject("Msxml2.ServerXMLHTTP.5.0"); srvXMLHTTP.open ("GET", "http://dataserver/news/NewsService.asp", false); srvXMLHTTP.send(); newsElement = srvXMLHTTP.responseXML.selectSingleNode("/news/story1"); %> <html> <body> <p>Top News Story<p> <%Response.Write(newsElement.text);%> </body> </html>
Response on the Presentation Tier
The user sees the following output in the Web browser.
Top News Story
Here is the top news story…