下一步按钮将生成一组新问题

时间:2016-11-23 00:37:09

标签: java jsp servlets

我正在尝试使用jsp和servlet创建在线测验系统。 当用户选择主题时,它会转到第一个问题。在用户选择他们的选择并点击下一个按钮后,它应该显示一组新的问题和选择。我不知道该怎么做。到目前为止,这是我的代码:

数据库:

public static MathQuestion getQuestionInfo(int questionNum)
    {
        MathQuestion math = new MathQuestion();
      //  int questionNum = 1;
        try
        {
            Connection conn = DBConnection.getConnection();

            String query = "SELECT * FROM MathQuestions where QuestionNumber = ?";
            PreparedStatement stmt = conn.prepareStatement(query);
            stmt.setInt(1, questionNum);
        //    questionNum++;
            ResultSet rs = stmt.executeQuery();

            while(rs.next())
            {
                math.setQuestionNum(rs.getInt("QuestionNumber"));
                math.setQuestion(rs.getString("Question"));

                String questionOptions = rs.getString("QuestionOptions");
                String[] splittedValues = null;
                for(int i = 0; i < 4; i++)
                {
                   splittedValues = questionOptions.split(",");
                }
                math.setQuestionOptions(splittedValues);
                math.setCorrectAnswer("CorrectAnswer");
            }
        } 
        catch (Exception e)
        {
            System.out.println(e);
        }

        return math;
    }

JSP

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Math</title>
    </head>
    <body>
        <h1>Math Section</h1>

        <%
            String email = (String) session.getAttribute("email");
            int questionNum = (Integer) request.getAttribute("questionNum");
            MathQuestion math = new MathQuestion();

            if(email != null)
            {
                math = MathService.getQuestionInfo(questionNum);
            }

            String[] questionOptions = math.getQuestionOptions();
        %>

        <form name="MathForm" action="Math" method="post">
            Question <input type="text" value="<%=math.getQuestionNum()%>"> <br/>
            <input type="text" value="<%=math.getQuestion()%>"> <br/>

            <input type="radio" name="options" value="OptionA"><%=questionOptions[0]%> <br/> 
        <input type="radio" name="options" value="OptionB"><%=questionOptions[1]%> <br/>
            <input type="radio" name="options" value="OptionC"><%=questionOptions[2]%> <br/>    
            <input type="radio" name="options" value="OptionD"><%=questionOptions[3]%> <br/>

            <input type="button" name="Next" value="Next">
        </form>
    </body>
</html>

的Servlet

@WebServlet(name = "Math", urlPatterns =
{
    "/Math"
})
public class Math extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        if (SessionService.validateSession(request, response))
        {
            RequestDispatcher dispatcher = request.getRequestDispatcher("math-page.jsp");
            request.setAttribute("questionNum", 1);
            dispatcher.forward(request, response);
        }
        else
        {
            RequestDispatcher dispatcher = request.getRequestDispatcher("login-page.jsp");
            dispatcher.forward(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        for(int i = 2; i <= 10; i++)
        {
             RequestDispatcher dispatcher = request.getRequestDispatcher("math-page.jsp");
             request.setAttribute("questionNum", i);
             dispatcher.forward(request, response);
        }

        String userChoice = request.getParameter("options");
    }
}

0 个答案:

没有答案