JSP WEBServer的實現原理

來源:互聯網
上載者:User
js|server|web 因為要實現一個WebServer,寫完了一個簡單的WebServer後突發奇想,何不實現一個JSP 的WebServer呢?
有了這個想法後,就開始考慮JSP的實現原理,其實JSP的實現也很簡單,說白了就是將其轉換成一個Java檔案,再對這個Java檔案進行編譯,產生類檔案,接著再由伺服器使用這個類檔案。從總體上分,可以將JSP檔案的內容劃分為兩大塊,即輸出語句和控制語句。
至於JSP檔案中的HTML語句就是我們要輸出的內容,而JSP標籤則是控制HTML語句的輸出。例如,有一個JSP檔案的內容如下:
<html>
<body>
<%! java.util.Date date=new java.util.Date();
int size=10;
%>
<% for(int i=0;i<size;i++){ %>
<h1>Hello<%= i %>
</h1>
<% } %>
<%=date%>
</body>
</html>
這個JSP檔案將產生如下效果:

Hello 0
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8
Hello 9
Thu Dec 16 18:30:21 CST 2004

那麼,現在的問題就是我們就產生什麼樣的java檔案,才能獲得正確的結果。
首選寫一個Interface,該介面就是我們要產生的JAVA檔案將要繼承的介面。其定義如下:
package httpserver;
import java.net.Socket;
import java.io.IOException;

public interface Jsp2HtmlInterface{
void printHtml(Socket socket)throws IOException;
}

接下來就寫一個類JavaServerPage負責解析JSP檔案,在這個類中將會動態產生JAVA檔案,並編譯產生一個可用類。再調用這個類檔案的printHtml(socket) 方法,將響應資訊寫給用戶端(其中socket為伺服器accept返回的套介面。這樣客戶就能看到JSP正確啟動並執行結果。這個解析JavaServerPage的類架構如下:
public class JavaServerPage{
public JavaServerPage(File file,Socket socket)throws Exception{} //file為用戶端請求的一個JSP檔案
//socket為伺服器同用戶端聯結的套介面
public void writeReponse() { //這個函數將負責根據指定的JSP檔案根據某種
GeneratJava(); //動態產生java檔案並將編譯,
Process proc=Runtime.getRuntime().exec("javac ...");
try{
proc.waitFor();
}catch(InterruptedException ie){ }
httpserver.Jsp2HtmlInterface object=(httpserver.Jsp2HtmlInterface)Class.forName("...").newInstance();
object.printHtml(socket);
}
}

假設我們用某種演算法將上面的JSP檔案產生如下的JAVA檔案
package httpserver;
import java.io.*;
import java.awt.*;
import java.net.Socket;
public class test implements Jsp2HtmlInterface{
private PrintStream out;
public test(){}
public void println(Object object)throws IOException{
out.print(object.toString()+"\r\n");}
public void println(int i)throws IOException{
out.print(i+"\r\n");}
public void println(float i)throws IOException{
out.print(i+"\r\n");}
public void println(double i)throws IOException{
out.print(i+"\r\n");}
public void println(long i)throws IOException{
out.print(i+"\r\n");}
public void println(char ch)throws IOException{
out.print(ch+"\r\n");}
public void println(char[] ch)throws IOException{
out.print(ch+"\r\n");}
public void println()throws IOException{
out.print("\r\n");
}
public void close(){
out.close();
}
public void printHtml(Socket socket)throws IOException{
out=new PrintStream(socket.getOutputStream());
this.println("<html>");
this.println("<title>Hello</title>");
this.println("<body>");
java.util.Date date=new java.util.Date();
int size=10;



for(int i=0;i<size;i++){

this.println(" <h1>Hello");
this.println( i );
this.println();
this.println(" </h1>");
}

this.println( date );
this.println();
this.println("</body>");
this.println("</html>");
this.println();
this.close();
}
}


通過調用該類printHtml()方法即可實現JSP檔案的解析。
本方法在JBuilderX環境下編譯通過。
說到這裡,大家是不是想動手實現一個自已的tomcat或者實現別的什麼動態指令碼語言了。





相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.