JSP generate static news page (2)

Source: Internet
Author: User

Template page modle.html
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> untitled document </title>
</Head>
<Table width = "200" border = "1">
<Tr>
<TD> title: ### title ### </TD>
</Tr>
<Tr>
<TD> Source: ### source ### </TD>
</Tr>
<Tr>
<TD> Release Date: ### addtime ### </TD>
</Tr>
<Tr>
<TD> Article: ### Article ### </TD>
</Tr>

</Table>

<Body>
</Body>
</Html>

Addnews. jsp (the encoding here is gb2312)
<% @ Page Language = "Java" contenttype = "text/html; charset = gb2312" errorpage = "" %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Title> my JSP & apos; addnews. jsp & apos; starting page </title>
</Head>
<Body>
<Form action = "add_do.jsp" method = "Post">
<Table>
<Tr>
<TD> title: </TD>
<TD> <input type = "text" name = "title"/> </TD>
</Tr>
<Tr>
<TD> Source: </TD>
<TD> <input type = "text" name = "Source"/> </TD>
</Tr>
<Tr>
<TD> content: </TD>
<TD> <textarea rows = "10" Cols = "40" name = "article"> </textarea> </TD>
</Tr>
<Tr>
<TD> </TD>
<TD> <input type = "Submit" value = "Submit"/> <input type = "reset" value = "cancel"/> </TD>
</Tr>
</Table>
</Body>
</Html>

Generate static news page add_do.jsp
<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8" errorpage = "" %>
<% @ Page import = "Java. util. *, java. Io. *, java. Text. *" %>

 

<%
String title = new string (request. getparameter ("title"). getbytes ("iso-8859-1"), "gb2312 ");
// System. Out. Print (title );
String source = new string (request. getparameter ("Source"). getbytes ("iso-8859-1"), "gb2312 ");
String article = new string (request. getparameter ("article"). getbytes ("iso-8859-1"), "gb2312 ");
Simpledateformat format = new simpledateformat ("yyyy-mm-dd ");
String nowtime = format. Format (new date ());
String fileame = "";
Int A = 0;
Try {
String filepath = "", Path = "";
Filepath = request. getrealpath ("/") + "modle.html ";
Filepath = filepath. replaceall ("////","/");
String templatecontent = "";
 
// Read the hmtl Template File
Fileinputstream FCM = new fileinputstream (filepath );
Stringbuffer content = new stringbuffer ();
Datainputstream in = new datainputstream (FCM );
Bufferedreader BR = new bufferedreader (New inputstreamreader (in, "UTF-8 "));
String line = NULL;
While (line = Br. Readline ())! = NULL)
Content. append (LINE + "/N ");
BR. Close ();
In. Close ();
FCM. Close ();
 
// Replace the relevant part of the template
Templatecontent = new string (content );
Templatecontent = templatecontent. replaceall ("### title ###", title );
Templatecontent = templatecontent. replaceall ("### source ###", source );
Templatecontent = templatecontent. replaceall ("### addtime ###", nowtime );
Templatecontent = templatecontent. replaceall ("### Article ###", article); // replace the corresponding content in the module
System. Out. println (templatecontent );
Calendar calendar = calendar. getinstance ();
Fileame = string. valueof (calendar. gettimeinmillis () + ". html ";
 
// Create a news page storage directory
Path = request. getrealpath ("/") + nowtime + "/";
File d = new file (PATH); // create a file object that represents the sub directory and obtain a reference
If (! D. exists () {// check whether the sub directory exists
D. mkdir (); // create a sub directory
}
File F = new file (path, fileame );
If (! F. exists () {// check whether file.txt exists
F. createnewfile (); // create a file named file.txt in the current directory.
}
Fileame = request. getrealpath ("/") + nowtime + "/" + fileame; // save path of the generated HTML file

// Write the replaced content to the file
Fileoutputstream Fos = new fileoutputstream (fileame );
Writer output = new outputstreamwriter (FOS, "UTF-8 ");
Output. Write (templatecontent );
Output. Close ();
FOS. Close ();
Out. println ("<SCRIPT> window. alert (& apos; static news page generated successfully & apos;); window. location. href = & apos; addnews. JSP & apos; </SCRIPT> ");
 
} Catch (exception e ){
System. Out. Print (E. tostring ());
}
%>
Generate the final result file 2008-09-11/1247651927156. html
2. The database saves the file title, file name, date, and other information at the same time, and there is no need to retain other information such as news content;
3rd. You can directly retrieve the xxx.htm file name and file title from the data warehouse;

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.