Sara
Sara

Reputation: 9

How to move between JSP with NEXT and BACK buttons?

I work on an online examination prject using servlets and JSP. The Admin can add exams which contain questions with their options. The first JSP is when he adds the first question . it contains only the next button .The last JSP contains only the back button and others contain the next and back buttons. Each Exam has 5 questions. My problem is how to move between these JSP using the buttons. Here is my code, i don't know why it doesn't work

// Servlet code:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    int i=1;
    String button1=request.getParameter("back");
    String button2=request.getParameter("next");
    if(button1!=null){
        i=i-1;
    }
    if (button2!=null){
        i=i+1;
    }
    if(i==1 ){
        this.getServletContext().getRequestDispatcher("/WEB-INF/AddFirstQuestion.jsp").forward( request, response );
    }
    else if (i==5  ){
        this.getServletContext().getRequestDispatcher("/WEB-INF/AddLastQuestion.jsp").forward( request, response );

    }
    else if (i>1 && i<5){
        this.getServletContext().getRequestDispatcher("/WEB-INF/AddQuestion.jsp").forward( request, response );
    }

}

and there is a part of JSP code:

<input type="submit" name ="next" value="Suivant"  />

Upvotes: 0

Views: 410

Answers (1)

Alan Pallath
Alan Pallath

Reputation: 99

First of all, you're JSP code snippet isn't visible, Try making a form method and inside set to method as "post" . I'm not sure about using getServletRequest. Try using this one.

RequestDispatcher dispatcher = request.getRequestDispatcher("AddLastQuestion.jsp");
dispatcher.forward( request, response );

Also, you're not getting the "back" attribute. Just the next attribute is received for the servlet to process. Hence, the button2 will be always set to null. Try doing like this. JSP

<input type="submit" name ="next" value="Suivant"  />
<input type="submit" name ="back" value="Suivant"  />

On the server side, Servlet

if(i==1 ){
    RequestDispatcher dispatcher = request.getRequestDispatcher("AddLastQuestion.jsp");
dispatcher.forward( request, response );

}
else if (i==5  ){
    RequestDispatcher dispatcher = request.getRequestDispatcher("AddLastQuestion.jsp");
dispatcher.forward( request, response );


}

Upvotes: 1

Related Questions