JSP - 从jsp测验应用程序中获取分数

时间:2012-05-07 15:05:58

标签: java database jsp get

您好我正在jsp中创建一个测验应用程序,并且每个问题都使用不同的jsp页面。我想在每个问题得到解答后保持得分。我的问题是我从数据库中挑选随机答案,其中也是正确的答案。由于我无法猜出哪个字母会出现正确的答案,你能否建议我应该做些什么?我发布第一个和第二个问题jsp,让你知道我是怎么做的:

q1.jsp

<%@page import="java.util.Random"%>
<%@page import="java.util.ArrayList"%>
<%@page import="org.me.jsp.beans.WordBean"%>
<%@page import="java.util.List"%>
<!--This JSP acts as the first question in a multiple choice quiz, with the user
asked to submit their answer to the question-->

<?xml version = "1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@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">
<jsp:useBean id = "wordData" scope = "request"
             class = "org.me.jsp.beans.WordDataBean" />
<html>
    <head>
        <title>Big Java Quiz, question 1</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <%if (request.getParameter("choice").equals("N")) {
                out.print("<meta http-equiv='refresh' content='0;url=options.jsp'/>");
            }
            //Redirects user back to index if they did not want to take quiz%>
        <form action="q2.jsp" method="POST">
            <%
                List<WordBean> wordList = wordData.getWordList();
                List<String> answersList = new ArrayList<String>();
                Random random = new Random();
                Random forAnswers = new Random();

                WordBean goodOne = wordList.get(random.nextInt(wordList.size()));
                //take it out from the list
                wordList.remove(goodOne);
                //add it to the answers list
                answersList.add(goodOne.getGermanName());
                WordBean fakeOne = wordList.get(random.nextInt(wordList.size()));
                //take it out from the list
                wordList.remove(fakeOne);
                //add it to the answers list
                answersList.add(fakeOne.getGermanName());
                WordBean fakeTwo = wordList.get(random.nextInt(wordList.size()));
                //take it out from the list
                wordList.remove(fakeTwo);
                //add it to the answers list
                answersList.add(fakeTwo.getGermanName());

            %>What is the English word for the German word <%=goodOne.getEnglishName()%>?<br>

                <%
                    char letter = 'A';
                    for (String answer : answersList) {
                %>
                <input type="radio" name="q1Answer" value=""/><label for="<%=letter%>"><%=letter%>)<%=answersList.get(forAnswers.nextInt(3))%>  /> 
                    <% //point to the next letter
                            letter++;
                        }
                        %>
                    <input type="submit" value="Submit"/>
                    </form>
                    </body>
                    </html>

q2.jsp

<%-- 
    Document   : q2
    Created on : 06-May-2012, 18:54:32
    Author     : encore
--%>

<!--This JSP acts as the second question in a multiple choice quiz, with the user
asked to submit their answer to the question-->

<?xml version = "1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<%@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>
        <title>Big Java Quiz, question 2</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <%int score = 0;
        if(request.getParameter("q1Answer").equals("C"))
            score++; //Increments score if answer submitted was correct%>
        <form action="q3.jsp" method="POST">
            Your current score is: <%out.print(score);%>/20
            <input type="hidden" name="q2Score" value="<%out.print(score);%>"/>
            <!--Hidden button allows score to be accessed by next JSP-->
            <b>Question 2.</b> When an exception is generated it is said to have been _________?<br/><br/>
            <input type="radio" name="q2Answer" value="A"/><label for="A">A) Built</label><br/>
            <input type="radio" name="q2Answer" value="B"/><label for="B">B) Thrown</label><br/>
            <input type="radio" name="q2Answer" value="C"/><label for="C">C) Caught</label><br/>
            <input type="radio" name="q2Answer" value="D"/><label for="D">D) Detected</label><br/><br/>
            <input type="submit" value="Submit"/>
        </form>
    </body>
</html>

注意:第二个问题的形式显然会改为别的,目前我专注于获得分数。

1 个答案:

答案 0 :(得分:1)

在Q1的页面中,你应该知道正确的答案,把它放在会话中,然后在下一页中检索它。