网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > JSP技巧
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.打开页面就是全屏的方法.
.当Tomcat遭遇JBDC Driver,出现 .
.关于JSP中基于Session的在线用户.
.jsp从数据库取得数据作为下拉菜单.
.JSP中基于Session的在线用户统计.
.解决JSP开发Web程序中的中文问题.
.JSP中表单数据存储的一种通用方法.
.JSP/Servlet的URL重定向.
.Servlet、Jsp中的多国语言显示.
.Oracle数据库和JSP连接要注意的一.
.攻破JNDI连接池(Tomcat5.5下通过.
.JSP连接mysql数据库攻略.
.JSP中文乱码问题解决方法小结.
.快速应用JDBC控件访问数据库资源.
.JSP漏洞大观.
.学习在JSP中使用JavaBeans.
.tomcat频繁死掉的问题.
.jsp中任意文字转Unicode的通用模.
.JSP实现浏览器关闭cookies情况下.
.Java代码编写的一般性指导.

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

发表日期:2007-2-14


引子:
我们以两个大型网站为例作比较:
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自定义标签 人气:4355
下一篇:JSP生成静态页实践及其设计思想[2] 人气:5370
浏览全部JSP的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐