Implementation principle of JSPWEBServer

Source: Internet
Author: User

To implement a WebServer, after writing a simple WebServer, why not implement a jsp (first choice for SUN Enterprise Applications) WebServer?
With this idea, we began to consider the implementation principle of jsp (the preferred choice for SUN Enterprise Applications). In fact, the implementation of jsp (the preferred choice for SUN Enterprise Applications) is also very simple, to put it bluntly, convert it into a Java file, compile the Java file, generate a class file, and then use the class file on the server. In general, the content of jsp (the preferred choice for SUN Enterprise Applications) files can be divided into two main parts: Output statements and control statements.
As for the HTML statement in the jsp (preferred for SUN Enterprise Applications) file, we want to output the content, while the jsp (preferred for SUN Enterprise Applications) Tag controls the output of HTML statements. For example, the content of a jsp (preferred for SUN Enterprise Applications) file is as follows:
<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>
This jsp (preferred for SUN Enterprise Applications) file will produce the following results:
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

Now, the question is what java files are generated to get the correct results.
First, write an Interface, which is the Interface to be inherited from the JAVA file to be generated. It is defined as follows:
Package httpserver;
Import java.net. Socket;
Import java. io. IOException;

Public interface jsp (preferred for SUN Enterprise Applications) 2 HtmlInterface {
Void printHtml (Socket socket) throws IOException;
}

Next, write a class assumerverpage to parse jsp (preferred for SUN Enterprise Applications) files. In this class, JAVA files will be dynamically generated and an available class will be compiled. Call the printHtml (socket) method of this class file and write the response information to the client (where socket is the set of interfaces returned by the Server accept. In this way, the customer can see the correct running result of jsp (the preferred choice for SUN enterprise-level applications. The class framework for parsing assumerverpage is as follows:
Public class assumerverpage {
Public JavaServerPage (File file, Socket socket) throws Exception {} // file is a jsp (preferred for SUN Enterprise Applications) File requested by the client
// Socket is the interface connecting the server to the client
Public void writeReponse () {// this function will be responsible for depending on the specified jsp (preferred for SUN Enterprise Applications) File
GeneratJava (); // dynamically generate a java file and compile it,

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.