High performance Java Web page static Technology (original) __java

Source: Internet
Author: User
Package com.yancms.util;
Import java.io.*;
Import org.apache.commons.httpclient.*;
Import org.apache.commons.httpclient.methods.*;

Import Org.apache.commons.httpclient.params.HttpMethodParams; /** * Static page engine technology (chaotic garbled problem UTF-8) * @author Wu Yanwen * * */public class Htmlgenerator extends Baselog {httpclient httpclient = Null HttpClient instance GetMethod GetMethod =null;
	GetMethod instance BufferedWriter fw = null;
	String page = null;
	String webappname = null;
	BufferedReader br = null;
	InputStream in = null;
	StringBuffer SB = null; 
	String line = null;
		
	Construction method Public Htmlgenerator (String webappname) {this.webappname = Webappname;	
		/** a static page based on template and Parameters */public boolean createhtmlpage (String url,string htmlfilename) {Boolean status = FALSE;				
		int statusCode = 0;
			try{//Create a HttpClient instance to act as an analog browser httpclient = new HttpClient ();			
			Sets the character set to use when HttpClient reads Content httpclient.getparams (). Setparameter (Httpmethodparams.http_content_charset, "UTF-8"); Create an instance of a Get method GetMethod= new GetMethod (URL); With the default recovery policy provided by the system, 3 times Getmethod.getparams () is automatically retried when an exception occurs. Setparameter (Httpmethodparams.retry_handler, new
			Defaulthttpmethodretryhandler ());
			Sets the character set used to support the normal transfer of Chinese parameters ("Content-type", "Text/html;charset=utf-8") when the Get method submits the parameter Getmethod.addrequestheader.			
			Executes the Get method and obtains the return status code, 200 is normal, the other code is abnormal StatusCode = Httpclient.executemethod (GetMethod); if (statuscode!=200) {logger.fatal (static page engine fails to parse "+url+" to produce a static page "+htmlfilename+");}
				else{//Read parse result sb = new StringBuffer ();
				in = Getmethod.getresponsebodyasstream (); br = new BufferedReader (new InputStreamReader (in));//This method defaults to garbled, after a long period of groping, the following method can BR = new BufferedReader (New Inputstr
				Eamreader (In, "UTF-8"));
				while ((Line=br.readline ())!=null) {sb.append (line+ "\ n");
				} if (Br!=null) br.close ();
				page = Sb.tostring ();
				Replace the relative path in the page with an absolute path to ensure that the page resource is normally accessed by the pages = Formatpage (page);
				Writes the parsing result to the specified static HTML file to implement the static HTML generation writehtml (Htmlfilename,page);
			Status = TRUE; }}catCH (Exception ex) {logger.fatal (Static page engine failed while parsing "+url+" to produce a static page "+htmlfilename+": "+ex.getmessage ()");
        }finally{//Free HTTP connection getmethod.releaseconnection ();
	} return status;  ///Writes The parsing result to the specified static HTML file private synchronized void writehtml (String htmlfilename,string content) throws exception{FW
		= new BufferedWriter (new FileWriter (Htmlfilename));
		OutputStreamWriter fw = new OutputStreamWriter (new FileOutputStream (Htmlfilename), "UTF-8");	
		Fw.write (page);		
	if (fw!=null) fw.close (); //Replace the relative path in the page with an absolute path to ensure that the page resource is normally accessed by the private string Formatpage (String page) {page = Page.replaceall ("\\.\\./\\.\\./\\.
		\\./", webappname+"/");
		page = Page.replaceall ("\\.\\./\\.\\./", webappname+ "/");			
		page = Page.replaceall ("\\.\\./", webappname+ "/");
	return page;
		}//test method public static void main (string[] args) {htmlgenerator h = new Htmlgenerator ("Webappname"); H.createhtmlpage ("Http://localhost:8080/yanCms/three/three?parent_id=10&id=103&type=10 "," c:/a.html ");
		
	SYSTEM.OUT.PRINTLN ("Static page has been generated to c:/a.html"); }

}

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.