Java project generates static page code _JSP programming

Source: Internet
Author: User
Tags stringbuffer
There are the following factors:
1, from the page load time to see: Static pages do not need to establish a connection with the database, especially to access the larger number of pages, this page is mostly to check a lot of result sets, so the number of connections to establish more, time is not considerable, while static pages save these times.
2, from the perspective of easy search engine crawling: Search engines prefer static Web pages, static Web pages and dynamic pages, compared to search engines prefer static, easier to crawl, search engine SEO rankings easier to improve, some large portal pages are mostly static or pseudo static page to display, more convenient to crawl and rankings.
3, from the security point of view: Static Web pages should not be attacked by hackers, because hackers do not know the background of your site, the site using the program, the address of the database.
4, from the stability of: which day the database server hangs, Dynamic Web page will be bye-bye! And to run a static web page of the publisher, I believe we all know that the configuration is not too high also OK? Oh.

Therefore, I believe that the generation of static pages is feasible.

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

Code to the bong!

1), the following is the capture page source code program:
Copy Code code as follows:

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 generate (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 written to the HTML file, this need according to the needs of users, I encountered in the project according to the situation I wrote the following:
Copy Code code as follows:

/**
* Generite HTML Source code
*
* @author Xing,xiudong
* @date 2009.06.22
* @param request
* @param URL
* @param towebroot
* @param encoding
* @throws IOException
*/
public void genhtml (httpservletrequest request, String URL, Boolean towebroot, String encoding) throws IOException {

if (null = = URL) {
url = Request.getrequesturl (). toString ();
}

String ContextPath = Request.getcontextpath ();
String seq = stringutils.substring (string.valueof (New Date (). GetTime ()),-6);

String Ctxpath = Super.getservlet (). Getservletcontext (). Getrealpath (File.separator);
if (!ctxpath.endswith (File.separator)) {
Ctxpath + = File.separator;
}

String FilePath = stringutils.substringafter (URL, contextpath);
FilePath = Filepath.replaceall ("\ \ do|jsp|html|shtml) $ ",". html ");

String Savepath = "";
String autocreateddatedir = "";
if (!towebroot) {
Savepath = Stringutils.join (new string[] {"Files", "History", ""}, File.separator);

string[] Folderpatterns = new string[] {"yyyy", "MM", "DD", ""};
Autocreateddatedir = Dateformatutils.format (New Date (), Stringutils.join (Folderpatterns, file.separator));

FilePath = Stringutils.substringbefore (FilePath, ". html") + "-" + seq + ". html";
}

File File = new file (Ctxpath + savepath + autocreateddatedir + filePath);
Fileutils.writestringtofile (file, htmlgenerator.generate (URL), encoding);
}

Source: http://blog.csdn.net/xxd851116

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.