使用JSP技術實現(xiàn)一個簡單的在線測試系統(tǒng)的實例詳解
1、登陸界面
實現(xiàn):
本界面由三部分構成,F(xiàn)ooter.jsp,Index.jsp,Header.jsp
Header.jsp
<center> <h2>在線測試系統(tǒng)</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>
該部分主要實現(xiàn)主界面的頭部信息,顯示三個鏈接,分別可以跳轉到登陸界面,在線測試界面,以及成績榜界面
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>
該部分主要顯示登陸界面的中間界面,用戶可以輸入用戶名和密碼實現(xiàn)登陸系統(tǒng)
2、登陸檢測
當用戶點擊登陸按鈕,系統(tǒng)后臺會獲取用戶輸入的用戶名以及密碼,并與預設的進行比對,由于本例沒有使用數據庫,所以使用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"");} %>
當用戶輸入的用戶名及密碼正確時,系統(tǒng)會顯示用戶姓名,以及跳轉鏈接,同時使用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> <%}%>
進入頁面之前,會再次檢測用戶是否登錄,以防止用戶通過其他路徑訪問到該頁面。
點擊提交時,系統(tǒng)會提示是否提交,點擊確定后,系統(tǒng)后臺要做兩件事,第一件事就是注銷session,另一件事就是通過答案獲取相應的分數,并且將用戶名和分數保存。
4、提交頁面
用戶完成題目點擊提交后,系統(tǒng)會獲取用戶的答案,并與標準答案對比,獲取相應的分數,同時使用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技術實現(xiàn)一個簡單的在線測試系統(tǒng)的實例詳解的文章就介紹到這了,更多相關JSP技術實現(xiàn)一個簡單的在線測試系統(tǒng)內容請搜索以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持!
