乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      基于Servlet、JSP的學(xué)生管理系統(tǒng)(附完整源碼)

       路人甲Java 2021-06-26

      起因

      最近重溫servlet,想到了大學(xué)時期用同一個“學(xué)生管理系統(tǒng)”水了幾門課的課程設(shè)計,不免感慨萬千。

      周末簡單的寫了個界面,建了幾張表,做了一個小系統(tǒng)(試圖找一找當(dāng)年劃水的感覺,可惜沒找到)。

      寫的比較簡單,不過做個普通的課程設(shè)計應(yīng)該夠了,需要的可以自取。

      源碼地址

      https:///DayCloud/student-manage

      界面截圖

      主界面

       

       

       管理員界面

       

       

       學(xué)生管理(管理員視角)

       

       

       添加系統(tǒng)用戶(管理員視角)

       

       

       學(xué)生主頁

       

       

       學(xué)生個人信息

       

       目錄結(jié)構(gòu)

      運行環(huán)境

      tomcat9

      jdk1.8

      其他依賴jar包見WEB-INF下面的lib文件夾。

      涉及技術(shù):Servlet、JSP、BootStrap、Jquery(較少)

      主要功能

      系統(tǒng)有兩個角色,管理員和學(xué)生。做的比較簡單,沒有建額外的角色表、權(quán)限表,僅僅用了一個字段區(qū)分。

      管理員可以管理學(xué)生信息、教師信息、可以添加系統(tǒng)用戶,錄入成績,具有增刪改查的一切權(quán)限。

      學(xué)生只能查看自己的分?jǐn)?shù),個人檔案等。

      代碼分析

      首頁數(shù)據(jù)統(tǒng)計

      系統(tǒng)運行時常、當(dāng)前在線人數(shù),這兩個功能用到了servlet的組件,監(jiān)聽器。

      通過繼承ServletContextListener, HttpSessionListener, HttpSessionAttributeListener等接口,可以完成對servlet上下文、session的創(chuàng)建銷毀等關(guān)鍵節(jié)點的監(jiān)聽。

      在線人數(shù),必然是登錄成功的人數(shù)。而session是有人訪問頁面就會創(chuàng)建,所以我們不能根據(jù)session的創(chuàng)建和銷毀來統(tǒng)計在線人數(shù)。

      在登陸成功后,會在session里添加一個變量,我們可以監(jiān)聽這一行為。

      當(dāng)設(shè)置session變量的時候,在線人數(shù)+1

      移除session變量的時候,在線人數(shù)-1。

      當(dāng)然這種做法還是有問題的,比如直接關(guān)閉瀏覽器,不點注銷,數(shù)據(jù)統(tǒng)計就會失效,這里不做深入探究。

      再來說說系統(tǒng)運行時長,我的思路是servlet上下文創(chuàng)建的時候,記錄下那個時刻的時間戳。

      后面用到的時候,直接用當(dāng)前的時間戳減去保存的時間戳,就可以計算出相隔的毫秒數(shù),也就可以得到天數(shù)。

       

      @WebListener
      public class CustomServerListener implements ServletContextListener, HttpSessionListener, HttpSessionAttributeListener {
      
          private volatile ServletContext application = null;
          //上下文初始化,記錄當(dāng)前時間的時間戳,初始化人數(shù)統(tǒng)計變量
          @Override
          public void contextInitialized(ServletContextEvent sce) {
              System.out.println("初始化開始---------");
              int onlineNum = 0;
              application = sce.getServletContext();
              application.setAttribute("onlineNum", onlineNum);
              application.setAttribute("startTime", new Date().getTime());
          }
      
          @Override
          public void contextDestroyed(ServletContextEvent sce) {
              ServletContextListener.super.contextDestroyed(sce);
          }
      
          //session創(chuàng)建的時候調(diào)用該方法。但是我們計算在線人數(shù)指的是登錄成功的人
          @Override
          public void sessionCreated(HttpSessionEvent se) {
              
          }
      
          //連接斷開
          @Override
          public void sessionDestroyed(HttpSessionEvent se) {
              
          }
      
          //
          @Override
          public void attributeAdded(HttpSessionBindingEvent se) {
              System.out.println("有人登錄了---------");
              int onlineNum = (int) application.getAttribute("onlineNum");
              application.setAttribute("onlineNum", ++onlineNum);
          }
          
          
      
          @Override
          public void attributeRemoved(HttpSessionBindingEvent se) {
              System.out.println("有人退出了---------");
              int onlineNum = (int) application.getAttribute("onlineNum");
              application.setAttribute("onlineNum", --onlineNum);
          }
      
          @Override
          public void attributeReplaced(HttpSessionBindingEvent se) {
              
          }
      }

      計算統(tǒng)計數(shù)據(jù)的servlet

      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
              StudentService studentService = new StudentService();
              CourseService courseService = new CourseService();
              ScoreService scoreService = new ScoreService();
              int studentNum = studentService.count();
              int courseNum = courseService.count();
              int onlineNum = (int) request.getServletContext().getAttribute("onlineNum");
              long startTime = (long) request.getServletContext().getAttribute("startTime");
              List<ScoreDto> scoreList = scoreService.getTopScoreList(10);
              
              int days = (int)((new Date().getTime() - startTime) / (1000*3600*24)) + 1;
              request.setAttribute("studentNum", studentNum);
              request.setAttribute("courseNum", courseNum);
              request.setAttribute("onlineNums", onlineNum);
              request.setAttribute("days", days);
              request.setAttribute("scores", scoreList);
              
              
              
              
              request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request, response);
          }

      身份校驗

      身份校驗自然就用到了過濾器。

      這邊沒有做復(fù)雜的角色權(quán)限校驗,僅僅在用戶表加上一個字段表示區(qū)分。

      兩個過濾器。

      一個檢查用戶是否登錄(有些頁面需要登錄,有些不需要的可以放行)

      另一個檢查權(quán)限夠不夠。

      @WebFilter(value = "/*")
      public class LoginFilter implements Filter {
      
          private static List<String> passUrlList = Arrays.asList("login.jsp", "css"
                  , "js", "jpg", "loginUrl");
      
          /**
           * Default constructor.
           */
          public LoginFilter() {
              // TODO Auto-generated constructor stub
          }
      
          /**
           * @see Filter#destroy()
           */
          public void destroy() {
              // TODO Auto-generated method stub
          }
      
          /**
           * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
           */
          public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                  throws IOException, ServletException {
              HttpServletRequest req = (HttpServletRequest) request;
              HttpServletResponse resp = (HttpServletResponse) response;
      
              String uri = req.getRequestURI();
              // 登錄頁以及靜態(tài)資源放行
              boolean needLogin = true;
              //頁面名稱
              String pageName = "";
              //后綴名
              String endName = "";
              if(uri.lastIndexOf("/") != -1 && uri.lastIndexOf("/") + 1 < uri.length()) {
                  pageName = uri.substring(uri.lastIndexOf("/") + 1);
              }
              
              if(uri.lastIndexOf(".") != -1 && uri.lastIndexOf(".") + 1 < uri.length()) {
                  endName = uri.substring(uri.lastIndexOf(".") + 1);
              }
              
              for (String passUrl : passUrlList) {
                  if(passUrl.equals(pageName) || passUrl.equals(endName)) {
                      //不需要登錄
                      needLogin = false;
                  }
              }
              
              User user = (User) req.getSession().getAttribute("loginUser");
              
              if(needLogin && user == null) {
                  //該資源需要登錄,并且當(dāng)前用戶沒有登錄
                  resp.sendRedirect("/StudentManage/login.jsp");
              }else {
                  //不需要登錄
                  chain.doFilter(req, resp);
              }
      
              
          }
      
          /**
           * @see Filter#init(FilterConfig)
           */
          public void init(FilterConfig fConfig) throws ServletException {
              // TODO Auto-generated method stub
          }
      
      }

      權(quán)限校驗過濾器

      @WebFilter(value = "/admin/*", filterName = "B")
      public class AuthenticationFilter implements Filter{
      
          @Override
          public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                  throws IOException, ServletException {
              // TODO Auto-generated method stub
              HttpServletRequest req = (HttpServletRequest) request;
              
              User user = (User) req.getSession().getAttribute("loginUser");
              Byte type = user.getUserType();
              if(type != 1) {
                  //不是管理員,跳轉(zhuǎn)到錯誤頁面
                  req.setAttribute("msg", "抱歉,您沒有權(quán)限訪問!");
                  req.getRequestDispatcher("/WEB-INF/pages/error.jsp").forward(req, response);;
              }else {
                  chain.doFilter(req, response);
              }
          }
      
      }

      其他

      整體上寫的隨心所欲,不是很規(guī)范。

      查找以及分頁界面做了,后臺沒做。因為感覺沒啥必要,原生的servlet知道基本原理和用法即可,寫業(yè)務(wù)直接SpringBoot吧。

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多