1. 程式人生 > >[java原始碼] Java Web 文章管理系統(Jsp+Ajax+JDBC+MySql實現)

[java原始碼] Java Web 文章管理系統(Jsp+Ajax+JDBC+MySql實現)

本示例是使用JavaWeb技術實現一個簡單的文章管理系統(新聞管理系統)其中主要功能如下:

  • 使用者和管理員登入
  • 使用者釋出新文章、文章詳情檢視、文章修改、文章刪除與恢復
  • 使用者檢視他人對自己授權的文章及其文章資訊
  • 使用者將自己的文章對他人進行授權
  • 管理員對普通使用者新發布的文章進行稽核和刪除
  • 管理員檢視普通使用者釋出的所有文章及其詳情
  • 管理員釋出新文章

使用的主要技術有: 
JavaEE、JDBC、AJAX、JSP、JavaBean

本專案開發環境為:

  • Intellij IDEA 2016.3
  • Tomcat 8
  • JDK 1.8
  • MySQL 5.5

專案工程下載地址:文章管理系統

  http://bbs.jiandaima.com/thread-1313-1-1.html
下載完工程檔案,按照下面的資料庫結構截圖建立好資料庫的表,然後在 NewsRealeseDao.Java中配置好相關的資料庫URL、使用者名稱和密碼應該就可以直接執行起來了。

簡單介面展示

登入介面 
登入介面展示

使用者主要瀏覽介面 
主要瀏覽介面

釋出文章介面 
這裡寫圖片描述

管理員介面 
這裡寫圖片描述

為了能夠讓下載的工程檔案直接執行這裡再放上資料庫的結構圖: 
這裡寫圖片描述

簡單程式碼介紹

為了節省篇幅這裡主要介紹JSP中負責和Servlet或後臺DAO有關的程式碼。

登入介面:

 
主要的就是form標籤裡的action屬性,表示將表單裡的內容提交給後臺的checkLogin_user這個Servlet進行處理,其中input標籤裡的name屬性標記其中的值,可以在Servlet中使用request.getparameter()方法得到標籤中填入的值。 
(還有一點需要注意的是action裡的方法需要先在web.xml檔案中進行註冊,這樣tomcat伺服器才能正確的找到對應的類進行後續的處理,實際上所有的Servlet類都需要在web.xml中進行註冊,所以後面就不再贅述這個問題了)

<form method="post" action="checkLogin_user"
> <div class="panel"> <div class="panel-head"><strong>使用者登入</strong></div> <div class="panel-body" style="padding:30px;"> <div class="form-group"> <div class="field field-icon-right"> <input type="text" class="input" name="user" placeholder="Username"/> <span class="icon icon-user"></span> </div> </div> <div class="form-group"> <div class="field field-icon-right"> <input type="password" class="input" name="pass" placeholder="Password"/> <span class="icon icon-key"></span> </div> </div> </div> <div class="panel-foot text-center"> <button class="button button-block bg-main text-big">登入</button> </div> </div> </form>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

處理登入的Servlet: 
處理JSP傳送過來的資料,呼叫後臺程式進行處理,並返回結果。

package servlet;

import dao.NewsRealeseDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class checkLogin_user extends HttpServlet {



    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
       //這裡的"user"和"pass"必須要和JSP裡的對應的標籤的name屬性相同
        String user=request.getParameter("user");
        String pass=request.getParameter("pass");

        NewsRealeseDao newsRealeseDao=new NewsRealeseDao();
        try {
            boolean checked=newsRealeseDao.ischecked(user,pass,"user");
            //呼叫後臺的Dao方法利用user表進行身份驗證
            if(checked)
            {
                HttpSession session=request.getSession();
                session.setAttribute("username",user);//設定使用者的姓名
                           response.sendRedirect("content_user.jsp");
            }
            else
            {
                response.sendRedirect("login_user.jsp");

            }
        }
        catch (Exception ex)
        {
            Logger.getLogger(checkLogin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description"+"public String getServletInfo() ";
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60

資料庫訪問類newsRealeseDao: 
訪問資料庫並提供相關的方法。


import com.lut.beans.NewsRealese;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;

public class NewsRealeseDao {

    public static String driver = "com.mysql.jdbc.Driver";//定義驅動
    public static String url = "jdbc:mysql://localhost:3306/myNews?useUnicode=true&characterEncoding=utf-8";//定義URL
    public static String databseUser = "root";//定義使用者名稱
    public static String password = "root";//定義密碼


    private ArrayList getNews(Statement stat, String sql)//處理具體的新聞查詢請求,返回所有結果
    {
        ArrayList newsRealese = new ArrayList();
        try {
            ResultSet rs = stat.executeQuery(sql);
            while (rs.next()) {   //例項化VO
                NewsRealese news = new NewsRealese();
                news.setNewsId(rs.getString("newsid"));
                news.setContent(rs.getString("content"));
                news.setHead(rs.getString("head"));
                news.setIssueuser(rs.getString("issueuser"));
                news.setPublish_time(rs.getString("publish_time"));
                news.setNewstype(rs.getString("newstype"));
                newsRealese.add(news);
            }
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return newsRealese;
        }
    }


    public ArrayList UserQueryAllNews(String username, String table) throws Exception {//使用者檢視自己所有的文章
        Connection conn = null;
        ArrayList newsRealese = new ArrayList();
        try {
            //獲取連線
            Class.forName(driver);

            conn = DriverManager.getConnection(url, databseUser, password);
            //執行SQL語句
            String sql = "select * from " + table + " where issueuser='" + username + "' order by publish_time desc";
            Statement stat = conn.createStatement();
            newsRealese = getNews(stat, sql);
            if (newsRealese.size() == 0) {
                System.out.println("查詢不到任何資訊============");
                return null;
            }

            stat.close();
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            try {//關閉連線
                if (conn != null) {
                    conn.close();

                }
            } catch (Exception ex) {
            }
            return newsRealese;
        }
    }


    public ArrayList UserQueryOthersNews(String username) throws Exception {//檢視自己可見的其他人的所有的文章
        Connection conn = null;
        ArrayList rt = new ArrayList();
        try {
            //獲取連線
            Class.forName(driver);

            conn = DriverManager.getConnection(url, databseUser, password);
            //執行SQL語句
            String sql = "select * from news where newsId IN (select newsId from authority where username=?) order by publish_time desc";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, username);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                NewsRealese news = new NewsRealese();
                news.setNewsId(rs.getString("newsid"));
                news.setContent(rs.getString("content"));
                news.setHead(rs.getString("head"));
                news.setIssueuser(rs.getString("issueuser"));
                news.setPublish_time(rs.getString("publish_time"));
                news.setNewstype(rs.getString("newstype"));
                rt.add(news);
            }
            if (rt.size() == 0) {
                System.out.println("查詢不到任何資訊============Dao.UserQueryOthersNews");
                return null;
            }

            conn.close();
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            try {//關閉連線
                if (conn != null) {
                    conn.close();

                }
            } catch (Exception ex) {
            }
            return rt;
        }
    }


    public ArrayList AdministorQueryAllNews(String username) throws Exception {//管理員檢視所有的文章
        Connection conn = null;
        ArrayList newsRealese = new ArrayList();
        try {
            //獲取連線  
            Class.forName(driver);

            conn = DriverManager.getConnection(url, databseUser, password);
            //執行SQL語句 
            String sql = "select * from news order by publish_time desc";//獲取
            Statement stat = conn.createStatement();
            newsRealese = getNews(stat, sql);
            if (newsRealese.size() == 0) {
                System.out.println("查詢不到任何資訊============");
                return null;
            }

            stat.close();
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            try {//關閉連線
                if (conn != null) {
                    conn.close();

                }
            } catch (Exception ex) {
            }
            return newsRealese;
        }
    }

    //查詢一個訊息
    public ArrayList AdministorQueryCheckPending() throws Exception {//查詢所有的待稽核的文章
        Connection conn = null;
        ArrayList newsRealese = new ArrayList();

        try {
            //獲取連線  
            Class.forName(driver);

            conn = DriverManager.getConnection(url, databseUser, password);//不安全
            //執行SQL語句 
            Statement stat = conn.createStatement();
            String sql = "select * from check_pending order by publish_time desc";//獲取newsid,使用?代替字串,以免會發生錯誤
            Statement st = conn.createStatement();
            newsRealese = getNews(st, sql);

            stat.close();
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            try {//關閉連線
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (Exception ex) {
            }
            return newsRealese;
        }
    }


    //使用者刪除資料
    public String deleteOneNews(String newsid, String table) throws Exception {
        Connection conn = null;
        ArrayList newsRealese = new ArrayList();
        try {
            //獲取連線  
            Class.forName(driver);
            conn = DriverManager.getConnection(url, databseUser, password);//不安全
            //執行SQL語句

            String sql_move = "insert into dustbin select * from news where newsId='" + newsid + "'";

            String sql_delete = "DELETE FROM " + table + " WHERE newsId='" + newsid + "'";//獲取newsid,使用?代替字串,以免會發生錯誤
            Statement ps = conn.createStatement();

            int rs_move = ps.executeUpdate(sql_move);
            if (rs_move != 0) {
                int rs = ps.executeUpdate(sql_delete);
                if (rs == 0)
                    System.out.println("刪除失敗==================NewsrealeaseDao");
            } else {
                System.out.println("插入到dustbin出錯=============NewsrealeaseDao");
            }


        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            try {//關閉連線
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception ex) {
            }
            return newsRealese.toString();
        }
    }


    public String AdmindeleteCheck_pendingNews(String newsid) throws Exception {
        Connection conn = null;
        int rs = 0;
        try {
            //獲取連線
            Class.forName(driver);
            conn = DriverManager.getConnection(url, databseUser, password);//不安全
            //執行SQL語句


            String sql_delete = "DELETE FROM  check_pending WHERE newsId='" + newsid + "'";//獲取newsid,使用?代替字串,以免會發生錯誤
            Statement ps = conn.createStatement();


            rs = ps.executeUpdate(sql_delete);
            if (rs == 0) {
                System.out.println("刪除失敗==================NewsrealeaseDao");
                return null;
            }
            else
                return "成功刪除"+rs;
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            try {//關閉連線
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception ex) {
            }

        }
        return null;
    }


    public int AuthorizeOneNews(ArrayList<String> userlist, String newsid) {
        int count = 0;
        Connection conn = null;

        try {
            //獲取連線
            Class.forName(driver);
            conn = DriverManager.getConnection(url, databseUser, password);//不安全
            //執行SQL語句
            Statement stat = conn.createStatement();

            for (int i = 0; i < userlist.size(); i++) {
                String sql = "insert into authority  VALUES(?,?)";
                PreparedStatement ps = conn.prepareStatement(sql);
                ps.setString(1, newsid);
                ps.setString(2, userlist.get(i));
                System.out.println(ps.toString());
                count += ps.executeUpdate();

            }

            System.out.println("成功新增" + count + "行");
            stat.close();
            conn.close();

        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            try {//關閉連線
                if (conn != null) {
                    conn.close();

                }
            } catch (Exception ex) {
            }
            return count;
        }
    }


    //插入資料
    public int insertOneNews(HashMap<String, String> addnews_list, String table) throws Exception {//插入一個新的新聞
        Connection conn = null;

        try {
            //獲取連線  
            Class.forName(driver);
            conn = DriverManager.getConnection(url, databseUser, password);//不安全
            //執行SQL語句 
            Statement stat = conn.createStatement();
            String sql = "insert into " + table + "  VALUES(?,?,?,?,?,?)";//獲