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