![]() |
|
首页 │ Apache │ Linux│ Java│ MySQL│ 注册│帮助 | |||
今天想说明的不是PHP行不行的问题,标题只是想吸引朋友来一起见证一下,自从我学PHP那天开始就听了不少关于PHP负面评论的说法,但我只相信事实而不是因为别人言论而抹杀了PHP本身的价值,以上是本人的看法,下面我举一个小例子,等各位看看究竟谁是谁非。
题目:读取远程的XML文件并显示出来,就实现这一个功能而己,以下我用四种语言来实现
XML文件:(我在网上放了一个用于测试用的路径:http://insurance.dnsing.com/test.xml)
第一种:Java 另存为 ProxyServlet.java 先编译再运行
package org.jmatter.proxy;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ProxyServlet
extends HttpServlet
{
private String contentUrl;
public static final String CONTENT_KEY = "contentUrl";
public void init(ServletConfig servletConfig)
throws ServletException
{
contentUrl = servletConfig.getInitParameter(CONTENT_KEY);
if (contentUrl == null)
{
throw new ServletException("The init parameter [" + CONTENT_KEY +
"] must be specified as an init " +
"parameter.");
}
}
public void service (HttpServletRequest httpRequest,
HttpServletResponse httpResponse)
throws ServletException
{
URL content = null;
try
{
content = new URL(contentUrl);
}
catch (MalformedURLException exception)
{
throw new ServletException(contentUrl + " is a malformed url.");
}
URLConnection contentCon = null;
try
{
contentCon = content.openConnection();
}
catch (IOException exception)
{
throw new ServletException("Problem opening " + contentUrl +
": " + exception.toString());
}
//
// Get the content type from the URLConnection and set it on the
// response.
//
String contentType = contentCon.getContentType();
httpResponse.setContentType(contentType);
//
// Get and read the input stream.
//
try
{
InputStream in = contentCon.getInputStream();
byte[] buffer = new byte[1024];
ByteArrayOutputStream contentStream = new ByteArrayOutputStream();
for (int cnt = in.read(buffer);
cnt != -1;
cnt = in.read(buffer))
{
contentStream.write(buffer, 0, cnt);
if (in.available() == 0)
{
break;
}
}
//
// This is where you have the content from the request.
//
String contentString = contentStream.toString();
System.err.println(contentString);
//
// Now write the bytes out to the client.
//
byte[] contentBytes = contentString.getBytes();
OutputStream out = httpResponse.getOutputStream();
out.write(contentBytes, 0, contentBytes.length);
out.flush();
out.close();
}
catch (Exception exception)
{
throw new ServletException("Unable to proxy request: " + exception);
}
}
}
上面是用Java来读取,代码庞大得不得了,运行效率暂可不提,就敲起代码来也觉得费劲!
第二种:ASP 另存为 test.asp 运行
<%@ LANGUAGE=VBScript%>
<%
Response.Buffer=True
Dim MyConnection, TheURL
' Specifying the URL
TheURL = "http://insurance.dnsing.com/test.xml"
Set MyConnection = Server.CreateObject("Microsoft.XMLHTTP")
' Connecting to the URL
MyConnection.Open "GET", TheURL, False
' Sending and getting data
MyConnection.Send
TheData = MyConnection.responseText
'Set the appropriate content type
Response.ContentType = MyConnection.getResponseHeader("Content-Type")
Response.Write (TheData)
Set MyConnection = Nothing
%>
ASP我用了XMLHTTP组件来实取,虽然代码不算太长,但每读一次就要建立一次组件,效率稍逊一筹。
第三种:ColdFusion,另存为 test.cfm 来运行(首先必须要用ColdFusion的运行环境)
>
#cfhttp.fileContent#
ColdFusion,是大名鼎鼎的MicroMedia公司为动态网页而开发的语言,本人刚入门觉得此语言还不错。
最后出场:PHP 另存为 test.php 来运行
[php]
<?php
readfile('http://localhost/remotexml/test.xml');
?>
[/php]
程序写出来后连自己也差点不相信自己有那么的牛B,哈哈,PHP用了一行代码就搞掂,其实就是PHP牛B,因为PHP把很多功能都集成进去了,从某种角度上方便了开发者,同时也大大缩短了开发者的开发周期,运行的效率就不必提,大家心照就OK,本人只是做了一个抛砖引玉的作用,剩下的就交给你们了。。。
本文转载:DNSing Network
[ 本帖最后由 dnsing 于 2007-2-12 10:45 AM 编辑 ]

