我们一直做httpserver的时候,请求返回html文件的时候,都是得到一个PrintWriter对象,然后在
out.println("<HTML>");
out.println("<HEAD><TITLE>Demo<TITLE><HEAD>");
out.println("<BODY>");
out.println("<H1>Demo Header<H1>");
out.println("<H3>Sub Header:<H3>");
out.println("<FONT SIZE=\"+1\" FACE=\"Times\" COLOR=\"#FFFFFF">);
out.println("The big dog & the little cat chased each other.");
out.println("<FONT>");
out.println("<BODY>");
out.println("<HTML>");
如果返回的html文件中包含的内容比较少,这样做还是可以的.不过一直用out.println,大家没觉得有点太过于陈旧,一直用一种方法.
今天上apache的网站,发现
Jakarta ECS可以直接生成一个html对象(在java中一切皆为对象),用起来也是比较简单的.在apache网站中介绍
Jakarta ECS(由于本人e文不怎么好,不能给大家翻译)
The Element Construction Set is a Java API for generating elements for various markup languages it directly supports HTML 4.0 and XML, but can easily be extended to create tags for any markup language. It is designed and implemented by Stephan Nagy and Jon S. Stevens.
其实,我们看一个java的第三方jar,重要的是去看的api,看的方法是怎么用以及知道这个jar是用来干什么的.
我把jar包以及api下载了,下载地址:http://apache.mirrors.hoobly.com/jakarta/ecs/binaries/ecs-1.4.2.tar.gz,这里面包括jar以及api.
下载后把jar放到eclipse下,相信导入一个jar到一个工程中大家都应该知道了,牛刀小试.
package ECS;
import org.apache.ecs.HtmlColor;
import org.apache.ecs.html.Body;
import org.apache.ecs.html.Font;
import org.apache.ecs.html.H1;
import org.apache.ecs.html.H3;
import org.apache.ecs.html.Head;
import org.apache.ecs.html.Html;
import org.apache.ecs.html.Title;
public class TestECS {
public static void main(String[] args) {
Html html = new Html()
.addElement(new Head().addElement(new Title("Demo")))
.addElement(
new Body()
.addElement(new H1("Demo Header"))
.addElement(new H3("Sub Header:"))
.addElement(
new Font()
.setSize("+1")
.setColor(HtmlColor.WHITE)
.setFace("Times")
.addElement(
"The big dog & the little cat chased each other.")));
System.out.println(html.toString());
}
}
具体详细的大家可以用apache网站看,或是看api.apache网站关于
ECS,地址:http://jakarta.apache.org/ecs/
心动了么?还不行动,尝试一下ECS!