Example: Automatically generate HTML pages that can save data

xiaoxiao2021-03-06  115

In fact, this is a very simple technology that is very common in the news website. The principle is to save the data as a hard disk file, directly through the URL, used to alleviate the pressure of database access. Databases and tables:

Mysql> USE Mysecondnews; Database ChangeDmySql> Desc News; -------- ------------- ---- ----- ------- ---------------- | Field | Type | Null | Key | Default | Extra | -------- - -------------- ------ --- -------- --------- ------- | ID | INT (10) | | Pri | Null | Auto_INCREMENT || Title | VARCHAR (240) | | | | || Path | VARCHAR (240) | | | || Text | | | | | | Addtime | TimeStamp (14) | YES | | Null | | -------- -------------- --- - ----- --------- ----------------

Database.java: / / Define Database

Package org.eleaf.firstnews.Database;

import java.sql.ResultSet; public interface Database {String DATABASE = "mysecondnews"; String USERNAME = "bitan"; String PASSWORD = "bitan"; String URL_PREFIX = "jdbc: mysql: // localhost /" DATABASE; String DRIVER = "Org.gjt.mm.mysql.driver";

INT Doupdate (String SQL); ResultSet Doselect (String SQL);

TouchDatabase.java: / / Customize the JDBC interface class. For simplicity, the Close () method is not processed.

package org.eleaf.firstnews.database; import java.sql *;. public class TouchDatabase implements Database {private Connection conn; private Statement stmt; private PreparedStatement prepstmt; private void init () throws SQLException, ClassNotFoundException {stmt = null; prepstmt = null; Class.forName (DRIVER); conn = DriverManager.getConnection (uRL_PREFIX, USERNAME, PASSWORD);} / ** * Constructor for Statement * * / public TouchDatabase () {try {this.init ();. stmt = conn .createStatement ();} catch (SQLException sqle) {sqle.printStackTrace ();} catch (ClassNotFoundException cnfe) {cnfe.printStackTrace ();.}} / ** * Constructor for PreparedStatement * @param sql Pre-compile SQL string . * / Public TouchDatabase (String SQL) {Try {this.init (); prepstmt = conn.prepar eStatement (sql);} catch (SQLException sqle) {sqle.printStackTrace ();} catch (ClassNotFoundException cnfe) {cnfe.printStackTrace ();}} public int doUpdate (String sql) {// TODO method of automatically generating stubs try { return stmt.executeUpdate (sql);} catch (SQLException sqle) {sqle.printStackTrace (); return 0;}} public ResultSet doSelect (String sql) {// TODO method of automatically generating stubs try {return stmt.executeQuery (sql) } Catch (sqlexception sqle) {sqle.printStackTrace (); return null;} catch (exception e) {E.PrintStackTrace (); return null;

}} Public void setField (int index, String field) {try {prepstmt.setString (index, field);} catch (SQLException sqle) {sqle.printStackTrace ();}} public ResultSet doPrepareSelect () {try {return prepstmt. ExecuteQuery ();} catch (sqlexception sqle) {sqle.printStackTrace (); return null;} catch (exception e) {E.PrintStackTrace (); return null;}} }.com.java: / / News structure Package testnews; public class news {private string id; private string title; private string content; / ** * @return Returns AddTime. * / Public string getaddtime () {return addtime;} / ** * @Param addtime To set the Addtime. * / Public void setaddtime (string addtime) {this.addtime = addtime;} / ** * @return returns Content. * / Public string getContent () {return content;} / ** * @Param Content To set the content. * / Public void setContent (string content) {this.content = content;} / ** * @return Returns ID. * / Public string getId () {return;} / ** * @Param ID To set the ID. * / Public void setid (string id) {this.id = id;} / ** * @return returns PATH. * / Public string getpath () {return path;} / ** * @Param Path To set the path. * / Public void setpath (string path) {this.path = path;} / ** * @return Returns Title. * / Public string gettitle () {return title;} / ** * @Param Title To set the Title. * / Public void settitle (string title) {this.title = title;}}

Donnews.java: // Take the Database for the JDBC Interface Class;

import java.util *;. import java.sql *;. import org.eleaf.firstnews.database *;. public class DoneNews {public Vector getAllNews () {Vector vNews = new Vector (); String sql = "SELECT * FROM News "; TouchDatabase Tdata = new TouchDatabase (); ResultSet RS = TDATA.DOSET RS = TDATA.DOSET RS = TDATA.DOSET RS = TDATA.DOSET (SQL); Try {While (rs.next ()) {news news = new news (); news.setid (" id) ")); News.settitle (RS.GetString (" Title ")); news.setpath (rs.getstring (" path ")); news.setcontent (Rs.getstring (" Content ")); news.setaddtime rs.getString ( "addtime")); vNews.addElement (news);}} catch (SQLException sqle) {sqle.printStackTrace ();} return vNews;} public boolean addNews (News news) {boolean success = false; if (news! = null) {string SQL = "Insert News (Title, Path, Content) VALUES ('" news.gettitle () "','" news.getPath () "" "" "" "; touchdatabase tdata = new touchDatabase (); success = (0! = tdata.doupdate (SQL) }}}}}}}}} WriteNews.java: // Write data flows to the hard disk. And call the donnews.addnews () method. Package testnews;

Import java.io. *; import java.util. *; public class whitNews {private factory string middle_dir = ""; // can be customized to the directory. Private final string file_suffix = ".html"; // can customize the file suffix name. public boolean write (News news, String realPathPrefix, Calendar calendar) {int year = calendar.get (Calendar.YEAR); int month = calendar.get (Calendar.MONTH) 1; int dayOfMonth = calendar.get (Calendar.DAY_OF_MONTH ); String fileName = calendar.getTimeInMillis () FILE_SUFFIX; String realPath = realPathPrefix MIDDLE_DIR year "//" month "//" dayOfMonth "//"; try {if (mkDirs (realPath!) ) {// throw new IOException ( "make dir ( '" realPath "') unsuccessfully!");} FileOutputStream fos = new FileOutputStream (new File (realPath fileName)); byte [] conBytes = news.getContent ( ); fos.write (); fos.close ();} catch (ooexception ooe) {oE.PrintStackTrace (); returnaf false;} String newspath = year "/" month "/" Dayofmonth "/" filename; news.setpath (newspath); Return new donnews (). AddNews (news); String realpath = false; file path = new file (realpath); if (! Path.exists ()) {success = path.mkdirs );}} Form.jsp: // Enter a form

<% @ page language = "java"%> Lomboz JSP </ Title> </ head > <body bgcolor = "# ffffff"> <form method = post action = "save.jsp"> <input type = "text" name = "title"> <br> <textarea name = "limited" rows = "10 "COLS =" 50 "> </ textarea> <br> <input type =" submit> </ form> </ body> </ html> save.jsp: // Save data. For simplicity, here is not Enter data to make any character filtering or conversion.</p> <p><% @ page language = "java"%> <% @ page import = "java.util. *, testnews. *"%> <! doctype html public "- // w3c // DTD HTML 4.0 Transitional // en" > <html> <head> <title> Lomboz JSP </ Title> </ head> <body bgcolor = "# fffff"> <% string title = request.getParameter ("title"); string content = request.getParameter "Content"); if (title == null | content == null) {response.sendredirect ("form.jsp"); return; news news = new news (); news.settitle; news.setContent (content); String realPathPrefix = application.getRealPath ( "/"); Calendar calendar = Calendar.getInstance (); boolean success = new WriteNews () write (news, realPathPrefix, calendar);. String message = ""; if ( {Message = "{Message =";} else {message = "; .jsp "> return to form.jsp </a> <br> <a href="show.jsp"> Go to show.jsp </a> <br> </ body> </ html> show.jsp: // Display link</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-98137.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="98137" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.038</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'zTFIr6eJK9EA5hDITnb2XOVZ5jAgGhayBSyzwfvmCKhcJ1qoC9dqCIb89K_2BgS3zkPO9_2FcdqkarGGnqXnNj8xQA_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>