简体   繁体   English

用JSP和Servlet发布问题?

[英]Post Issue with the JSP and Servlet?

I want to create a small quiz app in jsp which will ask the questions which are stored in the array. 我想在jsp中创建一个小的测验应用程序,它将询问存储在数组中的问题。 It should ask the ask until the last index of array. 它应该询问直到数组的最后一个索引。 Here answer is also stored in the array. 答案也存储在数组中。 What I have done so far is: 到目前为止,我所做的是:

Main Servlet 主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();
        }
    }

The Quiz model 测验模型

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];
    }

and the quiz.jsp file: 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>

In the first run it shows the view correctly but when I reload the page it start to show null in the currQues in the view is it a problem in session if yes then where i have done wrong can anyone please explain it to me thank you!! 在第一次运行中,它正确显示了视图,但是当我重新加载页面时,它开始在视图中的currQu中显示null 。如果是的话,会话是否存在问题,那么我做错了任何人都可以向我解释一下,谢谢! !

When you reload the page doGet method will work, you are setting question if the session is newly created and you put it in request. 当您重新加载页面的doGet方法将起作用时,您正在设置问题,是否新创建了会话并将其置于请求中。 So when you reload your page session is not newly created and your currentQuestion is not set. 因此,当您重新加载页面时,不是新创建的页面会话,也没有设置currentQuestion。 You schould add an else case and assign the question or you should put these variables also in your session and retrieve it from the session in your jsp. 您应该添加else大小写并分配问题,或者也应将这些变量也放入会话中,然后从jsp的会话中检索出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM