JSP static file generation
Last Update:2018-12-05
Source: Internet
Author: User
JSP directly generates HTML files
The Touch version is defined as follows:
News. Template
<HTML>
<Head>
<Title> untitled document </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Link href = "../css.css" rel = stylesheet type = text/CSS>
</Head>
<Body>
<Table width = "500" border = "0" align = "center" cellpadding = "0" cellspacing = "2">
<Tr>
<TD align = "center"> <$ title $> </TD>
</Tr>
<Tr>
<TD align = "center"> author: <$ author $> & nbsp; Release Date: <font color = # ff0000> <$ date $> </font> </TD>
</Tr>
<Tr>
<TD> <$ content $>
</TD>
</Tr>
</Table>
</Body>
</Html>
A class that generates HTML:
Import java. Io .*;
Public class writehtml
{
Public writehtml ()
{
}
Public static void save (string S, string S1, string S2)
Throws writefileexception
{
Try
{
A (S1 );
Fileoutputstream = new fileoutputstream (S1 S2 );
Byte abyte0 [] = S. getbytes ();
Fileoutputstream. Write (abyte0 );
Fileoutputstream. Close ();
}
Catch (ioexception)
{
Throw new writefileexception ();
}
}
Private Static void a (string S)
{
File file = new file (s );
If (! File. exists ())
File. mkdirs ();
}
}
A class for reading touch versions:
Import java. Io .*;
Public class readtemplates
{
Private Static string _ fldif = NULL;
Private Static object A = new object ();
Public readtemplates ()
{
}
Public static string gettlpcontent (string S)
Throws readtemplateexception
{
If (_ fldif = NULL)
Synchronized ()
{
If (_ fldif = NULL)
Try
{
System. Out. println ("");
_ Fldif = a (s );
}
Catch (readtemplateexception)
{
Throw new readtemplateexception ("failed to read template information. ");
}
}
Return _ fldif;
}
Private Static synchronized string a (string S)
Throws readtemplateexception
{
String S1 = NULL;
Try
{
Fileinputstream = new fileinputstream (s );
Int I = fileinputstream. Available ();
Byte abyte0 [] = new byte [I];
Fileinputstream. Read (abyte0 );
Fileinputstream. Close ();
S1 = new string (abyte0 );
}
Catch (ioexception)
{
Throw new readtemplateexception ();
}
Return S1;
}
}
JSP file:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Include file = "/Inc. jsp" %>
<%
String [] flag = {"<$ title $>", "<$ date $>", "<$ author $>", "<$ content $> "};
String title = request. getparameter ("title ");
String content = request. getparameter ("content ");
String editer = "admin ";
// Session. getattribute ("s_username ");
Int classid = integer. parseint (request. getparameter ("class "));
String filepath = "";
Filepath = application. getrealpath ("./adminroot/news. template ");
String templatecontent;
Try {
Templatecontent = readtemplates. gettlpcontent (filepath );
} Catch (readtemplateexception e ){
Throw new exception ("failed to read template information. Contact the system administrator. ");
}
Templatecontent = replaceall. Replace (templatecontent, flag [0], title );
Templatecontent = replaceall. Replace (templatecontent, flag [1], getdate. getstringdate ());
Templatecontent = replaceall. Replace (templatecontent, flag [2], editer );
Templatecontent = replaceall. Replace (templatecontent, flag [3], content );
// Obtain the file name and path name based on the time
Calendar calendar = calendar. getinstance ();
String filename = string. valueof (calendar. gettimeinmillis () ". shtml ";
String pathname = application. getrealpath ("./News") "//" calendar. Get (calendar. Year)
"//" (Calendar. Get (calendar. month) 1) "//" calendar. Get (calendar. day_of_month )"//";
Try {
Writehtml. Save (templatecontent, pathname, filename );
} Catch (writefileexception we ){
Throw new exception ("operation failed! ");
} %>