ECS来创建我们的HTML元素
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.ecs.html.A;
import org.apache.ecs.html.BR;
import org.apache.ecs.html.Body;
import org.apache.ecs.html.Form;
import org.apache.ecs.html.Head;
import org.apache.ecs.html.Html;
import org.apache.ecs.html.Input;
import org.apache.ecs.html.LI;
import org.apache.ecs.html.Label;
import org.apache.ecs.html.Meta;
import org.apache.ecs.html.TD;
import org.apache.ecs.html.TR;
import org.apache.ecs.html.Table;
import org.apache.ecs.html.Title;
import org.apache.ecs.html.UL;
/**
* 利用Apache提供的ECS来创建我们的HTML元素、通常和自定义标签结合使用
* 需要ecs-1.4.2.jar的支持
* @author LiChuntao
* 2014-5-22
*/
publicclass ECSServlet extends HttpServlet {
private static final longserialVersionUID = 1L;
publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/* 设置编码以及IO对象 */
request.setCharacterEncoding("gbk");
response.setContentType("text/html;charset=gbk");
PrintWriter pw = response.getWriter();
/* 构建html页面,并设置相关属性 */
Html html = new Html();
/* 构建html的head部分 */
Head head = new Head();
head.addElement(new Title("esc设置的标题"));
Meta meta = new Meta();
meta.addAttribute(" http-equiv", "Content-Type");
meta.addAttribute("content", "text/html; charset=gbk");
head.addElement(meta);
/* 构建html的body部分 */
Body body = new Body();
body.addAttribute("style", "border:1px solid #cdcdcd");
body.addAttribute("title", "我是ecs做的body");
//table 部分
Table table = new Table();
table.addAttribute("id", "escTableID");
TR tr = new TR();
tr.addAttribute("style", "background:#ebebeb");
tr.addElement(new TD("姓名").addAttribute("width", "100px;"));
tr.addElement(new TD("性别").addAttribute("width", "100px;"));
tr.addElement(new TD("身份证号").addAttribute("width", "100px;"));
TR tr2 = new TR();
tr2.addElement(new TD("李春涛"));
tr2.addElement(new TD("男"));
tr2.addElement(new TD().addElement(new A("www.baidu.com").addElementToRegistry("230223199108203031")));
body.addElement(table.addElement(tr).addElement(tr2)).addElement(new BR());
//form 部分
Form form = new Form();
form.addAttribute("name", "ecsForm");
form.addAttribute("action","testAction!to_ecs.action");
form.addAttribute("method","post");
UL ul = new UL();
ul.addAttribute("style", "list-style:none");
LI liName = new LI();
liName.addElement(new Label().addElementToRegistry("姓名:")).addElement(new Input().addAttribute("name", "name"));
LI liGender = new LI();
liGender.addElement(new Label().addElementToRegistry("年龄:")).addElement(new Input().addAttribute("name", "age"));
LI liIDNumber = new LI();
liIDNumber.addElement(new Label().addElementToRegistry("身份证号:")).addElement(new Input().addAttribute("name", "idnumber"));
ul.addElement(liName).addElement(liGender).addElement(liIDNumber);
body.addElement(form.addElementToRegistry(ul));
/* 各部分元素合并成页面 */
html.addElement(head).addElement(body);
pw.print(html);
}
publicvoid doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}