Because of a webserver, I have written a simple webserver, why not implement a JSP WebServer?
After having this idea, you will start thinking about the principle of JSP. In fact, the implementation of JSP is also very simple. It is said that it is converted into a java file, then compiles this Java file, generates class files, then use the server. This class file. From the overall point, the contents of the JSP file can be divided into two blocks, ie output statements and control statements.
As for the HTML statement in the JSP file is the content we have to output, and the JSP tag is the output of the control HTML statement. For example, there is a JSP file as follows:
<%! java.util.date Date = new java.util.date ();
INT size = 10;
%>
<% For (int i = 0; i
h1>
<%}%>
<% = DATE%>
body>
html>
This JSP file will produce the following effects:
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 So, the current problem is what we can generate what kind of Java file can get the correct result. The first choice to write an interface, the interface is the interface we have to generate will inherit the interface. Defined as follows: package httpserver; import java.net.Socket; import java.io.IOException; public interface Jsp2HtmlInterface {void printHtml (Socket socket) throws IOException;} Next, write a class is responsible for parsing JavaServerPage JSP file in this class The Java file will be dynamically generated and generate an available class. The printhtml (socket) method of this class file is called, and the response information is written to the client (where socket is the socket returned by the server Accept. This customer can see the result of the JSP's correct operation.
The class framework for this parsing JavaServerPage is as follows: Public Class JavaServerPage {Public Class JavaServerPage {Public Class JavaSerPage (File File, Socket Socket) THROWS Exception {} // File A JSP file for the client requests the server with the client connection PUBLIC VOID WriteReponse () {// This function will be responsible for generating Java files based on certain geneatjava (), and will compile according to some generatjava (); //, process prot = runtime.getime (). EXEC ("Javac ...") ; try {proc.waitFor ();} catch (InterruptedException ie) {} httpserver.Jsp2HtmlInterface object = (httpserver.Jsp2HtmlInterface) Class.forName ( "...") newInstance ();. object.printHtml (socket);} } Suppose we use an algorithm to generate the following JSP file above the document JAVA package httpserver; import java.io *;. import java.awt *;. import java.net.Socket; public class test implements Jsp2HtmlInterface {private PrintStream out PUB Lic Test () {} (Object Object) THROWS IOEXCEPTION {Out.Print (Object.tostring () "} public void println (int i) throws oews oException {Out.print (i "/ r / n");} public void println (float i) throws ioException {Out.print (i "/ r / n");} public void println (double i) throws oews oews ooException {Out.print (i "/ R / N ");} public void println (long i) throws ioException {Out.print (i " / r / n ");} public void println (char ch) throws oews oewception {Out.print (CH " / R / N ");} public void println (char [] ch) throws iexception {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 oews oException {out = new printstream (socket.getOutputStream ()); this.println (""); this.println ("
THIS.PRINTLN ("
THIS.PRINTLN (Date); this.println (); this.println (" body>"); this.println (" html>"); this.clintln (); this.close (); } The analysis of JSP files can be implemented by calling this clarinthtml () method. This method compiles through the JBUILDERX environment. Speaking of this, everyone is not to do a self-Tomcat or do something else. <%! java.util.date Date = new java.util.date (); int size = 10;%> <% for (int i = 0; i
<%}%> <% = DATE%>