使用JSP技術實現一個簡單的線上測試系統的例項詳解
1、登陸介面
實現:
本介面由三部分構成,Footer.jsp,Index.jsp,Header.jsp
Header.jsp
<center> <h2>線上測試系統</h2> <p> <a href="Index.jsp" rel="external nofollow" >登入</a> | <a href="test.jsp" rel="external nofollow" >線上測試</a> | <a href="scorelist.jsp" rel="external nofollow" >成績榜</a> </p> </center>
該部分主要實現主介面的頭部資訊,顯示三個連結,分別可以跳轉到登陸介面,線上測試介面,以及成績榜介面
Footer.jsp
<%!int pageCount = 0;%> <% pageCount++; %> <center> <p>Copyright @ 2018 | 訪問次數:<%=pageCount%></p> </center>
該部分顯示登入頁面的底部資訊,即顯示訪問次數等其他資訊
Index.jsp
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <jsp:include page="Header.jsp" /> <center> <form action="check.jsp" method="get"> 使用者名稱<input type="text" name="username" /> <br> 密碼<input type="password" name="psd" /> <br><br> <button type="submit">登入</button> <button type="reset">重填</button> </center> <jsp:include page="Footer.jsp" /> </form> </body> </html>
該部分主要顯示登陸介面的中間介面,使用者可以輸入使用者名稱和密碼實現登陸系統
2、登陸檢測
當用戶點選登陸按鈕,系統後臺會獲取使用者輸入的使用者名稱以及密碼,並與預設的進行比對,由於本例沒有使用資料庫,所以使用Map儲存使用者名稱及密碼
<%! Map<String,String> userlist= new HashMap<String,String>(); %> <% userlist.put("qq","11"); userlist.put("ww","22"); userlist.put("ee","33"); %> <%! boolean check(String username,String psd){ if(userlist.containsKey(username)){ if(userlist.get(username).equals(psd)){ return true; } } return false; } %> <% String username=request.getParameter("username"); String psd=request.getParameter("psd"); if(check(username,psd)){ session.setAttribute("username",username); out.print("歡迎"+username); out.print("<a href='test.jsp'>開始測試</a>"); } else{ out.print("登陸失敗,3秒鐘後重新登入"); response.setHeader("refresh","3;url='Index.jsp'"); } %>
當用戶輸入的使用者名稱及密碼正確時,系統會顯示使用者姓名,以及跳轉連結,同時使用session儲存使用者名稱,密碼不正確時,3秒後返回登陸介面,
3、測試頁面
使用者輸入使用者名稱及密碼後便進入測試頁面,測試頁面的第一行顯示使用者名稱,之後顯示題目資訊。
<% String username=(String)session.getAttribute("username"); if(username==null){ out.print("未登陸,3秒鐘後重新登入"); response.setHeader("refresh","3;url='Index.jsp'"); } else{ %> 考生:<%=session.getAttribute("username") %> <h3>線上測試題</h3> <form action="submit.jsp" onsubmit="return confirm('確定提交嗎?')"> 第1題:湖北省會是 <input type="text" name="q1" /> <br><br> 第2題:宋朝開國皇帝是 <br> <input type="radio" value="趙匡胤" name="q2"> 趙匡胤 <input type="radio" value="朱元璋" name="q2"> 朱元璋 <input type="radio" value="李淵" name="q2"> 李淵 <br><br> 第3題:四大名著有 <br> <input type="checkbox" value="紅樓夢" name="q3"> 紅樓夢 <input type="checkbox" value="水滸傳" name="q3"> 水滸傳 <input type="checkbox" value="J2EE程式設計技術" name="q3"> J2EE程式設計技術 <br><br> <button type="submit">提交</button> </form> <%}%>
進入頁面之前,會再次檢測使用者是否登入,以防止使用者通過其他路徑訪問到該頁面。
點選提交時,系統會提示是否提交,點選確定後,系統後臺要做兩件事,第一件事就是登出session,另一件事就是通過答案獲取相應的分數,並且將使用者名稱和分數儲存。
4、提交頁面
使用者完成題目點選提交後,系統會獲取使用者的答案,並與標準答案對比,獲取相應的分數,同時使用application儲存使用者名稱和成績,這樣就可以在成績榜中顯示每個使用者的成績資訊
<%! Map<String,Integer> score_list = new HashMap<String,Integer>(); //存放使用者名稱+成績 %> <% int score=0; String q1=request.getParameter("q1"); String q2=request.getParameter("q2"); String[] q3=request.getParameterValues("q3"); if(q1!=null&&q1.equals("武漢")){ score+=10; } if(q2!=null&&q2.equals("趙匡胤")){ score+=10; } if(q3!=null&&q3.length==2&&q3[0].equals("紅樓夢")&&q3[1].equals("水滸傳")){ score+=10; } //out.print("<h2>你的成績=" + score + "</h2>"); score_list.put((String)session.getAttribute("username"),score); application.setAttribute("scorelist",score_list); response.sendRedirect("logout.jsp"); %>
5、成績榜
成績榜通過application顯示所有登陸使用者的使用者名稱及成績,並按照成績進行排序‘'
<h1>成績榜</h1> <%! //降序排序 public <K,V extends Comparable<? super V>> Map<K,V> sortByValueDescending(Map<K,V> map) { List<Map.Entry<K,V>> list = new LinkedList<Map.Entry<K,V>>(map.entrySet()); Collections.sort(list,new Comparator<Map.Entry<K,V>>() { public int compare(Map.Entry<K,V> o1,Map.Entry<K,V> o2) { int compare = (o1.getValue()).compareTo(o2.getValue()); return -compare; } }); Map<K,V> result = new LinkedHashMap<K,V>(); for (Map.Entry<K,V> entry : list) { result.put(entry.getKey(),entry.getValue()); } return result; } %> <% if(application.getAttribute("scorelist")==null){ out.print("<h3>沒有成績</h3>"); } else{ //遍歷顯示所有成績(Map遍歷) Map<String,Integer> score_list= (Map<String,Integer>)application.getAttribute("scorelist"); score_list=sortByValueDescending(score_list); Set s=score_list.keySet(); Iterator it=s.iterator(); while(it.hasNext()){ String username=(String)it.next(); int score=score_list.get(username); out.print("<h3>"+username+":"+score+"</h3>"); } } %>
6、完整流程
到此這篇關於使用JSP技術實現一個簡單的線上測試系統的例項詳解的文章就介紹到這了,更多相關JSP技術實現一個簡單的線上測試系統內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!