基於JavaWeb實現的研究室綜合系統

代碼地址以下:<br>http://www.demodashi.com/demo/14641.htmlcss

概述html

基於JavaWeb實現的研究室綜合系統,功能包括研究室成員註冊、登錄,後臺管理,相冊功能,新聞模塊,資源模塊,論壇模塊,諮詢模塊,簡易答題系統模塊。前端

1、準備工做

開發環境:java

  • JDK1.8
  • Tomcat9.0
  • Intellij IDEA2018.2

運行環境:jquery

  • windows7以上
  • Mac
  • Chrome瀏覽器

實現功能:ajax

  • 研究室綜合系統,功能包括研究室成員註冊、登錄,後臺管理,相冊功能,新聞模塊,資源模塊,論壇模塊,諮詢模塊,簡易答題系統模塊。

2、代碼結構

sql 目錄下面的sql文件是項目數據庫結構sql

程序實現-Web系統

1.後臺上傳資源實現

public class uploadResServlet extends HttpServlet {

    private final String local = URL.getUrl();
    private final String tempFile = local + "resources\\tempFile"; //臨時上傳目錄
    private final String uploadFile = local + "resources\\uploadFile"; //上傳目錄
    FileUploadProperties instance = FileUploadProperties.getInstance();
    private final String exts = instance.getPropertyValue("exts");
    private final String fileMaxSize = instance.getPropertyValue("file.max.size");
    private final String total = instance.getPropertyValue("total.file.max.size");
    private dbOperator db = null;

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        ServletFileUpload upload = getUpload();
        String user = request.getSession().getAttribute("user").toString();
        String userpower = request.getSession().getAttribute("userpower").toString();
        String content = "";
        String fileName = "";
        String filePath = "";
        String path  = "";
        if(userpower.equals("1")){
            path = "resources/useraddres.jsp";
        }else if(userpower.equals("2")){
            path = "admin/resources/uploadres.jsp";
        }
        
        try {
            List<FileItem> items = upload.parseRequest(request);
            for (FileItem item : items) {
                if (item.isFormField()) {
                    String formName = item.getFieldName();
                    String formContent = item.getString("utf-8");
                    if (formName.equals("introduce")) {
                        content = formContent;
                    }
                } else {
                    fileName = item.getName();
                    String extName = fileName.substring(fileName.indexOf(".") + 1);
                    try {
                        validate(extName);
                        db = new dbOperator();
                        if (!db.isSameName("r_title", "resources", fileName)) {
                            InputStream inStream = item.getInputStream();
                            filePath = uploadFile + "\\" + fileName;
                            String sql = "insert into resources values(" + null + ",'" + fileName + "','" + content + "','" + filePath.replace('\\', '/') + "','" + user + "')";
                            if (db.SqlQuery(sql) != 0) {
                                byte[] buffer = new byte[1024];
                                int len = 0;
                                OutputStream outStream = new FileOutputStream(filePath);
                                while ((len = inStream.read(buffer)) != -1) {
                                    outStream.write(buffer, 0, len);
                                }
                                inStream.close();
                                outStream.close();
                                request.setAttribute("message", "上傳成功!");
                            } else {
                                request.setAttribute("message", "上傳失敗!");
                            }
                        } else {
                            request.setAttribute("message", "上傳失敗,該文件名已存在!請檢查是否已經上傳!");
                        }

                    } catch (InvalidExtNameException ex) {
                        request.setAttribute("message", ex.getMessage());
                        ex.printStackTrace(System.err);
                    }
                }

            }
            request.getRequestDispatcher(path).forward(request, response);
        } catch (FileUploadException e) {
        }

    }

    private ServletFileUpload getUpload() {
        // 爲基於硬盤文件的項目集建立一個工廠
        DiskFileItemFactory factory = new DiskFileItemFactory();
        // 設置工廠的約束條件
        factory.setSizeThreshold(1024 * 5);

        File tempDir = new File(tempFile);
        File uploadDir = new File(uploadFile);
        if (!tempDir.exists()) {
            tempDir.mkdirs();
        }
        if (!uploadDir.exists()) {
            uploadDir.mkdirs();
        }
        factory.setRepository(tempDir);

        // Create a new file upload handler(建立一個新文件上傳處理程序)
        ServletFileUpload upload = new ServletFileUpload(factory);
        //設置請求的總大小限制
        upload.setSizeMax(Integer.parseInt(fileMaxSize));
        return upload;
    }

    private void validate(String extName) {
        List<String> extList = Arrays.asList(exts.split(","));
        if (!extList.contains(extName)) {
            throw new InvalidExtNameException("文件類型錯誤!請上傳文檔類型的資源(doc,docx,pptx,ppt,txt)!");
        }
    }

}

基於EasyUI實現的後臺用戶管理頁面

<%-- 
    Document   : manageuser
    Created on : 2017-6-19, 9:48:30
    Author     : 挺
    註釋:這是對普通用戶進行增刪改查的界面
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link href="../../images/b128.png" rel="icon" type="images/x-ico"/>
        <link href="../../EasyUI/themes/icon.css" rel="stylesheet" />
        <link href="../../EasyUI/themes/default/easyui.css" rel="stylesheet" />
        <script src="../../EasyUI/jquery.min.js"></script>
        <script src="../../EasyUI/jquery.easyui.min.js"></script>
        <script src="../../EasyUI/locale/easyui-lang-zh_CN.js"></script>
        <title>用戶信息管理</title>
    </head>
    <body>
        <table id="dg">
        </table>
    </body>
</html>

<script>
    var flag;
    var dep = [{"value": "信息學院", "text": "信息學院"}];
    var major = [{"value": "網絡工程", "text": "網絡工程"}, {"value": "計算機科學與工程", "text": "計算機科學與工程"}];
    var sex = [{"value": "男", "text": "男"}, {"value": "女", "text": "女"}];
    var Class = [{"value": "網絡121", "text": "網絡121"}, {"value": "網絡122", "text": "網絡122"}, {"value": "計算機121", "text": "計算機121"}, {"value": "計算機122", "text": "計算機122"}, {"value": "計算機123", "text": "計算機123"}];

    $('#dg').datagrid({
        url: '../../UserServlet',
        loadMsg: "數據加載中...",
        pagination: true,
        fit: true,
        singleSelect: true,
        fitColumns: true,
        onSelect: function() {
            $('#btn_edit').linkbutton('enable');
            $('#btn_no').linkbutton('enable');
            $('#btn_save').linkbutton('enable');
            $('#btn_reload').linkbutton('enable');
            $('#btn_cut').linkbutton('enable');
        },
        onUnselect: function() {
            $('#btn_edit').linkbutton('disable');
            $('#btn_no').linkbutton('disable');
            $('#btn_save').linkbutton('disable');
            $('#btn_reload').linkbutton('disable');
            $('#btn_cut').linkbutton('disable');
        },
        columns: [[
                {field: 'uname', title: '用戶名', width: 10, editor: 'text'},
                {field: 'urealname', title: '姓名', width: 10, editor: 'text'},
                {field: 'uage', title: '年齡', width: 5, editor: 'numberbox'},
                {field: 'usex', title: '性別', width: 5,
                    editor: {
                        type: 'combobox', options: {data: sex, valueField: 'value', textField: 'text', panelHeight: 'auto', editable: false}
                    }
                },
                {field: 'uaddress', title: '地址', width: 15, editor: 'text'},
                {field: 'uinstitute', title: '學院', width: 10,
                    editor: {
                        type: 'combobox', options: {data: dep, valueField: 'value', textField: 'text', panelHeight: 'auto', editable: false}
                    }
                },
                {field: 'umajor', title: '專業', width: 10,
                    editor: {
                        type: 'combobox', options: {data: major, valueField: 'value', textField: 'text', panelHeight: 'auto', editable: false}
                    }
                },
                {field: 'uclass', title: '班級', width: 10,
                    editor: {
                        type: 'combobox', options: {data: Class, valueField: 'value', textField: 'text', panelHeight: 'auto', editable: false}
                    }
                },
                {field: 'ustudentid', title: '學號', width: 10},
                {field: 'uemail', title: '郵箱', width: 15, editor: 'text'}
            ]],
        toolbar: [
            {
                iconCls: 'icon-add',
                id: 'btn_add',
                text: '添加',
                handler: function() {
                    flag = 1;
                    $('#dg').datagrid('insertRow', {index: 0, row: {}});
                    $('#dg').datagrid('selectRow', 0);
                    $('#dg').datagrid('beginEdit', 0);
                }
            }, '-', {
                iconCls: 'icon-edit',
                id: 'btn_edit',
                text: '編輯',
                disabled: true,
                handler: function() {
                    flag = 2;
                    var row = $('#dg').datagrid("getSelected");
                    var index = $('#dg').datagrid('getRowIndex', row);
                    $('#dg').datagrid('beginEdit', index);
                }
            }, '-', {
                iconCls: 'icon-save',
                id: 'btn_save',
                text: '保存',
                disabled: true,
                handler: function() {
                    var row = $('#dg').datagrid("getSelected");
                    $('#edit').linkbutton('disable');
                    $('#no').linkbutton('disable');
                    $('#save').linkbutton('disable');
                    $('#reload').linkbutton('disable');
                    var index = $('#dg').datagrid('getRowIndex', row);
                    $('#dg').datagrid('endEdit', index);
                    if (row.uname + "" == "" || row.urealname + "" == "") {
                        alert("信息不能爲空!");
                        $('#btn_edit').linkbutton('enable');
                        $('#btn_no').linkbutton('enable');
                        $('#btn_save').linkbutton('enable');
                        $('#dg').datagrid('beginEdit', index);
                        return;
                    }
                    if (flag == 1) {
                        $.messager.prompt('提示信息', '請輸入學號:', function(r) {
                            if (r) {
                                row.ustudentid = r;
                                $.ajax({
                                    type: "post",
                                    url: "../../UserAdminServlet", //對管理員進行添加和編輯進行邏輯處理的servlet
                                    data: {
                                        flag: flag,
                                        ustudentid: row.ustudentid,
                                        uname: row.uname,
                                        urealname: row.urealname,
                                        uage: row.uage,
                                        usex: row.usex,
                                        uaddress: row.uaddress,
                                        uinstitute: row.uinstitute,
                                        umajor: row.umajor,
                                        uclass: row.uclass,
                                        uemail: row.uemail
                                    },
                                    success: function(datas) {
                                        var datas = eval("(" + datas + ")");
                                        if (datas.data == "0") {
                                            $.messager.alert('個人消息', '添加成功!', 'info');
                                            $('#dg').datagrid('reload', null);
                                            $('#btn_edit').linkbutton('disable');
                                            $('#btn_no').linkbutton('disable');
                                            $('#btn_save').linkbutton('disable');
                                            $('#btn_reload').linkbutton('disable');
                                            $('#btn_cut').linkbutton('disable');
                                            $('#dg').datagrid('reload', null);
                                        }
                                        else if (datas.data == "2") {
                                            $.messager.alert('個人消息', '添加失敗!', 'info');
                                            $('#dg').datagrid('reload', null);
                                        } else {
                                            $.messager.alert('個人消息', '添加的用戶名已存在!', 'info');
                                            //$('#dg').datagrid('reload', null);
                                            $('#dg').datagrid('beginEdit', 0);
                                        }
                                    }
                                });
                            }
                        });
                    } else if (flag == 2) {
                        $.ajax({
                            type: "post",
                            url: "../../UserAdminServlet", //對管理員進行添加和編輯進行邏輯處理的servlet
                            data: {
                                flag: flag,
                                ustudentid: row.ustudentid,
                                uname: row.uname,
                                urealname: row.urealname,
                                uage: row.uage,
                                usex: row.usex,
                                uaddress: row.uaddress,
                                uinstitute: row.uinstitute,
                                umajor: row.umajor,
                                uclass: row.uclass,
                                uemail: row.uemail
                            },
                            success: function(datas) {
                                var datas = eval("(" + datas + ")");
                                if (datas.data == "1") {
                                    $.messager.alert('個人消息', '編輯成功!', 'info');
                                    $('#dg').datagrid('reload', null);
                                    $('#btn_edit').linkbutton('disable');
                                    $('#btn_no').linkbutton('disable');
                                    $('#btn_save').linkbutton('disable');
                                    $('#btn_reload').linkbutton('disable');
                                    $('#btn_cut').linkbutton('disable');
                                    $('#dg').datagrid('reload', null);
                                }
                                else {
                                    $.messager.alert('個人消息', '編輯失敗!', 'info');
                                    $('#dg').datagrid('reload', null);
                                }
                            }
                        });
                    }

                }
            }, '-', {
                iconCls: 'icon-cut',
                id: 'btn_cut',
                text: '刪除',
                disabled: true,
                handler: function() {
                    var row = $('#dg').datagrid("getSelected");
                    $.messager.confirm('刪除', '確認刪除嗎?', function(r) {
                        if (r) {
                            flag = 3;
                            // 退出操做;
                            $.ajax({
                                type: "post",
                                url: "../../UserAdminServlet",
                                data: {
                                    flag: flag,
                                    ustudentid: row.ustudentid
                                },
                                success: function(data) {
                                    var data = eval("(" + data + ")");
                                    if (data.data == "5") {
                                        $.messager.alert('個人消息', '刪除成功!', 'info');
                                        $('#dg').datagrid('reload', null);
                                        $('#btn_edit').linkbutton('disable');
                                        $('#btn_no').linkbutton('disable');
                                        $('#btn_save').linkbutton('disable');
                                        $('#btn_reload').linkbutton('disable');
                                        $('#btn_cut').linkbutton('disable');
                                    }
                                    else {
                                        $.messager.alert('個人消息', '刪除失敗!', 'info');
                                    }
                                }
                            })
                        }
                    });
                }
            }, '-', {
                iconCls: 'icon-no',
                id: 'btn_no',
                text: '取消編輯',
                disabled: true,
                handler: function() {
                    var row = $('#dg').datagrid("getSelected");
                    var index = $('#dg').datagrid('getRowIndex', row);
                    $('#dg').datagrid('cancelEdit', index);
                }
            }, '-', {
                iconCls: 'icon-reload',
                id: 'btn_reload',
                text: '重置密碼',
                disabled: true,
                handler: function() {
                    var row = $('#dg').datagrid("getSelected");
                    $.messager.confirm('重置', '確認重置嗎?', function(r) {
                        if (r) {
                            flag = 4;
                            // 退出操做;
                            $.ajax({
                                type: "post",
                                url: "../../UserAdminServlet",
                                data: {
                                    flag: flag,
                                    ustudentid: row.ustudentid
                                },
                                success: function(datas) {
                                    var datas = eval("(" + datas + ")");
                                    if (datas.data == "7") {
                                        $.messager.alert('個人消息', '重置成功!', 'info');
                                        $('#dg').datagrid('reload', null);
                                        $('#btn_edit').linkbutton('disable');
                                        $('#btn_no').linkbutton('disable');
                                        $('#btn_save').linkbutton('disable');
                                        $('#btn_reload').linkbutton('disable');
                                        $('#btn_cut').linkbutton('disable');
                                    }
                                    else {
                                        $.messager.alert('個人消息', '重置失敗!', 'info');
                                    }
                                }
                            });
                        }
                    });
                }
            }]

    });

    //分頁
    var pagenum = 10;
    load();
    function load() {
        var p = $('#dg').datagrid('getPager');
        $(p).pagination({
            pageSize: pagenum, //每頁顯示的記錄條數,默認爲10  
            pageList: [10, 20, 30], //能夠設置每頁記錄條數的列表 
            beforePageText: '第', //頁數文本框前顯示的漢字  
            afterPageText: '頁    共 {pages} 頁',
            displayMsg: '當前顯示 {from} - {to} 條記錄   共 {total} 條記錄',
            onBeforeRefresh: function(pageNumberNow, pageSizeNow) {
                pagenum = pageSizeNow;
                $('#dg').datagrid('load', null);
                load();
            }

        });
    }
</script>

4、運行效果

一、 將sql文件導入本身本地的數據庫; 二、 Intellij IDEA 導入下載的項目,在Add Configuration中配置好Tomcat,點擊Run 便可; 三、 管理員用戶:admin 密碼:123456數據庫

首頁

登錄頁面

後臺管理頁面

後臺用戶管理頁面

  • 包括增刪改查、重置密碼

後臺上傳資源頁面

####後臺新聞發佈頁面 windows

前端用戶資源頁面

前端論壇頁面

前端答題系統頁面

前端分數查詢頁面

基於JavaWeb實現的研究室綜合系統瀏覽器

代碼地址以下:<br>http://www.demodashi.com/demo/14641.html

注:本文著做權歸做者,由demo大師代發,拒絕轉載,轉載須要做者受權

相關文章
相關標籤/搜索