初学JSP制作的论坛

        一个简单的练习,没有做什么前台,用到了递归,参数传递,数据库操作,session以及字符编码转换。

         其中有两个主要问题费了一些时间:

        1.ResultSet对象的getRow()方法是返回指针指向的行,next()可以移动到下一行

        2.字符转换的问题可以用 new String(Str.getBytes("ISO-8859-1")) ,有时候request.setCharacterEncoding("GB2312")转换的时候不是很有效。还是太菜,有待考证。

 

 

  1. --bbs.sql
  2. --创建数据库
  3. create database bbs;
  4. use bbs;
  5. --创建表
  6. --使用
  7. create table article(
  8. id int primary key auto_increment,
  9. --父节点id
  10. pid int,
  11. --根结点id
  12. rootid int,
  13. title varchar(255),
  14. cont text,
  15. pdate datetime,
  16. --是否是叶子节点
  17. isleaf int
  18. );
  19. --0 代表leaf ,1代表非leaf
  20. insert into article values (null,0,1,'蚂蚁大战大象','蚂蚁大战大象',now(),1);
  21. insert into article values (null,1,1,'大象被打趴下了','大象被打趴下了',now(),1);
  22. insert into article values (null,2,1,'蚂蚁也不好过','蚂蚁也不好过',now(),0);
  23. insert into article values (null,2,1,'瞎说','瞎说',now(),1);
  24. insert into article values (null,4,1,'没有瞎说','没有瞎说',now(),0);
  25. insert into article values (null,1,1,'怎么可能','怎么可能',now(),1);
  26. insert into article values (null,6,1,'怎么没有可能','怎么没有可能',now(),0);
  27. insert into article values (null,6,1,'可能性是很大的','可能性是很大的',now(),0);
  28. insert into article values (null,2,1,'大象进医院了','大象进医院了',now(),1);
  29. insert into article values (null,9,1,'护士是蚂蚁','护士是蚂蚁',now(),0);

 

  1. //ShowArticleTree.jsp
  2. <%@ page language="java" contentType="text/html;charset=gb2312" pageEncoding="gb2312"%>
  3. <%@ page import="java.sql.*" %>
  4. <%
  5. String path = request.getContextPath();
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  7. Class.forName("com.mysql.jdbc.Driver");
  8. String url = "jdbc:mysql://localhost:3306/bbs?user=root&password=root";
  9. Connection conn = DriverManager.getConnection(url);
  10. Statement smt = conn.createStatement();
  11. ResultSet rs = smt.executeQuery("select * from article where pid=0");
  12. while( rs.next() ){
  13.     str = "<tr><td>" + rs.getInt("id") + "</td><td>" +
  14.              "<a href='ArticleDetail.jsp?id=" + rs.getInt("id") + "'>" 
  15.              + rs.getString("title") + "</a>" + "</td><td>" + "<a href='Delete.jsp?id=" +
  16.              rs.getInt("id") + "'>" + "删除</a>""</td></tr>";
  17.     if( rs.getInt("isleaf") != 0){
  18.         Tree(conn,rs.getInt("id"),1);
  19.     }
  20. }
  21. rs.close();
  22. smt.close();
  23. conn.close();
  24. %>
  25. <%!
  26. String str="";
  27. private void Tree(Connection conn,int id,int level) {
  28.     String preString = "";
  29.     Statement stm = null;
  30.     ResultSet rs = null;
  31.     for(int i=0;i<level;i++){
  32.         preString += "----";
  33.     }
  34.     try{
  35.         stm = conn.createStatement();
  36.         rs = stm.executeQuery("select * from article where pid="+ id );
  37.         while(rs.next()){
  38.             str += "<tr><td>" + rs.getInt("id") + "</td><td>" +
  39.                     preString + "<a href='ArticleDetail.jsp?id=" + rs.getInt("id") + "'>" 
  40.                      + rs.getString("title") + "</a></td>""<td>" + "<a href='Delete.jsp?id=" +
  41.              rs.getInt("id") + "'>" + "删除</a>""</td></tr>";
  42.             if( rs.getInt("isleaf") != 0)
  43.                 Tree(conn,rs.getInt("id"),level+1);
  44.         }
  45.     }catch(SQLException e){
  46.         e.printStackTrace();
  47.     }finally{
  48.         try{
  49.             if(rs != null){
  50.                 rs.close();
  51.                 rs = null;
  52.             }
  53.             if(stm != null){
  54.                 stm.close();
  55.                 stm = null;
  56.             }
  57.         }catch(SQLException e){
  58.             e.printStackTrace();
  59.         }
  60.     }
  61. }
  62. %>
  63. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  64. <html>
  65.   <head>
  66.     <base href="<%=basePath%>">
  67.     
  68.     <title>论坛的树状结构</title>
  69.     
  70.     <meta http-equiv="pragma" content="no-cache">
  71.     <meta http-equiv="cache-control" content="no-cache">
  72.     <meta http-equiv="expires" content="0">    
  73.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  74.     <meta http-equiv="description" content="This is my page">
  75.     <!--
  76.     <link rel="stylesheet" type="text/css" href="styles.css">
  77.     -->
  78.   </head>
  79.   
  80.   <body> 
  81.     This is my BBS page. <br>
  82.     <table border="1">
  83.     <%= str %>
  84.     </table>
  85.   </body>
  86. </html>

 

  1. //ArticleDetail.jsp
  2. <%@ page language="java" contentType="text/html;charset=gbk" pageEncoding="gbk"%>
  3. <%@ page import=" java.sql.* " %>
  4. <%
  5. String path = request.getContextPath();
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  7. String paraId=request.getParameter("id");
  8. int id = Integer.parseInt(paraId);
  9. Class.forName("com.mysql.jdbc.Driver");
  10. String url = "jdbc:mysql://localhost:3306/bbs?user=root&password=root";
  11. Connection conn = DriverManager.getConnection(url);
  12. Statement smt = conn.createStatement();
  13. ResultSet rs = smt.executeQuery("select * from article where id=" + id);
  14. %>
  15. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  16. <html>
  17.   <head>
  18.     <base href="<%=basePath%>">
  19.     
  20.     <title>主题的标题和内容</title>
  21.     
  22.     <meta http-equiv="pragma" content="no-cache">
  23.     <meta http-equiv="cache-control" content="no-cache">
  24.     <meta http-equiv="expires" content="0">    
  25.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  26.     <meta http-equiv="description" content="This is my page">
  27.     <!--
  28.     <link rel="stylesheet" type="text/css" href="styles.css">
  29.     -->
  30.   </head>
  31.   
  32.   <body> 
  33.     
  34.     <%  
  35.     if( rs.next() ) { 
  36.     %>
  37.     <table border=1>
  38.     <tr>
  39.         <td>ID</td>
  40.         <td><%= rs.getInt("id") %></td>
  41.     </tr>
  42.     <tr>
  43.         <td>Title</td>
  44.         <td><%= rs.getString("title") %></td>
  45.     </tr>   
  46.     <tr>
  47.         <td>Cont</td>
  48.         <td><%=rs.getString("Cont")%></td>
  49.     </tr>
  50.     </table>
  51.     <a href="Reply.jsp?id=<%=rs.getInt("id") %>&rootid=<%=rs.getInt("rootid") %>">回复</a>
  52.     <a href="Delete.jsp?id=<%=rs.getInt("id") %>">删除</a>
  53.     <% } %>
  54.     
  55.     
  56.     <%
  57.     rs.close();
  58.     smt.close();
  59.     rs.close();
  60.     %>
  61.   </body>
  62. </html>

 

 

  1. //Delete.jsp
  2. <%@ page language="java" contentType="text/html;charset=GB2312" pageEncoding="GB2312"%>
    <%@ page import="java.sql.*" %>
  3. <%!
    private void delete(Connection conn,int id) {
     Statement stm = null;
     ResultSet rs = null;
     try{
      stm = conn.createStatement();
      String sql = "select * from article where pid=" + id;
      rs = stm.executeQuery(sql);
      while( rs.next() ) {
       delete(conn , rs.getInt("id") );
      }
      stm.executeUpdate("delete from article where id=" + id);
     }catch(SQLException e){
      e.printStackTrace();
     }finally{
      try {
       if( rs != null) {
        rs.close();
        rs = null;
       }
       if( stm != null ) {
        stm.close();
        stm = null;
       }
      }catch(SQLException e){
       e.printStackTrace();
      }
     }
    }
  4. %>

  5. <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  6. int id = Integer.parseInt(request.getParameter("id"));
    int pid = Integer.parseInt(request.getParameter("pid"));
  7. Class.forName("com.mysql.jdbc.Driver");
    String url = "jdbc:mysql://localhost:3306/bbs?user=root&password=root";
    Connection conn = DriverManager.getConnection(url);
    conn.setAutoCommit(false);
  8. delete(conn,id);
  9. conn.commit();
    conn.setAutoCommit(true);
  10. String sql = "select count(*) from article where pid=" + pid;
    Statement stm1 = conn.createStatement();
    ResultSet rs = stm1.executeQuery(sql);
    rs.next();
    int count = rs.getInt(1);
    if(count <=0 ) {
     stm1.executeUpdate("update article set isleaf=0 where id=" + pid);
    }
    rs.close();
    stm1.close();
    conn.close();
    %>
  11. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
       
        <title>My JSP 'Delete.jsp' starting page</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">
     -->
  12.   </head>
     
      <body>
       
        <% response.sendRedirect("ShowArticleTree.jsp");    %>
      </body>
    </html>
  1. //Reply.jsp
  2. <%@ page language="java" contentType="text/html; charset=GB2312"
  3.     pageEncoding="GB2312"%>
  4. <%
  5. String path = request.getContextPath();
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  7. int id = Integer.parseInt(request.getParameter("id"));
  8. int rootid = Integer.parseInt(request.getParameter("rootid"));
  9. %>
  10. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  11. <html>
  12.   <head>
  13.     <base href="<%=basePath%>">
  14.     
  15.     <title>回复页面</title>
  16.     
  17.     <meta http-equiv="pragma" content="no-cache">
  18.     <meta http-equiv="cache-control" content="no-cache">
  19.     <meta http-equiv="expires" content="0">    
  20.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  21.     <meta http-equiv="description" content="This is my page">
  22.     <!--
  23.     <link rel="stylesheet" type="text/css" href="styles.css">
  24.     -->
  25.   </head>
  26.   
  27.   <body>
  28.     <form action="ReplyOk.jsp">
  29.         <input type="hidden" name="id" value="<%=id %>" />
  30.         <input type="hidden" name="rootid" value="<%=rootid %>" />
  31.         <input type="hidden" name="testString" value="回答是开放" />
  32.         <table border="1">
  33.             <tr>
  34.                 <td><input type="text" name="title" size="80" /></td>            
  35.             </tr>
  36.             <tr>
  37.                 <td><textarea rows="15" cols="80" name="cont"></textarea></td>
  38.             </tr>
  39.             <tr>
  40.             <td><input type="submit" value="回复" /></td>
  41.             </tr>
  42.             
  43.         </table>
  44.     </form>
  45.   </body>
  46. </html>

 

 

 

 

 

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值