I have a problem creating one application. For example I have a List or Array of string lines - these are questions. I have a servlet and JSP page, in JSP page there is a button "next" a one question has to be printed in JSP at a time when next button is pressed.
Please give me some advise how can I make this work.
I have a struts action class in which i have my code to place the question:
public class SecondWriteAction extends Action { /** * */ private static final String SUCCESS = "success"; /** * */ protected int count = 0; /** * */ protected Collection<QuestionsBean> questionsBeansCollection = new ArrayList<QuestionsBean>(); /** * This is the action called from the Struts framework. * @param mapping The ActionMapping used to select this instance. * @param form The optional ActionForm bean for this request. * @param request The HTTP Request we are processing. * @param response The HTTP Response we are processing. * @throws java.lang.Exception * @return */ @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { HttpSession session = request.getSession(); if (session.getAttribute("questioningTimeCount") == null){ session.setAttribute("questioningTimeCount", request.getParameter("timeCount")); } int timeCount = Integer.parseInt(session.getAttribute("questioningTimeCount").toString()); WriteFormBean formBean = (WriteFormBean) form; QuestionsBean questionsBean = new QuestionsBean(); questionsBean.setSentence(formBean.getQuestion()); questionsBeansCollection.add(questionsBean); if (session.getAttribute("countFromJsp") != null) { int countFromJsp = Integer.parseInt(session.getAttribute("countFromJsp").toString()); if (countFromJsp == 1){ session.setAttribute("questionsBeansCollection", questionsBeansCollection); } if (countFromJsp <= 0) { questionsBeansCollection.clear(); questionsBeansCollection = new ArrayList<QuestionsBean>(); count = 0; } } QuestionsHandler handler = QuestionsHandler.getInstance(request.getSession().getServletContext().getRealPath("/data/sentences_"+ timeCount)); count = count + 1; request.setAttribute("question", handler.getQuestions().get(count)); request.setAttribute("count", count); RequestDispatcher rd = request.getRequestDispatcher("/student/secondWrite.jsp"); rd.forward(request, response); return mapping.findForward(SUCCESS); }}
In JSP page I count them and when questioning is finished I am trying to forward to ohter JSP page.
<% if (request.getAttribute("count") != null){ int count = 11 - Integer.parseInt(request.getAttribute("count").toString()); out.print("<p>Liko klausimų: "+ count +"</p>"); session.setAttribute("countFromJsp", count); if (count == 0){ if (Integer.parseInt(session.getAttribute("questioningTimeCount").toString()) > 1){ %><script type="text/javascript"> $.post('update', function(data) {});</script> <% } else { //response.sendRedirect("content/informantForm.jsp"); //RequestDispatcher rd = request.getRequestDispatcher("/content/informantForm.jsp"); //rd.forward(request, response); } } }%>
Problem is I can't. If I use RequestDispatcher
in JSP page I get an exception
cannot forward after responce has been commited
If I try to access servlet with jQuery it's all OK but the RequestDispacher
doesn't work and I can't redirect to other JSP event from servlet.
Any ideas why?