写的很浅,但养成习惯吧。
本系统呢主要是做了一个是测验,留言,查看测验过的人的分数信息以及用word导出信息的功能;
由于代码较长就没贴全部代码,文末会给百度云链接,有兴趣的朋友可以去看看。
接下来说说主要功能(不包含HTML代码)
首先是主界面,id和非空验证

//非空验证
if(name.isEmpty() || strId.isEmpty()){ //表单提交必定有值,不为null
application.setAttribute("error", "1");
%> <jsp:forward page="error.jsp"></jsp:forward>
<%
}else{
for(int i = 0 ; i < aryId.length - 1 ; i ++ ){
if(aryId [ i ].equals(strId) ){ //学号验证,
%> <jsp:forward page="showTest.jsp"></jsp:forward>
<% }
}
application.setAttribute("error", "2"); //不等于就更改error并跳转
%><jsp:forward page="error.jsp"></jsp:forward>
<% }
%>其次是测试,包括网站访客人数,访问时间,提交数据时做验证

//访客
Integer visitor = (Integer)application.getAttribute("visitor");
if(visitor==null)
visitor=1;
else
visitor++;
application.setAttribute("visitor", visitor);
<%//接收数据并处理
String sq5 = "";
//多选题
String [ ] q5 = request.getParameterValues("ques5"); //dai优化
if(q5 == null){
application.setAttribute("error" , "3" );
%><jsp:forward page="error.jsp"></jsp:forward>
<% }
for(int i = 0 ; i < q5.length ; i ++){ //将数组遍历
sq5 += q5 [ i ] ;
}
//提交的答案
String [ ] answer = {
request.getParameter("ques1"),
request.getParameter("ques2"),
request.getParameter("ques3"),
request.getParameter("ques4"),
sq5
};
application.setAttribute("answer", answer);
//判断非空
for(int i = 0 ; i <= answer.length - 1 ; i++){
if( answer[ i ] == null ){
application.setAttribute("error", "3");
%> <jsp:forward page="error.jsp"></jsp:forward>
<% }
}
%>
<jsp:forward page="logicScore.jsp"></jsp:forward>
接着是计算得分、点评及留言页面

<%
int total = 0 ; //总分
String showAns = ""; //显示提交的答案
String showSAns = "";
String comment = ""; //点评
request.setCharacterEncoding("utf-8");
String [ ] answer = (String [ ] )application.getAttribute("answer");
String [ ] aryAns = (String [ ] )application.getAttribute("aryAnswer"); //标准答案
//计算成绩
for(int i = 0 ; i <= aryAns.length - 1 ; i++){
if(aryAns[ i ].equals(answer[ i ])){
total+= 20 ;
}
if( answer[ i ] == null ){
application.setAttribute("error", "3");
%> <jsp:forward page="error.jsp"></jsp:forward>
<% }
showAns += ( i +1 ) + "-" + answer[ i ] + "\t";
showSAns += (i + 1) + "-" + aryAns[ i ] + "\t";
}
//给点评
if(total <= 60){
comment = "哎哟不错哦!";
} else if (total <= 90 ){
comment = "路在脚下";
}else{
comment = "你还有远方";
}
String strTotal = total + ""; //设置application对象
application.setAttribute("total", strTotal.trim());
application.setAttribute("showAns", showAns);
application.setAttribute("showSAns",showSAns);
application.setAttribute("comment",comment);
%>
<jsp:forward page="showScore.jsp"></jsp:forward>然后是查看留言部分和查看测试信息页面,都是用了一个Vector集合放数据再遍历


<%
request.setCharacterEncoding("utf-8");
String content = request.getParameter("mess"); //取值乱码,form提交方式 出错
String time = new Date().toLocaleString();
String name = (String)application.getAttribute("name");
if(content.isEmpty() || content == null){
application.setAttribute("error", "4");
%> <jsp:forward page = "error.jsp" ></jsp:forward>
<% }
Vector<String> messV = (Vector<String>)application.getAttribute("messV"); //create Vector<String>
if(messV ==null )//如果没有集合则创建
messV =new Vector<String>();
String str="<tr><td>"+ name +"</td><td>"+time+"</td><td><textarea readonly = 'readonly' >"+content+"</textarea></td></tr>";
messV.add(str);
application.setAttribute("messV", messV); //set vector
response.sendRedirect("showMess.jsp");//使用forwar刷新会出现重复
%>
还有一个导出word,excle,关于显示乱码,更改编码即可

<%//刷新自动增加,bug,已解决
Vector <String> v = (Vector<String>)application.getAttribute("testV");
%>
<table border = "1" name = "testInfo">
<tr><td>班级</td><td>学号</td><td>姓名</td><td>成绩</td></tr>
<%
Iterator it = v.iterator();
while(it.hasNext()){
out.print(it.next());
}
/*清除测验信息
v.clear();
application.setAttribute("testV",v);
*/
%>
</table>
<%
request.setCharacterEncoding("utf-8");
String t = request.getParameter("type");
if(t != null ){
if(t.equals("0")){
response.setContentType("application/msword;charset=utf-8");
response.setHeader("Content-disposition","attachment; filename=print.doc");
}else if (t.equals("1")){
response.setContentType("application/msexcel;charset=utf-8");
response.setHeader("Content-disposition","attachment; filename=print.xls");
}
}else{
application.setAttribute("error", "5");
%> <jsp:forward page="error.jsp"></jsp:forward>
<% }
%>
最后是error界面,各种错误都会跳转到该页面,然后提示,截图带过

哈!下面是链接
云盘链接: https://pan.baidu.com/s/1OSKaFTljTbiJdCYnlY9YYw 密码: fq9k
这是一个简单的JSP测试系统,具备测验、留言、成绩查看和信息导出(Word)等功能。代码部分未完全展示,提供百度云链接供下载查看。系统包含主界面的ID和非空验证、测验及访客统计、得分计算与点评、留言查看、信息导出(解决乱码问题)以及错误处理页面。

3327

被折叠的 条评论
为什么被折叠?



