使用JSP技術(shù)實現(xiàn)一個簡單的在線測試系統(tǒng)的實例詳解
1、登陸界面

實現(xiàn):
本界面由三部分構(gòu)成,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)主界面的頭部信息,顯示三個鏈接,分別可以跳轉(zhuǎn)到登陸界面,在線測試界面,以及成績榜界面
Footer.jsp
<%!int pageCount = 0;%> <% pageCount++; %> <center> <p>Copyright @ 2018 | 訪問次數(shù):<%=pageCount%></p> </center>
該部分顯示登錄頁面的底部信息,即顯示訪問次數(shù)等其他信息
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、登陸檢測
當(dāng)用戶點擊登陸按鈕,系統(tǒng)后臺會獲取用戶輸入的用戶名以及密碼,并與預(yù)設(shè)的進(jìn)行比對,由于本例沒有使用數(shù)據(jù)庫,所以使用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'");
}
%>
當(dāng)用戶輸入的用戶名及密碼正確時,系統(tǒng)會顯示用戶姓名,以及跳轉(zhuǎn)鏈接,同時使用session保存用戶名,密碼不正確時,3秒后返回登陸界面,
3、測試頁面
用戶輸入用戶名及密碼后便進(jìn)入測試頁面,測試頁面的第一行顯示用戶名,之后顯示題目信息。
<%
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編程技術(shù)" name="q3">
J2EE編程技術(shù)
<br><br>
<button type="submit">提交</button>
</form>
<%}%>
進(jìn)入頁面之前,會再次檢測用戶是否登錄,以防止用戶通過其他路徑訪問到該頁面。
點擊提交時,系統(tǒng)會提示是否提交,點擊確定后,系統(tǒng)后臺要做兩件事,第一件事就是注銷session,另一件事就是通過答案獲取相應(yīng)的分?jǐn)?shù),并且將用戶名和分?jǐn)?shù)保存。
4、提交頁面
用戶完成題目點擊提交后,系統(tǒng)會獲取用戶的答案,并與標(biāo)準(zhǔn)答案對比,獲取相應(yīng)的分?jǐn)?shù),同時使用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顯示所有登陸用戶的用戶名及成績,并按照成績進(jìn)行排序‘'
<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、完整流程




到此這篇關(guān)于使用JSP技術(shù)實現(xiàn)一個簡單的在線測試系統(tǒng)的實例詳解的文章就介紹到這了,更多相關(guān)JSP技術(shù)實現(xiàn)一個簡單的在線測試系統(tǒng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于jsp:included的使用與jsp:param亂碼的解決方法
本篇文章是對jsp:included的使用與jsp:param亂碼的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
jsp學(xué)習(xí)之scriptlet的使用方法詳解
這篇文章主要介紹了jsp學(xué)習(xí)之scriptlet的使用方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
JSP中c:foreach遍歷和s:iterator遍歷異同實例分析
這篇文章主要介紹了JSP中c:foreach遍歷和s:iterator遍歷異同,以兩個完整實例形式對比分析了c:foreach遍歷和s:iterator遍歷的具體用法,具有一定參考借鑒價值,需要的朋友可以參考下2015-09-09
AJAX自學(xué)練習(xí) 無刷新從數(shù)據(jù)庫后臺取數(shù)據(jù)顯示
AJAX自學(xué)練習(xí) 無刷新從數(shù)據(jù)庫后臺獲取數(shù)據(jù)并顯示的代碼,大家可以參考下。2009-09-09
jsp response.sendRedirect不跳轉(zhuǎn)的原因分析及解決
最近做項目時遇到一個問題,明明加了response.sendRedirect() ,系統(tǒng)也執(zhí)行了,但是它就是不跳轉(zhuǎn),具體的原因如下,感興趣的各位可以參考下哈,希望對大家有所幫助2013-07-07
純JSP+DWR實現(xiàn)三級聯(lián)動下拉選擇菜單實現(xiàn)技巧
今天我做了一個dwr+jsp做的例子:純JSP+DWR實現(xiàn)三級聯(lián)動下拉選擇菜單,感興趣的朋友可以參考下,或許本文對你有所幫助2013-01-01
搭建Eclipse+MyEclipse開發(fā)環(huán)境
搭建Eclipse+MyEclipse開發(fā)環(huán)境...2007-02-02

