如何使用“下一步”和“上一步”按钮在JSP之间移动?
我使用servlet和JSP处理一个在线考试项目。管理员可以添加包含问题及其选项的考试。第一个JSP是当他添加第一个问题时。它只包含next按钮,最后一个JSP只包含back按钮,其他JSP则包含next和back按钮。每次考试有5道题。我的问题是如何使用按钮在这些JSP之间移动。这是我的代码,我不知道为什么它不起作用如何使用“下一步”和“上一步”按钮在JSP之间移动?,jsp,servlets,Jsp,Servlets,我使用servlet和JSP处理一个在线考试项目。管理员可以添加包含问题及其选项的考试。第一个JSP是当他添加第一个问题时。它只包含next按钮,最后一个JSP只包含back按钮,其他JSP则包含next和back按钮。每次考试有5道题。我的问题是如何使用按钮在这些JSP之间移动。这是我的代码,我不知道为什么它不起作用 // Servlet code: protected void doPost(HttpServletRequest request, HttpServletResponse re
// 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 );
}
}
还有一部分JSP代码:
<input type="submit" name ="next" value="Suivant" />
首先,如果JSP代码片段不可见,请尝试创建一个表单方法,并将其内部设置为post方法。我不确定是否使用getServletRequest。试着用这个
RequestDispatcher dispatcher = request.getRequestDispatcher("AddLastQuestion.jsp");
dispatcher.forward( request, response );
此外,您不会获得back属性。只接收servlet要处理的下一个属性。因此,按钮2将始终设置为空。
试着这样做。JSP
<input type="submit" name ="next" value="Suivant" />
<input type="submit" name ="back" value="Suivant" />
JSP是一种服务器端技术,不知道在浏览器工具栏上单击了哪个按钮。最接近的方法是使用javascript获取单击的按钮,并将其作为请求参数传递给jsp页面——但为什么会有人这样做呢?
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 );
}