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

WBOY
發布: 2016-06-17 08:32:33
原創
1041 人瀏覽過

我是这样思考的,要自己写几个解析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>
登入後複製
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板