SoFunction
Updated on 2025-04-05

Code for generating static pages in Java projects

There are nothing more than the following factors:
1. From the perspective of page loading time: static pages do not need to establish connections with the database, especially pages with large amounts of data access. Most of these pages need to look for a lot of result sets, so the number of connections is increased, and the time is unpredictable, while static pages save this time.
2. From the perspective of being convenient for search engine crawling: search engines prefer static web pages. Compared with dynamic web pages, search engines prefer static web pages, which are easier to crawl, and search engines' SEO rankings are easier to improve. Most of the pages of some major portal websites use static or pseudo-static web pages to display, which is easier to crawl and ranking for search engines.
3. From a security perspective: Static web pages should not be attacked by hackers because hackers do not know the background of your website, the website's program, and the address of the database.
4. From the perspective of stability: one day the database server hangs up, the dynamic web page will be celebrated! To run a static web page publishing server, I believe everyone knows that the configuration is not too high, right? hehe.

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

So how do you generate static web pages with the code of dynamic web pages? Where does it exist? The principle is actually very simple.
1. Use Freemark templates to generate static pages. Search for a lot of codes online and choose as you like. I won’t talk about them here.
I hate this method very much because it is too much work for a page with a large amount of data. It has to write templates, and the syntax is quite weird and not popular!
2. It’s also what I think of occasionally. Using URLConnection in Java to grab a certain URL web page source code (this is the core of the principle) to generate an html file, it is that simple! That's it easy!

The code is presented!

1) The following is the program to capture web page source code:
Copy the codeThe code is as follows:

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

import ;
import ;

/**
* @author Xing,XiuDong
*/
public class HTMLGenerator {

    public static final String generate(final String url) {
        if ((url)) {
            return null;
        }

        Pattern pattern = ("(http://|https://){1}[\\w\\.\\-/:]+");
        Matcher matcher = (url);
        if (!()) {
            return null;
        }

        StringBuffer sb = new StringBuffer();

        try {
            URL _url = new URL(url);
            URLConnection urlConnection = _url.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(()));

            String inputLine;
            while ((inputLine = ()) != null) {
                (inputLine);
            }
        } catch (MalformedURLException e) {
            ();
        } catch (IOException e) {
            ();
        }

        return ();
    }

    /**
     * Test Code
     * Target : /
     */
    public static void main(String[] args) throws IOException {
        String src = ("/");

        File file = new File("C:" + + "");
        (file, src, "UTF-8");
    }

}

2) Write the source code to the Html file. This needs to be based on the user's needs. I wrote the following code based on the situation encountered in my project:
Copy the 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 = ().toString();
        }

        String contextPath = ();
        String seq = ((new Date().getTime()), -6);

        String ctxPath = ().getServletContext().getRealPath();
        if (!()) {
            ctxPath += ;
        }

        String filePath = (url, contextPath);
        filePath = ("\\.(do|jsp|html|shtml)$", ".html");

        String savePath = "";
        String autoCreatedDateDir = "";
        if (!toWebRoot) {
            savePath = (new String[] { "files", "history", "" }, );

            String[] folderPatterns = new String[] { "yyyy", "MM", "dd", "" };
            autoCreatedDateDir = (new Date(), (folderPatterns, ));

            filePath = (filePath, ".html") + "-" + seq + ".html";
        }

        File file = new File(ctxPath + savePath + autoCreatedDateDir + filePath);
        (file, (url), encoding);
    }

source:/xxd851116