您现在的位置:计算机技术学习网 > 技术中心 > WEB编程 > JSP >

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

来源:未知 责任编辑:智问网络 发表时间:2013-10-30 11:08 点击:

引子:
我们以两个大型网站为例作比较:
51job和智联招聘(先声明我不是为他们作广告,仅以他们为例作技术上的比较)
51job采用的是比较“先进”的php技术,而智联用的是比较落后的asp.但我们可能会明显的感觉到51job的反应速度相比智联招聘实在是太慢了,为什么会这样?细心的人可能会察觉到了。智联虽然用的是asp,但他采用了另一种更巧妙的技术--asp生成静态页技术。所有的动态页基本上都转换成了html静态页,不用访问数据库,当然反应快了。
下面我们讨论一下jsp(SUN企业级应用的首选)怎么转换成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(SUN企业级应用的首选)文件、(为说明问题我们从简单入手以一个jsp(SUN企业级应用的首选)文件为例)
 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();
嗯,核心技术就是这样了,如果大家要求的性能更高,可以改用freemarker做模板。

经过一翻调试,成功了。。呵

附上源码。。


JDK 1.5 +ECLIPSE +tomcat(一个很好用的JSP运行平台) 5.0.28 + MySQL(和PHP搭配之最佳组合) 5.0

数据库TEST ,表名news
字段: id   int 自动增长 , Title  varchar(20) , Content  varchar(200)  , Author  varchar(10)

makeFile.jsp(SUN企业级应用的首选)

 <%
  Connection conn = DBconn.getConnection();
  Statement stmt = conn.createStatement();
  ResultSet Rs = stmt.executeQuery("select * from news");
  System.out.println("success");

 %>
 
  <%
 
  String filePath = request.getRealPath("/")+"template.htm";

  System.out.println(filePath);
 
  String templateContent;
  FileInputStream fileinputstream = new FileInputStream(filePath);
  int lenght = fileinputstream.available(); //available() 返回可以不受阻塞地从此文件输入流中读取的字节数。
 
byte bytes[] = new byte[lenght];

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • JSP与Servlet
    • 自己动手写MiniBBS系列(基本篇)之用户登录
    • JSP取当前日期
    • JDBC 入门(一)
    • 打开一个jsp页面默认查询所有数据,调用action
    • 使用JSP标签库验证用户的输入(2)完
    • WIN98/2000下的jsp服务器
    • 自定义JSP标签(tag)浅议
    • Struts学习傻瓜式入门篇
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1