当前位置: 移动技术网 > IT编程>开发语言>Java > JSP生成静态页实践及其设计思想[转]

JSP生成静态页实践及其设计思想[转]

2017年12月12日  | 移动技术网IT编程  | 我要评论
引子:
我们以两个大型网站为例作比较:
51job和智联招聘(先声明我不是为他们作广告,仅以他们为例作技术上的比较)
51job采用的是比较“先进”的php技术,而智联用的是比较落后的asp.但我们可能会明显的感觉到51job的反应速度相比智联招聘实在是太慢了,为什么会这样?细心的人可能会察觉到了。智联虽然用的是asp,但他采用了另一种更巧妙的技术--asp生成静态页技术。所有的动态页基本上都转换成了html静态页,不用访问数据库,当然反应快了。
下面我们讨论一下jsp怎么转换成html??
首先要做一个模板。后缀不限,但一般都用*.template例子
<html>
<head>
<title>#title#</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<link href="../css.css" rel=stylesheet type=text/css>
</head>

<body>
<p align="center">
#title#<br><br><br>
作者:#author#<br><br>
<br>
#content#<br><br><br><br>
</p>
</body>
</html>
做一个处理模板的类或者jsp文件、(为说明问题我们从简单入手以一个jsp文件为例)
 filepath = request.getrealpath("/")+"web-inf/templates/template.htm";
 out.print(filepath);
 string templatecontent="";
 fileinputstream fileinputstream = new fileinputstream(filepath);//读取模块文件
 int lenght = fileinputstream.available();
 byte bytes[] = new byte[lenght];
 fileinputstream.read(bytes);
 fileinputstream.close();
 templatecontent = new string(bytes);
 out.print("以下是模板内容:<br>"+templatecontent+"<br> 以下是置换以后的html内容<br><hr>");
 templatecontent=templatecontent.replaceall("#title#",title);
 templatecontent=templatecontent.replaceall("#author#",editer);//替换掉模块中相应的地方
 templatecontent=templatecontent.replaceall("#content#",content);
 // 根据时间得文件名
 calendar calendar = calendar.getinstance();
 string fileame = string.valueof(calendar.gettimeinmillis()) +".html";
 fileame = request.getrealpath("/")+fileame;//生成的html文件保存路径

 out.print(templatecontent);
 fileoutputstream fileoutputstream = new fileoutputstream(fileame);//建立文件输出流
 byte tag_bytes[] = templatecontent.getbytes();
 fileoutputstream.write(tag_bytes);
 fileoutputstream.close();
嗯,核心技术就是这样了,如果大家要求的性能更高,可以改用。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网