仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2116|回复: 20
打印 上一主题 下一主题

[学习教程] JAVA网站制作之JSP WEBServer的完成道理

[复制链接]
变相怪杰 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-18 11:30:37 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
首先第一点:jsp,servlet,javabean这些最基本的,嘿嘿,就算你是高手的话,在大行的企业级应用的话还是需要框架的,一个好的框架确实能构解决许多问题。js|server|web由于要完成一个WebServer,写完了一个复杂的WebServer后突发奇想,何不完成一个JSP的WebServer呢?
有了这个设法后,就入手下手思索JSP的完成道理,实在JSP的完成也很复杂,说白了就是将其转换成一个Java文件,再对这个Java文件举行编译,天生类文件,接着再由服务器利用这个类文件。从整体上分,能够将JSP文件的内容分别为两年夜块,即输入语句和把持语句。
至于JSP文件中的HTML语句就是我们要输入的内容,而JSP标签则是把持HTML语句的输入。比方,有一个JSP文件的内容以下:
<html>
<body>
<%!java.util.Datedate=newjava.util.Date();
intsize=10;
%>
<%for(inti=0;i<size;i++){%>
<h1>Hello<%=i%>
</h1>
<%}%>
<%=date%>
</body>
</html>
这个JSP文件将发生以下效果:

Hello0
Hello1
Hello2
Hello3
Hello4
Hello5
Hello6
Hello7
Hello8
Hello9
ThuDec1618:30:21CST2004

那末,如今的成绩就是我们就天生甚么样的java文件,才干取得准确的了局。
首选写一个Interface,该接口就是我们要天生的JAVA文件将要承继的接口。其界说以下:
packagehttpserver;
importjava.net.Socket;
importjava.io.IOException;

publicinterfaceJsp2HtmlInterface{
voidprintHtml(Socketsocket)throwsIOException;
}

接上去就写一个类JavaServerPage卖力剖析JSP文件,在这个类中将会静态天生JAVA文件,并编译天生一个可用类。再挪用这个类文件的printHtml(socket)办法,将呼应信息写给客户端(个中socket为服务器accept前往的套接口。如许客户就可以看到JSP准确运转的了局。这个剖析JavaServerPage的类框架以下:
publicclassJavaServerPage{
publicJavaServerPage(Filefile,Socketsocket)throwsException{}//file为客户端哀求的一个JSP文件
//socket为服务器同客户端连接的套接口
publicvoidwriteReponse(){//这个函数将卖力依据指定的JSP文件依据某种
GeneratJava();//静态天生java文件并将编译,
Processproc=Runtime.getRuntime().exec("javac...");
try{
proc.waitFor();
}catch(InterruptedExceptionie){}
httpserver.Jsp2HtmlInterfaceobject=(httpserver.Jsp2HtmlInterface)Class.forName("...").newInstance();
object.printHtml(socket);
}
}

假定我们用某种算法将下面的JSP文件天生以下的JAVA文件
packagehttpserver;
importjava.io.*;
importjava.awt.*;
importjava.net.Socket;
publicclasstestimplementsJsp2HtmlInterface{
privatePrintStreamout;
publictest(){}
publicvoidprintln(Objectobject)throwsIOException{
out.print(object.toString()+"
");}
publicvoidprintln(inti)throwsIOException{
out.print(i+"
");}
publicvoidprintln(floati)throwsIOException{
out.print(i+"
");}
publicvoidprintln(doublei)throwsIOException{
out.print(i+"
");}
publicvoidprintln(longi)throwsIOException{
out.print(i+"
");}
publicvoidprintln(charch)throwsIOException{
out.print(ch+"
");}
publicvoidprintln(char[]ch)throwsIOException{
out.print(ch+"
");}
publicvoidprintln()throwsIOException{
out.print("
");
}
publicvoidclose(){
out.close();
}
publicvoidprintHtml(Socketsocket)throwsIOException{
out=newPrintStream(socket.getOutputStream());
this.println("<html>");
this.println("<title>Hello</title>");
this.println("<body>");
java.util.Datedate=newjava.util.Date();
intsize=10;



for(inti=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大概完成其余甚么静态剧本言语了。




不得不提一下的是:.net是看到java红,而开发出来的工具。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-11 20:38

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表