您的位置:首页 > 编程语言 > Java开发

利用Java生成静态HTML页面

2011-12-29 09:43 483 查看
导读:

  这几天公司需要生成静态的HTML页面以减小数据库与服务器的压力和负担,于是在网络上一阵狂搜,找到几篇相当不错的文章和一些相当有用的资料。为了方便,我整理在自己的BLOG,以供参考!

  在接下来的应用中,我自己想到另一种解决方案,就是通过Ajax + Struts + XML解决静态页面方案,一并写下来,与大家分享!

  生成静态页面技术解决方案之一(转载)

  生成静态页面技术解决方案之二(转载)

  一个实现将动态页面转为静态的方案(转载)

  JSP生成静态HTML页面范例(转载)

  利用XML+XSL生成静态页面技术方案(转载)

  Ajax + Struts + XML解决静态页面方案(原创)

  随着网站访问量的加大,每次从数据库读取都是以效率作为代价的,静态页加在搜索时,也会被优先考虑。互联网上流行的做法是将数据源代码写入数据库再从数据库读取生成静态面,这样无形间就加大了数据库。将现有的JSP页直接生成静态页,将会节省很多。

  为什么要生成静态首页?

  1、如果你首页读取的数据库次数比较多,速度很慢,而且占用很多服务器资源。使用静态页面访问速度当然快多了

  2、搜索引擎容易搜索到

  3、如果程序出问题,也能保证首页能访问

  诸如此类等等好处,那么下面几篇文章给大家几个完整的解决方案!

  ----------------------------------------------------------------------------------------------------

  转载者前言:这是一个全面的jsp动态页面静态化方案,本站的帖子静态化方案将借鉴这篇帖子中方法。向http://www.agilejava.org的single的共享精神致敬。

  转帖正文:

  相信很多人都希望自己的页面越快越好,最好是能静态的,提高客户访问速度。也便于搜索引擎搜索。所以,就希望我们的动态读取数据库的页面,尽可能的生成静态页面。一下系列文章,介绍一下个人的解决方案。

  本系列将介绍个人的一种方法,在不改变原来jsp文件的基础上,只需要加入少量的代码,就让你的新闻发布系统,很容易就完全变成静态的页面。

  本文假设你是用java开发的web动态页面。

  第一步,加入servlet.代码如下。

  public class ToHtml extends HttpServlet {

  public void service(HttpServletRequest request, HttpServletResponse response)

  throws ServletException, IOException {

  String url = "";

  String name = "";

  ServletContext sc = getServletContext();

  String file_name = request.getParameter("file_name");// 你要访问的jsp文件名,如index,不包括扩展名

  // 则你访问这个servlet时加参数.如http://localhost/test/toHtml?file_name=index

  url = "/" + file_name + ".jsf";// 你要生成的页面的文件名。我的扩展名为jsf .

  name = ConfConstants.CONTEXT_PATH+"//"+ file_name + ".htm";// 这是生成的html文件名,如index.htm.文件名字与源文件名相同。扩展名为htm

  //ConfConstants.CONTEXT_PATH为你的应用的上下文路径。

  RequestDispatcher rd = sc.getRequestDispatcher(url);

  final ByteArrayOutputStream ōs = new ByteArrayOutputStream();

  final ServletOutputStream stream = new ServletOutputStream() {

  public void write(byte[] data, int offset, int length) {

  os.write(data, offset, length);

  }

  public void write(int b) throws IOException {

  os.write(b);

  }

  };

  final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));

  HttpServletResponse rep = new HttpServletResponseWrapper(response) {

  public ServletOutputStream getOutputStream() {

  return stream;

  }

  public PrintWriter getWriter() {

  return pw;

  }

  };

  rd.include(request, rep);

  pw.flush();

  FileOutputStream fos = new FileOutputStream(name); // 把jsp输出的内容写到xxx.htm

  os.writeTo(fos);

  fos.close();

  PrintWriter ōut = response.getWriter();

  out

  .print("
页面已经成功生成!single
http://www.agilejava.org/space/? 233
");

  }

  }

  第二步、配置你的web.xml

  

  toHtml

  mj.util.html.ToHtml//你的servlet的类。

  

  

  toHtml

  /toHtml

  

  第三步、运行servlet。如:http://localhost:8080/test/toHtml?file_name=index

  

  OK,这就在你的test项目的根目录下,生成了一个index.htm的静态文件。 

  

  局限性:本文只能生成一个文件!访问一次,生成一个文件。并且生成的文件名也与原来的文件名相同。

  比较适合主页生成静态页面。

  

  本系列的后续文章将解决更多的问题。使之在新闻发布系统中,很容易就集成应用。

  ----------------------------------------------------------------------------------------------------

  注意:转贴本文,请加上本文链接http://www.agilejava.org/space/?233/action_viewspace_itemid_21.html

  在上一篇文章中,生成静态页面,是有一定的局限性的。生成主页是很方便,但要生成二级页面,就不方便了。

  本文假设一个新闻发布系统。希望后台发布的,前台显示的是静态的文档。这就涉及,主页要是静态的,同时二级列表也是静态的,新闻内容也是静态的。也就是说, 在发布一篇新闻的时候,可能涉及到三个地方生成静态文档。并且,要生成一个网页,必须访问一个servlet。在大量生成静态网页的时候,

  以下方法,可以解决这些问题。

  一、加入一下servelet

  /**

  * @file_name 文件名及文件之后的参数.最好为a.jsf?fileId=aaaa

  * @path 文件所在的路径.相对于根目录而言的.

  * @realName文件要保存的名字

  * @realPath文件要保存的真实路径。默认与文件所在的目录相同。

  */

  public class ToHtmlPath extends HttpServlet {

  public void service(HttpServletRequest request, HttpServletResponse response)

  throws ServletException, IOException {

  String url = "";

  String name = "";

  ServletContext sc = getServletContext();

  String file_name = request.getParameter("file_name");// 你要访问的jsp文件,如news.jsf。

  // file_name如:fileDetail.jsf?fileId=56.要是有参数, 只有一个参数。并且以参数名作为文件名。

  String realName = request.getParameter("realName");// 要保存的文件名。如aaa;注意可以没有这个参数。

  String path = request.getParameter("path");// 你要访问的jsp文件路径。如news。注意可以没有这个参数。

  String realPath = request.getParameter("realPath");// 你要保存的文件路径,如htmlNews.注意可以没有这个参数。

  // 下面确定要保存的文件名字。

  if (realName == null || realName == "") {

  int a = 0;

  a = file_name.indexOf("=") + 1;

  realName = file_name.substring(a);

  if (realName.indexOf(".")>0) {

  realName = file_name.substring(0, file_name.indexOf("."));

  }

  }

  // 下面构造要访问的页面。

  if (path == null || path == "") {

  url = "/" + file_name;// 这是你要生成HTML的jsp文件,如

  } else {

  url = "/" + path + "/" + file_name;// 这是你要生成HTML的jsp文件,如

  }

  // 下面构造要保存的文件名,及路径。

  // 1、如果有realPath,则保存在realPath下。

  // 2、如果有path则保存在path下。

  // 3、否则,保存在根目录下。

  if (realPath == null || realPath == "") {

  if (path == null || path == "") {

  name = ConfConstants.CONTEXT_PATH + "//" + realName + ".htm";// 这是生成的html文件名,如index.htm.说明: ConfConstants.CONTEXT_PATH为你的上下文路径。

  } else {

  name = ConfConstants.CONTEXT_PATH + "//" + path + "//"

  + realName + ".htm";// 这是生成的html文件名,如index.htm.

  }

  } else {

  name = ConfConstants.CONTEXT_PATH + "//" + realPath + "//"

  + realName + ".htm";// 这是生成的html文件名,如index.htm.

  }

  // 访问请求的页面,并生成指定的文件。

  RequestDispatcher rd = sc.getRequestDispatcher(url);

  final ByteArrayOutputStream ōs = new ByteArrayOutputStream();

  final ServletOutputStream stream = new ServletOutputStream() {

  public void write(byte[] data, int offset, int length) {

  os.write(data, offset, length);

  }

  public void write(int b) throws IOException {

  os.write(b);

  }

  };

  final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));

  HttpServletResponse rep = new HttpServletResponseWrapper(response) {

  public ServletOutputStream getOutputStream() {

  return stream;

  }

  public PrintWriter getWriter() {

  return pw;

  }

  };

  rd.include(request, rep);

  pw.flush();

  FileOutputStream fos = new FileOutputStream(name); // 把jsp输出的内容写到xxx.htm

  os.writeTo(fos);

  fos.close();

  PrintWriter ōut = response.getWriter();

  out.print("
success!
");

  }

  }

  

  二、在web.xml里面配置你的servlet

  

  toHtmlPath

  mj.util.html.ToHtmlPath

  

  

  toHtmlPath

  /toHtmlPath

  

  三、写一个通用的方法, 供调用。

  public class CallHtml {

  public static void callOnePage(String fileName, String path,

  String realName, String realPath) {

  try {

  String str = "http://localhost:8080/test/toHtmlPath?file_name="

  + fileName + "&&path=" + path + "&&realName=" + realName

  + "&&realPath=" + realPath;

  int httpResult;

  URL url = new URL(str);

  URLConnection connection = url.openConnection();

  connection.connect();

  HttpURLConnection httpURLConnection = (HttpURLConnection) connection;

  httpResult = httpURLConnection.getResponseCode();

  if (httpResult != HttpURLConnection.HTTP_OK) {

  System.out.println("没有连接成功");

  } else {

  System.out.println("连接成功了 ");

  }

  } catch (Exception e) {

  // TODO: handle exception

  }

  }

  //这个方法适当重载,就可以省去一些参数传递。

  }

  四、在你的新闻发布save时,调用方法。

  1、CallHtml.callOnePage("info.jsf?file_id=aaa",news,"", "");//将在news目录下生成一个aaa.htm的静态文件

  2、CallHtml.callOnePage("newsList.jsf",news,"", "");//将在news目录下生成一个newsList.htm的静态文件,显示最新的新闻。

  3、CallHtml.callOnePage("index.jsf","","", "");//生成主页。

  好了,这就保持了,主页、列表、新闻内容都是最新的静态页面了。

  ----------------------------------------------------------------------------------------------------

  1.前言

  为了能深入浅出的理解这个框架的由来,我们首先来了解一下JSP解析器将我们写的JSP代码转换成的JAVA文件的内容。

  下面是一个JSP文件test.jsp

  经过TOMCAT转换出的JAVA文件test$jsp.java内容如下:

  package org.apache.jsp;

  import javax.servlet.*;

  import javax.servlet.http.*;

  import javax.servlet.jsp.*;

  import org.apache.jasper.runtime.*;

  

  public class test$jsp extends HttpJspBase {

  

  static {

  }

  public testOutRedir$jsp( ) {

  }

  

  private static boolean _jspx_inited = false;

  

  public final void _jspx_init() throws org.apache.jasper.runtime.JspException {

  }

  

  public void _jspService(HttpServletRequest request, HttpServletResponse response)

  throws java.io.IOException, ServletException {

  

  JspFactory _jspxFactory = null;

  PageContext pageContext = null;

  HttpSession session = null;

  ServletContext application = null;

  ServletConfig config = null;

  JspWriter out= null;

  Object page = this;

  String _value = null;

  try {

  

  if (_jspx_inited == false) {

  synchronized (this) {

  if (_jspx_inited == false) {

  _jspx_init();

  _jspx_inited = true;

  }

  }

  }

  _jspxFactory = JspFactory.getDefaultFactory();

  response.setContentType(text/html;charset=GB2312);

  pageContext = _jspxFactory.getPageContext(this, request, response,

  , true, 8192, true);

  

  application = pageContext.getServletContext();

  config = pageContext.getServletConfig();

  session = pageContext.getSession();

  out= pageContext.getOut();

  //为了节省篇幅,我删除了解释器添加的注释

  out.write(/r/n);

  //上一句是由于后面的换行产生的

  out.write();

  out.write(/r/n/r/n/r/n/r/n);

  out.print( 输出 );

  out.write(/r/n/r/n/r/n/r/n);

  } catch (Throwable t) {

  if (out!= null &&out.getBufferSize() != 0)

  out.clearBuffer();

  if (pageContext != null) pageContext.handlePageException(t);

  } finally {

  if (_jspxFactory != null) _jspxFactory.releasePageContext(pageContext);

  }

  }

  }

  从上面的代码中可以清晰的看到JSP内建的几个对象(out、request、response、session、pageContext、application、config、page)是怎么产生的,懂servlet的朋友一看就能明白。

  下面重点理解一下out对象,它被声明为JspWriter类型,JspWriter是一个抽象类,在包javax.servlet.jsp中可以找到它的定义。

  abstractpublicclass javax.servlet.jsp.JspWriter extends java.io.Writer{

  final public static intNO_BUFFER = 0;

  final public static intDEFAULT_BUFFER = -1;

  final public static intUNBOUNDED_BUFFER = -2;

  protected intbufferSize;

  protected BooleanautoFlush;

  protectedjavax.servlet.jsp.JspWriter(intarg1, booleanarg2);

  

  abstractpublicvoidnewLine() throwsIOException ;

  abstractpublicvoidprint(booleanarg0) throwsIOException ;

  abstractpublicvoidprint(chararg0) throwsIOException ;

  abstractpublicvoidprint(intarg0) throwsIOException ;

  abstractpublicvoidprint(longarg0) throwsIOException ;

  abstractpublicvoidprint(floatarg0) throwsIOException ;

  abstractpublicvoidprint(doublearg0) throwsIOException ;

  abstractpublicvoidprint(char[] arg0) throwsIOException ;

  abstractpublicvoidprint(String arg0) throwsIOException ;

  abstractpublicvoidprint(Object arg0) throwsIOException ;

  abstractpublicvoidprintln() throwsIOException ;

  abstractpublicvoidprintln(booleanarg0) throwsIOException ;

  abstractpublicvoidprintln(chararg0) throwsIOException ;

  abstractpublicvoidprintln(intarg0) throwsIOException ;

  abstractpublicvoidprintln(longarg0) throwsIOException ;

  abstractpublicvoidprintln(floatarg0) throwsIOException ;

  abstractpublicvoidprintln(doublearg0) throwsIOException ;

  abstractpublicvoidprintln(char[] arg0) throwsIOException ;

  abstractpublicvoidprintln(String arg0) throwsIOException ;

  abtractpublicvoidprintln(Object arg0) throwsIOException ;

  abstractpublicvoidclear() throwsIOException ;

  abstractpublicvoidclearBuffer() throwsIOException ;

  abstractpublicvoidflush() throwsIOException ;

  abstractpublicvoidclose() throwsIOException ;

  publicintgetBufferSize() ;

  abstractpublicintgetRemaining() ;

  publicboolean isAutoFlush() ;

  }

  我相信当我写到这里你可能已经知道我想怎么做了。是的,来个偷天换日,继承JspWriter类,然后实现其定义的虚函数,然后把out变量替换成你自己实现的类的实例就ok了。

  2.实现替换

  假设

  3.更新问题

  下面就讨论一下如何更新生成静态文件,其实从上面实现中你可以看到,很简单的就是将生成的静态文件删除即可,至于什么时候删除,要看你的需求了。我能想到的几种情况如下

  当用来生成页面的数据更新时

  如果不需要很提供时时的数据可以定时更新

  永远不更新

  ----------------------------------------------------------------------------------------------------

  先建立一个模本页面:template.htm

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  
###title###
作者:###author###
###content###

  

  

  =========================================

  再写一个jsp页面: buildhtml.jsp

  <%@ page contentType="text/html; charset=gb2312" import="Java.util.*,java.io.*"%>

  <%

try{

String title="jsp生成静态html文件";

String content="小样,还搞不定你?";

String editer="webjxcom";

String filePath = "";

filePath = request.getRealPath("/")+"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(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);//建立文件输出流

out.print("文件输出路径:<  try{

  String title="jsp生成静态html文件";

  String content="小样,还搞不定你?";

  String editer="webjxcom";

  String filePath = "";

  filePath = request.getRealPath("/")+"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(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);//建立文件输出流

  out.print("文件输出路径:

");>

  out.print(fileame);

  byte tag_bytes[] = templateContent.getBytes();

  fileoutputstream.write(tag_bytes);

  fileoutputstream.close();

  }

  catch(Exception e){

  out.print(e.toString());

  }

  %>

  ----------------------------------------------------------------------------------------------------

  对于其他几位前辈提出的方案,我也是受益良深,所以方案中,最简单的就是将JSP输出为HTML,最麻烦的是利用IO将输出信息组成HTML文件,最难的对我来说应该是生成XML的方案。我没有学XSL,但据说比较难,远不如HTML那么容易。尽管如此,生成XML这种方案却是我最欣赏的,原因有四:

  第一是实现了表示与数据的分离;

  第二是易于操作,增删改都相当方便;

  第三是跨平台特性让它应用领域更广;

  第四XML本身就可以当数据库使用,使得它可以合理组织数据。

  

  OK,基于这些原因,小弟在推敲中想到一个解决方案:如果我们在服务器生成若干XML文档,由统一的HTML装载,然后客户端执行HTML时,通过AJAX异步载入XML文档数据,那么结果是否会达到达人心动的效果呢?

  

  实现此技术需要解决的问题:

  1. 从数据库取数据动态生成并写入XML文件

  2. 利用URL传入XML文档名

  3. 客户端使用JS解析URL取XML文档名然后使用AJAX载入文件,最后动态组织数据到页面中

  

  以上分析是小弟一些拙见,高手勿笑!小弟在想到这个方案时,便马上写了一个Demo测试了一下,程序采用的是Strut框架写的,完成功能就是以上三点的描述,不过为了测试方便,并未使用数据库,而是自己定义了用于构造XML文件的类手动输入的,程序打包的下载地址:http://download.csdn.net/user/rodgersnow大家找到利用AJAX生成静态HTML的Demo就可以下载了

  

  

  Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1863395
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: