开发一个简单的考试系统,在HTML页面中建立一个表单,通过post方法传递参数。题目类型包括单选题、多选题和填空题,要求程序给出考试成绩。
<!DOCTYPE html>
<html>
<head>
<title>question.html</title>
<meta name="content-type" content="text/html" charset=UTF-8>
</head>
<body>
<a>请回答下面问题: </a>
<form action="" method="post">
<table>
<tr><td>1.Windows操作系统是那家公司产品?
<input type="radio" name="cooperation" value="apple">apple公司
<input type="radio" name="cooperation" value="ibm">IBM公司
<input type="radio" name="cooperation" value="microsoft">Microsoft公司
</td></tr>
<tr><td>2.下列设计语言,那些事面向对象的?
<input type="checkbox" name="language" value="java">java语言
<input type="checkbox" name="language" value="c">c语言
<input type="checkbox" name="language" value="c++">c++语言
</td></tr>
<tr><td>3.编写Servlet程序应该继承那个类?
<input type="text" name="impClass" size="20' vlaue=" ">
</td></tr>
<tr><td>4.下图是那种编程语言的图标?
<input type="checkbox" name="language" value="c++">c++
<input type="checkbox" name="language" value="python">python
<input type="checkbox" name="language" value="java">java
<br>
<img src="https://img0.baidu.com/it/u=1302690523,1507510661&fm=253&fmt=auto&app=138&f=PNG?w=989&h=500">
</td></tr>
<td>
交卷请单击<input type="submit" name="submit" value="提交">
重新交卷请单击<input type="reset" name="reset" value="重置">
</td>
</table>
</form>
</body>
</html>
package com.demo;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = "/quetionForm")
public class ExamServlet extends HttpServlet{
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
PrintWriter out = resp.getWriter();
String cooperation = req.getParameter("cooperation");
// out.println("1 "+cooperation);
String language1 = req.getParameter("language1");
String language2 = req.getParameter("language2");
String language3 = req.getParameter("language3");
// out.println(" 2 "+language1);
String impClass = req.getParameter("impClass");
// out.println(" 3 "+impClass);
String languagelogo = req.getParameter("languagelogo");
// out.println("4 "+languagelogo);
int sum = 0;
try {
if(cooperation.equals("Microsoft"))
sum += 25;
if(language1.equals("java") && language3.equals("c++") ){
sum += 25;
if(language2 != null)
sum -= 25;
}
if(impClass.equals("HttpServlet"))
sum += 25;
if(languagelogo.equals("java"))
sum += 25;
} catch (Exception e) {
out.print("请选择正确的答案");
}
// out.println("cooperation ==> "+cooperation +"\n"+
// "language ==> "+ language + "\n"+
// "impClass ==> "+ impClass + "\n"+
// "languagelogo ==> "+ languagelogo + "\n"
// + "sum = "+sum);
out.println("<!DOCTYPE>");
out.println("<HTML>");
out.println(" <HEAD><TITLE>result</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" ");
out.println("result == " +sum);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}