我的JSP留言本
 
初学JSP,写了简单的留言本。
 
一、效果展示
我的JSP留言本_职场
 
二、实现代码
 
1、建表
CREATE TABLE pinglun (                                                                
     id bigint(20) NOT NULL auto_increment,                            
     username varchar(24) NOT NULL,                                            
     content varchar(600) NOT NULL,                                            
     createtime datetime NOT NULL,                                            
     PRIMARY KEY    (id)                                                                    
) ENGINE=MyISAM DEFAULT CHARSET=gbk    
 
2、写页面
 
<%@ page language="java" import="java.util.*" import="java.sql.*" pageEncoding="GB18030"%>
<%@ page import="java.sql.Date" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <base href="<%=basePath%>">

        <title>我的留言本</title>

  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
  <!--
  <link rel="stylesheet" type="text/css" href="styles.css">
  -->

    </head>

    <body>
        我的留言本<br>
        <pre>
                观沧海

      东临碣石,以观沧海。
      水何澹澹,山岛竦峙。
      树木丛生,百草丰茂。
      秋风萧瑟,洪波涌起。
      日月之行,若出其中;
      星汉灿烂,若出其里。
      幸甚至哉,歌以咏志。
        </pre>
        文章评论:<br><br>
        <%
        request.setCharacterEncoding("GB18030");
        String username = request.getParameter("username");
        String pinglun = request.getParameter("pinglun");

  String dburl = "jdbc:mysql://localhost:3306/testdb";
  String user = "root";
  String password = "xiaohui";

  //1:注册驱动类
  Class.forName("com.mysql.jdbc.Driver");
  //2:创建数据库连接
  Connection conn = DriverManager.getConnection(dburl, user, password);
  //3:创建执行SQL的对象
    PreparedStatement pstmt = conn.prepareStatement("insert into pinglun(username,content,createtime) values(?,?,?)");    
  //4:执行SQL,并获取返回结果
  if(username != null){
    pstmt.setString(1,username);
    pstmt.setString(2,pinglun);
    pstmt.setDate(3,new Date(System.currentTimeMillis()));
    pstmt.executeUpdate();
  }

  Statement stmt = conn.createStatement();
  ResultSet rs = stmt.executeQuery("select id,username,content,createtime from pinglun order by id asc;");
  //5:处理返回结果,此处打印查询结果
        while(rs.next()){
         out.println(rs.getString("username")+"        \t\t"+rs.getDate("createtime"));
         out.println("<hr>");
         out.println(rs.getString("content")+"<br><br><br>");
        }
        conn.close();
     %>

        <table>
        <form action="lyb.jsp" method="post">
          <tr>
            <td>用户:</td>
            <td><input type="text" name="username"></td>
          </tr>
          <tr>
            <td>评论:</td>
            <td><textarea name="pinglun" rows="5" cols="50"></textarea></td>
          </tr>
          <tr>
            <td><input type="submit" value="提交"></td>
            <td><input type="reset" value="重置"></td>
          </tr>
        </form>
        </table>
    </body>
</html>