Implementation principle of JSP webserver

Source: Internet
Author: User
Tags date implement int size object object socket client
Js|server|web because to achieve a webserver, wrote a simple webserver after the whim, why not realize a JSP webserver?
With this idea, I began to consider the implementation of JSP principle, in fact, the implementation of JSP is also very simple, the white is to convert it into a Java file, and then compile the Java file, generate class files, and then by the server to use this class files. From the overall point of view, the contents of the JSP file can be divided into two large chunks, that is, output statements and control statements.
The HTML statement in the JSP file is what we want to output, and the JSP tag is the output that controls the HTML statement. For example, there is a JSP file with the following contents:
<body>
<%! Java.util.Date date=new java.util.Date ();
int size=10;
%>
<% for (int i=0;i<size;i++) {%>
<%}%>
<%=date%>
</body>
This JSP file will have the following effect:

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

So the question now is what Java files we generate to get the right results.
Preferred to write a interface, which is the interface that the Java file we want to generate will inherit. The definition is as follows:
Package httpserver;
Import Java.net.Socket;
Import java.io.IOException;

public interface jsp2htmlinterface{
void printhtml (Socket socket) throws IOException;
}

Then write a class javaserverpage is responsible for parsing the JSP file, in which the Java file will be dynamically generated and compiled to generate a usable class. The printhtml (socket) method of this class file is called, and the response information is addressed to the client (where the socket is the socket that the server accept returns.) This allows the customer to see the results of the JSP running correctly. This parsing Javaserverpage class framework is as follows:
public class javaserverpage{
Public javaserverpage (file File,socket Socket) throws exception{}//file a JSP file requested for the client
Socket interface for server to connect to client
public void Writereponse () {///This function will be responsible for the specified JSP file according to some kind of
Generatjava (); Dynamically generating Java files and compiling them,
Process proc=runtime.getruntime (). EXEC ("Javac ...");
try{
Proc.waitfor ();
}catch (Interruptedexception IE) {}
Httpserver. Jsp2htmlinterface object= (httpserver. Jsp2htmlinterface) Class.forName ("..."). newinstance ();
object.printhtml (socket);
}
}

Let's say we use some sort of algorithm to generate the following Java file from the above JSP file
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 ("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 ("This.println (i);
This.println ();
This.println ("}

THIS.PRINTLN (date);
This.println ();
This.println ("</body>");
This.println ("This.println ();
This.close ();
}
}


You can implement the parsing of JSP files by calling the class Printhtml () method.
This method compiles and passes in the JBUILDERX environment.
Speaking of which, do you want to implement a self-tomcat or some other dynamic scripting language?





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.