因为要实现一个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或者实现别的什么动态脚本语言了。
<%! java.util.Date date=new java.util.Date();
int size=10;
%><%
for(int i=0;i
<% } %><%= date %>
|