Code for generating static pages for Java Projects

Source: Internet
Author: User

Similar to the following factors:
1. From the page loading time point of view: static pages do not need to be connected to the database, especially when accessing pages with large data volumes. Most of these pages need to query many result sets, therefore, the number of connections is increased, and the time is not considerable, while the static page saves the time.
2. Search engines prefer static Web pages. Compared with dynamic web pages, search engines prefer static web pages to facilitate crawling, the SEO ranking of search engines is easier to improve. Most of the major portal pages are displayed on static or pseudo-static pages, making it easier for search engines to capture and rank.
3. From the security perspective: static Web pages should not be attacked by hackers because they do not know the background of your website, the website uses programs, and the database address.
4. From the perspective of stability: When the database server crashes, dynamic web pages will be okay! To run a static Web page Publishing Server, I believe everyone knows that the configuration is not too high? Haha.

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

So how can we generate static Web Pages Based on Dynamic Web Page code? What else does it exist? The principle is actually very simple.
1. Use the Freemark template to generate a static page. Search for a large amount of code on the Internet as you like, so I will not be here.
I hate this method very much, because for a page with a large amount of data, the workload is too large. To write templates, the syntax is weird and not popular!
2. I think about it occasionally. Using URLConnection in Java to capture the source code of a URL webpage (this is the core of the principle) to generate an html file, which is so simple! That's Easy!

Code!

1) The following is the source code program for capturing web pages:
Copy codeThe Code is 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) write the source code to an Html file. The following code is required based on your needs:
Copy codeThe Code is 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

Related Article

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.