Rumah > pembangunan bahagian belakang > tutorial php > 如何用 Java SE 写一个简易的 HTTP 服务器?如果要支持 JSP ,ASP 或者 PHP 又需要如何改进?如果不能,是否有其他语言可以实现?

如何用 Java SE 写一个简易的 HTTP 服务器?如果要支持 JSP ,ASP 或者 PHP 又需要如何改进?如果不能,是否有其他语言可以实现?

WBOY
Lepaskan: 2016-06-17 08:32:33
asal
1072 orang telah melayarinya

我是这样思考的,要自己写几个解析JSP,ASP,PHP的编译器不现实,所以我做的这个服务器当监听到动态网页请求时,把请求转发给IIS(如果请求是ASP的话),同理,如果是JSP就转发给TOMCAT,PHP就转发给Apache,然后再接收以上服务器解析之后返回的静态内容返回给客户端浏览器。那么这个转发功能用java要怎么实现呢?如果实现不了,其他语言可以否?

回复内容:

亲,你需要一个反向代理。 Java SE什么的不知道,不过如果交给Python/Nodejs/C++的话实现原理都是这么几句话:

(以下伪代码)

<code class="language-coffee-script"><span class="nv">server = </span><span class="nx">httpService</span><span class="p">.</span><span class="nx">createServer</span><span class="p">()</span>
<span class="nx">server</span><span class="p">.</span><span class="nx">on</span> <span class="s">'http request'</span><span class="p">,</span> <span class="nf">(req, res)-></span>
  <span class="nv">req_ext = </span><span class="nx">req</span><span class="p">.</span><span class="nx">require_url</span><span class="p">.</span><span class="nx">split</span> <span class="sr">/\.[asp|php|jsp|do]/i</span>
  <span class="k">switch</span> <span class="nx">req_ext</span>
    <span class="nx">case</span> <span class="s">'php'</span><span class="o">:</span>
      <span class="nx">res</span><span class="p">.</span><span class="nx">send</span> <span class="nx">os</span><span class="p">.</span><span class="nx">execute</span> <span class="s">'php req.require_url'</span>
    <span class="nx">case</span> <span class="s">'asp'</span><span class="o">:</span>
      <span class="nx">res</span><span class="p">.</span><span class="nx">pip</span> <span class="nx">http</span><span class="p">.</span><span class="nx">require</span> <span class="s">'IIS server...'</span>
    <span class="nx">case</span> <span class="s">'do'</span><span class="p">,</span> <span class="s">'jsp'</span><span class="o">:</span>
      <span class="nx">res</span><span class="p">.</span><span class="nx">send</span> <span class="nx">http</span><span class="p">.</span><span class="nx">require</span> <span class="s">'Tomcat server...'</span>
    <span class="nv">default:</span>
      <span class="nx">res</span><span class="p">.</span><span class="nx">send</span> <span class="nx">file</span><span class="p">.</span><span class="nx">read</span> <span class="s">''</span> <span class="k">if</span> <span class="nx">file</span><span class="p">.</span><span class="nx">exist</span> 
</code>
Salin selepas log masuk
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan