最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > jsp教程

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

日期:08-22    来源:中国设计秀    作者:cnwebshow.com

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

<body>X44中国设计秀
<P align="center">X44中国设计秀
#title#<BR><BR><BR>X44中国设计秀
作者:#author#<BR><BR>X44中国设计秀
<BR>X44中国设计秀
#content#<BR><BR><BR><BR>X44中国设计秀
</P>X44中国设计秀
</body>X44中国设计秀
</html>X44中国设计秀
做一个处理模板的类或者jsp文件、(为说明问题我们从简单入手以一个jsp文件为例)X44中国设计秀
 filePath = request.getRealPath("/")+"WEB-INF/templates/template.htm";X44中国设计秀
 out.PRint(filePath);X44中国设计秀
 String templateContent="";X44中国设计秀
 FileInputStream fileinputstream = new FileInputStream(filePath);//读取模块文件X44中国设计秀
 int lenght = fileinputstream.available();X44中国设计秀
 byte bytes[] = new byte[lenght];X44中国设计秀
 fileinputstream.read(bytes);X44中国设计秀
 fileinputstream.close();X44中国设计秀
 templateContent = new String(bytes);X44中国设计秀
 out.print("以下是模板内容:<br>"+templateContent+"<br> 以下是置换以后的html内容<br><hr>");X44中国设计秀
 templateContent=templateContent.replaceAll("#title#",title);X44中国设计秀
 templateContent=templateContent.replaceAll("#author#",editer);//替换掉模块中相应的地方X44中国设计秀
 templateContent=templateContent.replaceAll("#content#",content);X44中国设计秀
 // 根据时间得文件名X44中国设计秀
 Calendar calendar = Calendar.getInstance();X44中国设计秀
 String fileame = String.valueOf(calendar.getTimeInMillis()) +".html";X44中国设计秀
 fileame = request.getRealPath("/")+fileame;//生成的html文件保存路径X44中国设计秀

 out.print(templateContent);X44中国设计秀
 FileOutputStream fileoutputstream = new FileOutputStream(fileame);//建立文件输出流X44中国设计秀
 byte tag_bytes[] = templateContent.getBytes();X44中国设计秀
 fileoutputstream.write(tag_bytes);X44中国设计秀
 fileoutputstream.close();X44中国设计秀
嗯,核心技术就是这样了,如果大家要求的性能更高,可以改用freemarker做模板。X44中国设计秀

经过一翻调试,成功了。。呵X44中国设计秀

附上源码。。X44中国设计秀

X44中国设计秀
JDK 1.5 +ECLipSE +TOMCAT 5.0.28 + MySQL 5.0X44中国设计秀

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

makeFile.jspX44中国设计秀

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

 %>X44中国设计秀
 X44中国设计秀
  <%X44中国设计秀
  X44中国设计秀
  String filePath = request.getRealPath("/")+"template.htm";X44中国设计秀

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

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

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

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

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

}X44中国设计秀

if(conn!=null)X44中国设计秀
    {X44中国设计秀
        conn.close();X44中国设计秀
    }X44中国设计秀
    if(stmt!=null)X44中国设计秀
    {X44中国设计秀
        stmt.close();X44中国设计秀
    }X44中国设计秀

X44中国设计秀
  X44中国设计秀
  %>X44中国设计秀

//数据库连接文件X44中国设计秀

import java.sql.*;X44中国设计秀
public class DBconn {X44中国设计秀
    X44中国设计秀
    public DBconn() {X44中国设计秀
        X44中国设计秀
        // TODO Auto-generated constructor stubX44中国设计秀
    }X44中国设计秀

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

 X44中国设计秀

// 模板文件X44中国设计秀

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

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

本文引用地址:/bc/article_46651.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明