Generating static pages with JSP


Please refer to the main principle

After a turn of debugging, it succeeded.. Oh

Attach the source code..

JDK 1.5 +ECLIPSE +TOMCAT 5.0.28 + MYSQL 5.0 

Database test, table name news  
Field: ID     int   Automatic growth,   Title    varchar(20)  ,  Content    varchar(200)   ,  Author    varchar(10)  


  Connection conn = DBconn.getConnection();
  Statement stmt = conn.createStatement();
  ResultSet Rs = stmt.executeQuery(“select * from news”);



  String filePath = request.getRealPath(“/”)+”template.htm”;


  String templateContent;
  FileInputStream fileinputstream = new FileInputStream(filePath);
   int   lenght  =  fileinputstream.available();  // available()   Returns the number of bytes that can be read from this file input stream without blocking.

byte bytes[] = new byte[lenght];;  // read(byte[]   b)   The maximum number of entries from this input stream   b.length   Bytes of data are read into a byte array.

//templateContent = new String(bytes);
String title;
String content;
String author;
templateContent  =  new   String(bytes);// If this sentence is not used, there will be no #* * # flag in the templatecontent after replacement. So we need to regenerate
 title = Rs.getString(“Title”);
 content = Rs.getString(“Content”);
 author = Rs.getString(“Author”);
Out. Print (“the following is the template content: < br >” + templatecontent + “< br >   The following is the HTML content after replacement (< br > < HR > “);
templateContent=templateContent.replaceAll(“#author#”,author);// Replace the corresponding place in the module

//   File name by time
Calendar calendar = Calendar.getInstance();
String fileame = String.valueOf(calendar.getTimeInMillis()) +”.html”;
fileame  =  request.getRealPath(“/”)+”Html/”+fileame;// Save path of generated HTML file

FileOutputStream   fileoutputstream  =  new   FileOutputStream(fileame);// Create file output stream
byte tag_bytes[] = templateContent.getBytes();




//Database connection file

import java.sql.*;
public class DBconn {

    public DBconn() {

        // TODO Auto-generated constructor stub

    public static Connection getConnection() 
        Connection conn = null;

        try { 
            conn = DriverManager.getConnection(“jdbc:mysql://” + “localhost” + “/” + “test” +

        catch(Exception e)
        return conn;
    /*public static void main(String[] args) throws Exception
        Connection con=getConnection();


//Template file

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” ““>
<html xmlns=”“>
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″ />

<table width=”380″ height=”107″ border=”0″ cellpadding=”0″ cellspacing=”1″ bgcolor=”#FFCC99″>
    <td height=”16″ bgcolor=”#FFCC99″><div align=”center”>#title#</div></td>
    <td bgcolor=”#FFFFFF”>#content#</td>
    <td height=”13″ align=”right” bgcolor=”#FFFFFF”>#author#</td>

Recommended Today

The 50 MySQL interview questions, Ali’s interviewer called the expert (Part 1)

preface As for the so-called golden, silver and four, friends who want to change jobs to big factories should also be actively preparing for this period of time. Mysql, as one of the most popular relational database management systems, probably does not need me to say more about its importance. It is almost necessary to […]