Java projects generate static pages

Source: Internet
Author: User

The first time to do the project needs to generate static pages, many online Daniel on the page to generate static pages have many objections. Say what I think.

There are the following factors: 1, from the page load time: Static pages do not need to establish a connection with the database, especially the large amount of data access to the page, most of this page to look for a lot of result sets, so the number of connections increased, time is not significant, and static pages are omitted these times. 2, from the point of view to facilitate search engine crawling: Search engines prefer static pages, static pages and dynamic Web pages, search engines prefer static, more easy to crawl, SEO rankings are more easily improved, some of the big portal pages are mostly static or pseudo-static web pages to display, more convenient search engine crawl and ranking. 3, from the security point of view: Static Web pages should not be hacked, because hackers do not know your site backstage, the site Adoption program, database address. 4, from the stability point of view: Which day the database server hangs, Dynamic Web page goodbye! And to run a static web page publisher, I believe we all know that the configuration is not too high, right? Oh.

Therefore, I think it is feasible to generate static pages.

So how to generate static Web page code of Dynamic Web page? And where does it exist? The principle is actually very simple. 1, using Freemark template to generate static pages, search the Internet a lot of code with you pick, I am not here wordy. I hate this way, because a large amount of data on the page is too much work, to write templates, syntax is more strange, not popular! 2, also I occasionally think of. using Java urlconnection crawl a URL Web source (this is the core of the principle) to generate HTML files, it is so simple! That's how easy!.

The Code!

1), the following is the capture Web site source code program:

[Java]View Plaincopyprint?
  1. Import Java.io.BufferedReader;
  2. Import Java.io.File;
  3. Import java.io.IOException;
  4. Import Java.io.InputStreamReader;
  5. Import java.net.MalformedURLException;
  6. Import Java.net.URL;
  7. Import java.net.URLConnection;
  8. Import Java.util.regex.Matcher;
  9. Import Java.util.regex.Pattern;
  10. Import Org.apache.commons.io.FileUtils;
  11. Import Org.apache.commons.lang.StringUtils;
  12. /**
  13. * @author Xing,xiudong
  14. */
  15. Public class Htmlgenerator {
  16. public static final string Generate (final string url) {
  17. if (Stringutils.isblank (URL)) {
  18. return null;
  19. }
  20. Pattern pattern = Pattern.compile ("(http://|https://) {1}[//w//.//-/:]+");
  21. Matcher Matcher = pattern.matcher (URL);
  22. if (!matcher.find ()) {
  23. return null;
  24. }
  25. StringBuffer sb = new StringBuffer ();
  26. try {
  27. URL _url = new URL (URL);
  28. URLConnection urlconnection = _url.openconnection ();
  29. BufferedReader in = new BufferedReader (new InputStreamReader (Urlconnection.getinputstream ()));
  30. String Inputline;
  31. While ((Inputline = In.readline ()) = null) {
  32. Sb.append (Inputline);
  33. }
  34. } catch (Malformedurlexception e) {
  35. E.printstacktrace ();
  36. } catch (IOException e) {
  37. E.printstacktrace ();
  38. }
  39. return sb.tostring ();
  40. }
  41. /** 
  42. * Test Code
  43. * target:http://www.google.cn/
  44. */
  45. public static void Main (string[] args) throws IOException {
  46. String src = htmlgenerator.generate ("http://www.google.cn/");
  47. File File = new file ("C:" + file.separator + "index.html");
  48. Fileutils.writestringtofile (file, src, "UTF-8");
  49. }
  50. }

Import Java.io.bufferedreader;import java.io.file;import java.io.ioexception;import java.io.InputStreamReader; Import Java.net.malformedurlexception;import Java.net.url;import Java.net.urlconnection;import Java.util.regex.matcher;import Java.util.regex.pattern;import Org.apache.commons.io.fileutils;import org.apache.commons.lang.stringutils;/** * @author Xing,xiudong */public class Htmlgenerator {public static final String g Enerate (final String URL) {if (Stringutils.isblank (URL)) {return null;} Pattern pattern = Pattern.compile ("(http://|https://) {1}[//w//.//-/:]+"); Matcher Matcher = pattern.matcher (URL), if (!matcher.find ()) {return null;} StringBuffer sb = new StringBuffer (); try {url _url = new URL (URL); URLConnection urlconnection = _url.openconnection (); BufferedReader in = new BufferedReader (New InputStreamReader (Urlconnection.getinputstream ())); String inputline;while ((inputline = In.readline ()) = null) {sb.append (inputline);}} catch (Malformedurlexception e) {e.printstacktrace ();} CATCH (IOException e) {e.printstacktrace ();} return sb.tostring ();} /** * Test Code * target:http://www.google.cn/*/public static void Main (string[] args) throws IOException {String src = Htmlgenerator.generate ("http://www.google.cn/"); File File = new file ("C:" + file.separator + "index.html"); Fileutils.writestringtofile (file, src, "UTF-8");}}

2), the source code is written to the HTML file, which needs to be based on the needs of users, I based on the situation I encountered in the project wrote the following code: (with Test program: http://www.google.cn/)

[Java]View Plaincopyprint?
  1. /**
  2. * Generite HTML Source code
  3. *
  4. * @author Xing,xiudong
  5. * @date 2009.06.22
  6. * @param request
  7. * @param URL
  8. * @param towebroot
  9. * @param encoding
  10. * @throws IOException
  11. */
  12. Public void genhtml (httpservletrequest request, String URL, boolean towebroot, string encoding) throws Ioexce ption {
  13. if (null = = URL) {
  14. url = Request.getrequesturl (). toString ();
  15. }
  16. String ContextPath = Request.getcontextpath ();
  17. String seq = stringutils.substring (string.valueof (new Date (). GetTime ()),-6);
  18. String Ctxpath = super.getservlet (). Getservletcontext (). Getrealpath (File.separator);
  19. if (!ctxpath.endswith (File.separator)) {
  20. Ctxpath + = File.separator;
  21. }
  22. String FilePath = stringutils.substringafter (URL, contextpath);
  23. FilePath = Filepath.replaceall ("//. (  do|jsp|html|shtml) $ ", ". html ");
  24. String Savepath = "";
  25. String Autocreateddatedir = "";
  26. if (!towebroot) {
  27. Savepath = Stringutils.join (new string[] { "files", "History", ""}, File.separator);
  28. string[] Folderpatterns = new string[] { "yyyy", "MM", "DD", " "};
  29. Autocreateddatedir = Dateformatutils.format (new Date (), Stringutils.join (Folderpatterns, file.separator));
  30. FilePath = Stringutils.substringbefore (FilePath, ". html") + "-" + seq + ". html";
  31. }
  32. File File = new file (Ctxpath + savepath + autocreateddatedir + filePath);
  33. Fileutils.writestringtofile (file, htmlgenerator.generate (URL), encoding);
  34. }

Article Source: http://blog.csdn.net/xxd851116/archive/2009/06/24/4293239.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.