Jsp dynamic generation of static pages

Source: Internet
Author: User

 

I learned a little about jsp dynamic generation of static pages.

 

 

Java code

/**

* @ Author jeedroid

* @ Time 2011-11-24 00:47:52

*/

Package com. jeedroid. makehtml;

Import java. io .*;

Import java.net. MalformedURLException;

Import java.net. URL;

Import java.net. URLConnection;

Import java.net. URLEncoder;

Public class MakeHtml

{

Public static void makeHtml (String url)

{

Try {

// Read the content of the object to be accessed

URL _ url = new URL (url );

URLConnection conn = _ url. openConnection ();

InputStream inputStream = conn. getInputStream ();

BufferedReader buffer = new BufferedReader (new InputStreamReader (inputStream ));

StringBuilder stringBuilder = new StringBuilder ();

String line = null;

While (line = buffer. readLine ())! = Null)

{

StringBuilder. append (line );

}

String fileContent = stringBuilder. toString ();

InputStream. close ();

String htmlFileName = generateFileName (url );

File htmlFile = new File ("DHtml/", htmlFileName );

If (! HtmlFile. exists ())

{

HtmlFile. createNewFile ();

// Write fileContent to an html file

FileWriter fileWriter = new FileWriter (htmlFile, true );

BufferedWriter bufferedWriter = new BufferedWriter (fileWriter );

BufferedWriter. write (fileContent );

BufferedWriter. close ();

FileWriter. close ();

}

} Catch (MalformedURLException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

} Catch (IOException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

}

}

 

// Generate html filename according to request url

Public static String generateFileName (String url)

{

String preFileName = url. substring (url. lastIndexOf ("/"));

// Set "?" In the Request Parameters "? "Convert or the file name cannot contain question marks.

String fileName = preFileName. replace ("? ", URLEncoder. encode ("? "));

Return fileName. replace (".", "_") + ". html ";

}

 

}

 

/**

* @ Author jeedroid

* @ Time 2011-11-24 00:47:52

*/

Package com. jeedroid. makehtml;

Import java. io .*;

Import java.net. MalformedURLException;

Import java.net. URL;

Import java.net. URLConnection;

Import java.net. URLEncoder;

Public class MakeHtml

{

Public static void makeHtml (String url)

{

Try {

// Read the content of the object to be accessed

URL _ url = new URL (url );

URLConnection conn = _ url. openConnection ();

InputStream inputStream = conn. getInputStream ();

BufferedReader buffer = new BufferedReader (new InputStreamReader (inputStream ));

StringBuilder stringBuilder = new StringBuilder ();

String line = null;

While (line = buffer. readLine ())! = Null)

{

StringBuilder. append (line );

}

String fileContent = stringBuilder. toString ();

InputStream. close ();

String htmlFileName = generateFileName (url );

File htmlFile = new File ("DHtml/", htmlFileName );

If (! HtmlFile. exists ())

{

HtmlFile. createNewFile ();

// Write fileContent to an html file

FileWriter fileWriter = new FileWriter (htmlFile, true );

BufferedWriter bufferedWriter = new BufferedWriter (fileWriter );

BufferedWriter. write (fileContent );

BufferedWriter. close ();

FileWriter. close ();

}

} Catch (MalformedURLException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

} Catch (IOException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

}

}

 

// Generate html filename according to request url

Public static String generateFileName (String url)

{

String preFileName = url. substring (url. lastIndexOf ("/"));

// Set "?" In the Request Parameters "? "Convert or the file name cannot contain question marks.

String fileName = preFileName. replace ("? ", URLEncoder. encode ("? "));

Return fileName. replace (".", "_") + ". html ";

}

 

}

Then I used junit for the next test.

 

 

Java code

Package com. jeedroid. makehtml;

 

Import static org. junit. Assert .*;

 

Import org. junit. Test;

 

Public class MakeHtmlTest

{

 

@ Test

Public void testMakeHtml ()

{

MakeHtml. makeHtml ("http: // localhost: 8080/DynamicToHtml/index. jsp ");

}

 

}

 

Package com. jeedroid. makehtml;

 

Import static org. junit. Assert .*;

 

Import org. junit. Test;

 

Public class MakeHtmlTest

{

 

@ Test

Public void testMakeHtml ()

{

MakeHtml. makeHtml ("http: // localhost: 8080/DynamicToHtml/index. jsp ");

}

 

}

Not bad. You only need to use urlrewriter to directly generate the index when accessing the static page. jsp requests are directed to the servlet, and then the above javabean is called, and the html page is switched. As long as the url is the same, the html will be displayed directly. Haha

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.