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

實(shí)現(xiàn):
本界面由三部分構(gòu)成,F(xiàn)ooter.jsp,Index.jsp,Header.jsp
Header.jsp
<center> <h2>在線測(cè)試系統(tǒng)</h2> <p> <a href="Index.jsp" rel="external nofollow" >登錄</a> | <a href="test.jsp" rel="external nofollow" >在線測(cè)試</a> | <a href="scorelist.jsp" rel="external nofollow" >成績(jī)榜</a> </p> </center>
該部分主要實(shí)現(xiàn)主界面的頭部信息,顯示三個(gè)鏈接,分別可以跳轉(zhuǎn)到登陸界面,在線測(cè)試界面,以及成績(jī)榜界面
Footer.jsp
<%!int pageCount = 0;%> <% pageCount++; %> <center> <p>Copyright @ 2018 | 訪問(wèn)次數(shù):<%=pageCount%></p> </center>
該部分顯示登錄頁(yè)面的底部信息,即顯示訪問(wèn)次數(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"> 用戶(hù)名<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>
該部分主要顯示登陸界面的中間界面,用戶(hù)可以輸入用戶(hù)名和密碼實(shí)現(xiàn)登陸系統(tǒng)
2、登陸檢測(cè)
當(dāng)用戶(hù)點(diǎn)擊登陸按鈕,系統(tǒng)后臺(tái)會(huì)獲取用戶(hù)輸入的用戶(hù)名以及密碼,并與預(yù)設(shè)的進(jìn)行比對(duì),由于本例沒(méi)有使用數(shù)據(jù)庫(kù),所以使用Map存儲(chǔ)用戶(hù)名及密碼
<%!
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'>開(kāi)始測(cè)試</a>");
}
else{
out.print("登陸失敗,3秒鐘后重新登錄");
response.setHeader("refresh", "3;url='Index.jsp'");
}
%>
當(dāng)用戶(hù)輸入的用戶(hù)名及密碼正確時(shí),系統(tǒng)會(huì)顯示用戶(hù)姓名,以及跳轉(zhuǎn)鏈接,同時(shí)使用session保存用戶(hù)名,密碼不正確時(shí),3秒后返回登陸界面,
3、測(cè)試頁(yè)面
用戶(hù)輸入用戶(hù)名及密碼后便進(jìn)入測(cè)試頁(yè)面,測(cè)試頁(yè)面的第一行顯示用戶(hù)名,之后顯示題目信息。
<%
String username=(String)session.getAttribute("username");
if(username==null){
out.print("未登陸,3秒鐘后重新登錄");
response.setHeader("refresh", "3;url='Index.jsp'");
}
else{
%>
考生:<%=session.getAttribute("username") %>
<h3>在線測(cè)試題</h3>
<form action="submit.jsp" onsubmit="return confirm('確定提交嗎?')">
第1題:湖北省會(huì)是
<input type="text" name="q1" />
<br><br>
第2題:宋朝開(kāi)國(guó)皇帝是
<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="紅樓夢(mèng)" name="q3">
紅樓夢(mèng)
<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)入頁(yè)面之前,會(huì)再次檢測(cè)用戶(hù)是否登錄,以防止用戶(hù)通過(guò)其他路徑訪問(wèn)到該頁(yè)面。
點(diǎn)擊提交時(shí),系統(tǒng)會(huì)提示是否提交,點(diǎn)擊確定后,系統(tǒng)后臺(tái)要做兩件事,第一件事就是注銷(xiāo)session,另一件事就是通過(guò)答案獲取相應(yīng)的分?jǐn)?shù),并且將用戶(hù)名和分?jǐn)?shù)保存。
4、提交頁(yè)面
用戶(hù)完成題目點(diǎn)擊提交后,系統(tǒng)會(huì)獲取用戶(hù)的答案,并與標(biāo)準(zhǔn)答案對(duì)比,獲取相應(yīng)的分?jǐn)?shù),同時(shí)使用application保存用戶(hù)名和成績(jī),這樣就可以在成績(jī)榜中顯示每個(gè)用戶(hù)的成績(jī)信息
<%!
Map<String, Integer> score_list = new HashMap<String, Integer>(); //存放用戶(hù)名+成績(jī)
%>
<%
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("紅樓夢(mèng)")&&q3[1].equals("水滸傳")){
score+=10; }
//out.print("<h2>你的成績(jī)=" + score + "</h2>");
score_list.put((String)session.getAttribute("username"), score);
application.setAttribute("scorelist", score_list);
response.sendRedirect("logout.jsp");
%>
5、成績(jī)榜
成績(jī)榜通過(guò)application顯示所有登陸用戶(hù)的用戶(hù)名及成績(jī),并按照成績(jī)進(jìn)行排序‘'
<h1>成績(jī)榜</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>沒(méi)有成績(jī)</h3>");
}
else{ //遍歷顯示所有成績(jī)(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ù)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的在線測(cè)試系統(tǒng)的實(shí)例詳解的文章就介紹到這了,更多相關(guān)JSP技術(shù)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的在線測(cè)試系統(tǒng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于jsp:included的使用與jsp:param亂碼的解決方法
本篇文章是對(duì)jsp:included的使用與jsp:param亂碼的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
jsp學(xué)習(xí)之scriptlet的使用方法詳解
這篇文章主要介紹了jsp學(xué)習(xí)之scriptlet的使用方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
JSP中c:foreach遍歷和s:iterator遍歷異同實(shí)例分析
這篇文章主要介紹了JSP中c:foreach遍歷和s:iterator遍歷異同,以?xún)蓚€(gè)完整實(shí)例形式對(duì)比分析了c:foreach遍歷和s:iterator遍歷的具體用法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
AJAX自學(xué)練習(xí) 無(wú)刷新從數(shù)據(jù)庫(kù)后臺(tái)取數(shù)據(jù)顯示
AJAX自學(xué)練習(xí) 無(wú)刷新從數(shù)據(jù)庫(kù)后臺(tái)獲取數(shù)據(jù)并顯示的代碼,大家可以參考下。2009-09-09
IIS6+TOMCAT整合,實(shí)戰(zhàn)實(shí)例!
IIS6+TOMCAT整合,實(shí)戰(zhàn)實(shí)例!...2007-02-02
jsp response.sendRedirect不跳轉(zhuǎn)的原因分析及解決
最近做項(xiàng)目時(shí)遇到一個(gè)問(wèn)題,明明加了response.sendRedirect() ,系統(tǒng)也執(zhí)行了,但是它就是不跳轉(zhuǎn),具體的原因如下,感興趣的各位可以參考下哈,希望對(duì)大家有所幫助2013-07-07
JSP實(shí)現(xiàn)登錄功能之添加驗(yàn)證碼
jsp登陸驗(yàn)證,網(wǎng)頁(yè)登陸驗(yàn)證帶驗(yàn)證碼校驗(yàn),登錄功能之添加驗(yàn)證碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2016-12-12
純JSP+DWR實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)下拉選擇菜單實(shí)現(xiàn)技巧
今天我做了一個(gè)dwr+jsp做的例子:純JSP+DWR實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)下拉選擇菜單,感興趣的朋友可以參考下,或許本文對(duì)你有所幫助2013-01-01
URL中允許攜帶sessionid帶來(lái)的安全隱患分析
很多WEB開(kāi)發(fā)語(yǔ)言為了防止瀏覽器禁止了cookie而無(wú)法識(shí)別用戶(hù),允許在URL中攜帶sessionid,這樣雖然方便,但卻有可能引起釣魚(yú)的安全漏洞。2010-10-10
搭建Eclipse+MyEclipse開(kāi)發(fā)環(huán)境
搭建Eclipse+MyEclipse開(kāi)發(fā)環(huán)境...2007-02-02

