我正在尝试创建一个多选html页面,需要使用jsp来查看结果。但是,我似乎无法弄清楚如何在点击提交按钮后获取jsp文件以获得最终得分结果。 (例如,你有4个问题_正确_%)我可以设置html文件就好了。我究竟做错了什么?感谢。
下面的是一个带有html代码“SampleQuiz.html”
的示例页面<html>
<head>
<title>Multiple-Choice Quiz Example</title>
</head>
<h2>Sample Multiple-Choice Quiz</h2>
<form action="quizresults.jsp" method="get">
<h3>Q1. What color is the sky on a sunny day?</h3>
<input type="radio" name="answer1" value="blue" checked="checked">blue<br>
<input type="radio" name="answer1" value="red">red<br>
<input type="radio" name="answer1" value="green">green<br>
<h3>Q2. The sum of two and three equals...</h3>
<input type="radio" name="answer2" value="seven">seven<br>
<input type="radio" name="answer2" value="six">six<br>
<input type="radio" name="answer2" value="five" checked="checked" >five<br>
<h3>Q3. Which of these animals is NOT an mammal.</h3>
<input type="radio" name="answer3" value="lizard" checked="checked">lizard<br>
<input type="radio" name="answer3" value="monkey">monkey<br>
<input type="radio" name="answer3" value="dog">dog<br>
<h3>Q4. Santa Claus is most associated with this holiday.</h3>
<input type="radio" name="answer4" value="Halloween">Halloween<br>
<input type="radio" name="answer4" value="Christmas"checked="checked">Christmas<br>
<input type="radio" name="answer4" value="Fourth of July">Forth of July<br>
<br>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
</body>
</html>
更新:这是我对jsp的第二次尝试。我还将html更改为以下答案的建议。
下面是我尝试过的jsp代码“quizresults.jsp”
<html>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Final Quiz Score</title>
</head>
<body>
<h1>Here are your results</h1>
<%
if(request.getParameter("answer") != null) {
if(request.getParameter("answer").equals("answer1")) {
out.println("Question 1 was correct<br>");
}
else {
out.println("Question 1 was incorrect. Try again.<br>");
}
if(request.getParameter("answer").equals("answer2")) {
out.println("Question 2 was answered.<br>");
}
else {
out.println("Question 2 was incorrect. Try again.<br>");
}
if(request.getParameter("answer").equals("answer3")) {
out.println("Question 3 was answered.<br>");
}
else {
out.println("Question 3 was incorrect. Try again.<br>");
}
if(request.getParameter("answer").equals("answer4")) {
out.println("Question 4 was correct.<br>");
}
else {
out.println("Question 4 was incorrect. Try again.<br>");
}
}
%>
<form action="SampleQuiz.html" method="post">
<input type="submit" value="Return">
</form>
</body>
</html>
答案 0 :(得分:1)
是否正确检查答案你必须替换
if(request.getParameter("answer").equals("answer1")) {
out.println("Question 1 was correct<br>");
用这个
if(request.getParameter("answer1").equals("blue")) {
out.println("Question 1 was correct<br>");
并且我也没有看到您从html传递的任何答案参数,您正在使用jsp这样的空格检查
if(request.getParameter("answer") != null)
我认为您不必使用此空检查,因为您默认选中了每个问题中的一个单选按钮,因此它始终会传递您选中的值。
答案 1 :(得分:0)
您似乎不了解表单和单选按钮的工作原理。
首先,作为参数发送的是名称/值对:名称是<input>
个名称,值是<input>
值。对于单选按钮,仅提交已检查的无线电名称和值。
由于您的所有无线电都具有相同的名称,因此只能同时检查其中一个,因此for只会提交一个参数:answer=<the value of the unique checked radio button>
。
您的服务器端代码(实际上应该在servlet中而不是在scriptlet中)没有意义:您正在检查名为“questions”的参数的值。但是没有输入具有此名称,因此此参数的值将始终为null。
您需要的是为每组单选按钮指定一个不同的名称:
<h3>Q1. What color is the sky on a sunny day?</h3>
<input type="radio" name="answer1" value="blue" checked="checked">blue<br>
<input type="radio" name="answer1" value="red">red<br>
<input type="radio" name="ansser1" value="green">green<br>
<h3>Q2. The sum of two and three equals...</h3>
<input type="radio" name="answer2" value="seven">seven<br>
<input type="radio" name="answer2" value="six">six<br>
<input type="radio" name="answer2" value="five" checked="checked" >five<br>
然后,在服务器端,您可以使用
获取第一个问题的检查答案request.getParameter("answer1"); // null if nothing checked, blue, red or green otherwise
同样,要获得第二个问题的检查答案:
request.getParameter("answer2"); // null if nothing checked, seven, six or five otherwise