发布JSP和Servlet的问题?

时间:2016-03-10 20:38:22

标签: java jsp servlets

我想在jsp中创建一个小测验应用程序,它将询问存储在数组中的问题。它应该询问问题直到数组的最后一个索引。这里的答案也存储在数组中。到目前为止我所做的是:

主要Servlet

@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        try (PrintWriter out = response.getWriter()) {
            session = request.getSession(true);
            Quiz quiz = new Quiz();
            if (session.isNew()) {

                quiz.setTotalCorrectAnswers(0);
                quiz.setCounter(0);
                question = quiz.getNextQuestion(0);
                answer = quiz.getAnswer(0);
            }

            session.setAttribute("quizes", quiz);
            request.setAttribute("quiz", quiz);
            request.setAttribute("currentQuestion", question);
            request.setAttribute("error", false);
            RequestDispatcher view = request.getRequestDispatcher("quiz.jsp");
            view.forward(request, response);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try (PrintWriter out = response.getWriter()) {
            String userAnswer = request.getParameter("txtAnswer");
            Quiz quiz = (Quiz) session.getAttribute("quizes");
            boolean error = false;

            if (userAnswer.equals(quiz.getAnswer(quiz.getCounter()))) {
                totalQuestions++;
                quiz.setTotalCorrectAnswers(totalQuestions);
                quiz.setCounter(quiz.getCounter() + 1);
                session.setAttribute("nextQuestion", quiz.getCounter() + 1);
            } else {
                quiz.setTotalCorrectAnswers(totalQuestions);
                quiz.setCounter(quiz.getCounter());
                error = true;
            }
            session.setAttribute("quizes", quiz);

            if(quiz.getTotalCorrectAnswers()>=5){
                session.invalidate();
                generateQuizOverPage(out);
            }else{
                request.setAttribute("quiz", quiz);
                request.setAttribute("currentQuestion", quiz.getNextQuestion(quiz.getCounter()));
                request.setAttribute("error", error);
                RequestDispatcher view = request.getRequestDispatcher("quiz.jsp");view.forward(request, response);

            }            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

测验模型

String[] numberSeries = {
        "3, 1, 4, 1, 5, ",
        "1, 1, 2, 3, 5, ",
        "1, 4, 9, 16, 25, ",
        "2, 3, 5, 7, 11, ",
        "1, 2, 4, 8, 16, "
    };

    String[] answer = {"9", "8", "36", "13", "32"};

    private int counter;
    private boolean isCorrect;
    private int totalCorrectAnswers;

    public Quiz() {
        counter = 0;
        isCorrect = false;
        totalCorrectAnswers = 0;
    }

    public int getCounter() {
        return counter;
    }

    public void setCounter(int counter) {
        this.counter = counter;
    }

    public boolean isIsCorrect() {
        return isCorrect;
    }

    public void setIsCorrect(boolean isCorrect) {
        this.isCorrect = isCorrect;
    }

    public int getTotalCorrectAnswers() {
        return totalCorrectAnswers;
    }

    public void setTotalCorrectAnswers(int totalCorrectAnswers) {
        this.totalCorrectAnswers = totalCorrectAnswers;
    }

    public String getNextQuestion(int index) {
        return numberSeries[index];
    }

    public String getAnswer(int index) {
        return answer[index];
    }

quiz.jsp 文件:

<%@page import="com.app.numberquiz.models.Quiz"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% 
    Quiz quiz = (Quiz) request.getAttribute("quiz"); 
    String currQuest = (String) request.getAttribute("currentQuestion");
    Boolean error = (Boolean) request.getAttribute("error");
%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form method='post'>
        <h3>Have fun with NumberQuiz!</h3>
        <p>Your current score is: 
        <% out.print(quiz.getTotalCorrectAnswers()); %>  </br></br>
        <p>Guess the next number in the sequence! </p>
        <p>["<% out.print(currQuest); %><span style='color:red'>?</span>"]</p>

        <p>Your answer:<input type='text' name='txtAnswer' value='' /></p>


        <% if (error) {  %>
            <p style='color:red'>Your last answer was not correct! Please try again</p>
        <% } %>
        <p><input type='submit' name='btnNext' value='Next' /></p>

        </form>
    </body>
</html>

在第一次运行中,它正确显示了视图,但是当我重新加载页面时,它开始在视图中的 currQues 中显示 null ,这是会话中的问题是的,那么我做错了可以请任何人向我解释,谢谢!!

1 个答案:

答案 0 :(得分:0)

当您重新加载页面时,doGet方法将起作用,如果会话是新创建的并且您将其置于请求中,则设置问题。因此,当您重新加载页面会话时,不会重新创建会话,并且您的currentQuestion未设置。你可以添加一个else案例并分配问题,或者你也应该将这些变量放在你的会话中,并从jsp中的会话中检索它。