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 hackers do not know the background and website usage of your website. Program The address of the database.
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 change the dynamic web pageCodeWhat about generating static webpages? 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 = pattern. matcher (URL );
If (! Matcher. Find ()){
Return NULL;
}

Stringbuffer sb = new stringbuffer ();

Try {
URL _ url = new URL (URL );
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.