| 网站首页 | 文章中心 | 电子书下载 | 矢量图库 | 视频教程 | 素材下载 | 程序代码下载 | JS代码 | 论坛 | 
龙腾教程网  
常用软件类:
|杀毒安全 |联络聊天 |网络软件 |多媒体类 |系统工具 |图形图像 |系统工具 |应用软件 |行业软件
开发设计类:
|动画制作 |图像处理 |3D设计 |操作系统 |站长学院 |网络相关 |WEB设计 |数据库类 |程序开发
 
  您当前位置:您现在的位置: 龙腾软件教程网 >> 文章中心 >> WEB设计 >> JSP >> 技巧心得 >> 文章正文 
 
JSP生成静态HTML页面的实践方法和设计思路

JSP生成静态HTML页面的实践方法和设计思路

我们以两个大型网站为例作比较:
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();
嗯,核心技术就是这样了,如果大家要求的性能更高,可以改用freemarker做模板。

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

附上源码。。


JDK 1.5 +ECLIPSE +TOMCAT 5.0.28 + MYSQL 5.0

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

makeFile.jsp

 <%
  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];

fileinputstream.read(bytes); //read(byte[] b) 从此输入流中将最多 b.length 个字节的数据读入一个字节数组中。

fileinputstream.close();
//templateContent = new String(bytes);
String title;
String content;
String author;
while(Rs.next())
{
templateContent = new String(bytes);//如果不用这句,则替换一次之后,templateContent中就没有#**#标志了。所以要重新生成
 title = Rs.getString("Title");
 content = Rs.getString("Content");
 author = Rs.getString("Author");
out.println(title+"********"+content+"****"+author);
out.print("以下是模板内容:<br>"+templateContent+"<br> 以下是置换以后的html内容<br><hr>");
templateContent=templateContent.replaceAll("#title#",title);
templateContent=templateContent.replaceAll("#author#",author);//替换掉模块中相应的地方
templateContent=templateContent.replaceAll("#content#",content);

// 根据时间得文件名
Calendar calendar = Calendar.getInstance();
String fileame = String.valueOf(calendar.getTimeInMillis()) +".html";
fileame = request.getRealPath("/")+"Html/"+fileame;//生成的html文件保存路径


out.print(templateContent);
FileOutputStream fileoutputstream = new FileOutputStream(fileame);//建立文件输出流
byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();

}

if(conn!=null)
    {
        conn.close();
    }
    if(stmt!=null)
    {
        stmt.close();
    }


 
  %>

//数据库连接文件

import java.sql.*;
public class DBconn {
   
    public DBconn() {
       
        // TODO Auto-generated constructor stub
    }

    public static Connection getConnection()
    {
        Connection conn = null;
       
        try {
            Class.forName("org.gjt.mm.mysql.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://" + "localhost" + "/" + "test" +
        "?useUnicode=true&characterEncoding=GB2312","root","111111");
               
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return conn;
        }
    /*public static void main(String[] args) throws Exception
    {
        Connection con=getConnection();
        System.out.println(con.isClosed());
       
    }
*/
}

// 模板文件

template.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>#title#</title>
</head>

<body>
<table width="380" height="107" border="0" cellpadding="0" cellspacing="1" bgcolor="#FFCC99">
  <tr>
    <td height="16" bgcolor="#FFCC99"><div align="center">#title#</div></td>
  </tr>
  <tr>
    <td bgcolor="#FFFFFF">#content#</td>
  </tr>
  <tr>
    <td height="13" align="right" bgcolor="#FFFFFF">#author#</td>
  </tr>
</table>
</body>
</html>

为了减轻服务器压力,将原来的文章管理系统由JSP文件的从数据库中取数据显示改为由jsp生成静态html文件后直接访问html文件。下面是一个简单的示例

1.buildhtml.jsp

<%@ page contentType="text/html; charset=gb2312" import="java.util.*,java.io.*"%>
<%
try{
 String title="This is Title";
 String content="This is Content Area";
 String editer="LaoMao";
 String filePath = "";
 filePath = request.getRealPath("/")+"test/template.htm";
 //out.print(filePath+"<br>");
 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(templateContent);
 templateContent=templateContent.replaceAll("###title###",title);
 templateContent=templateContent.replaceAll("###content###",content);
 templateContent=templateContent.replaceAll("###author###",editer);//替换掉模块中相应的地方
 //out.print(templateContent);
 // 根据时间得文件名
 Calendar calendar = Calendar.getInstance();
 String fileame = String.valueOf(calendar.getTimeInMillis()) +".html";
 fileame = request.getRealPath("/")+fileame;//生成的html文件保存路径
 FileOutputStream fileoutputstream = new FileOutputStream(fileame);//建立文件输出流
 byte tag_bytes[] = templateContent.getBytes();
 fileoutputstream.write(tag_bytes);
 fileoutputstream.close();
}
catch(Exception e){
 out.print(e.toString());
}

%>

2. template.htm

<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>
<table width="500" border="0" align="center" cellpadding="0" cellspacing="2">
  <tr>
    <td align="center">###title###</td>
  </tr>
  <tr>
    <td align="center">author:###author###&nbsp;&nbsp;</td>
  </tr>
  <tr>
    <td>###content###
 </td>
 
  </tr>

</table>
</body>
</html>


 


上一篇:
  • 上一篇文章:
  • 下一篇
  • 下一篇文章: 没有了
  • 收藏此文到百度搜藏 百度搜藏| 新浪VIvi| 365key| Younote| 博采中心| 你好BLOG| 亿友网摘| 和讯网摘|
    相关文章    
    JSP连接Oracle数据库常见问题
    JSP生成网页静态HTML页方法实践及其设计思想
    jsp数据库操作心得
    JSP经典分页程序实列代码
    JPS环境配置- TOMCATv5.0.16安装图解
    jsp自定义标签介绍与操作实例
    解决JSP中使用request乱码问题
    Java技术在多数据库系统中的应用研究
    Linux系统下安装JSP
    Windwows系统的JSP服务器配置
     
     
     
    最新文章
    普通文章 JSP生成静态HTML页面的实践方法和最新文章
    普通文章 Cisco路由器密码和版本恢复技巧实最新文章
    普通文章 Cisco PIX上实现VPN步骤最新文章
    普通文章 Cisco交换机的MAC地址与IP绑定最新文章
    普通文章 Windows Moblie 笔迹图板开发最新文章
    普通文章 Coreldraw绘制可爱的情侣QQ企鹅最新文章
    普通文章 Photoshop制作端午节贺卡最新文章
    普通文章 Photoshop笔刷和蒙版修改照片色彩最新文章
    普通文章 详细讲解家用Linux的实用安全最新文章
    普通文章 Flash制作晃动的烛光最新文章
     
    热门文章
  • 没有推荐文章
  •  
     
    设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 

    版权所有2006-2008 龙腾教程网