2011-09-13 51 views
1

我在創建一個應用程序時遇到問題。例如,我有一個列表或數組的字符串行 - 這些都是問題。我有一個servlet和JSP頁面,在JSP頁面中有一個「next」按鈕,在按下下一個按鈕時,必須在JSP中打印一個問題。在同一個JSP頁面提問

請給我一些建議我該如何做這項工作。

我有一個Struts Action類中,我有我的代碼放置問題:

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

在JSP頁面中我數了問話結束時,我想轉發給ohter JSP頁面。

<% 
    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); 
      } 
     } 
    } 
%> 

問題是我不能。如果我在JSP頁面中使用RequestDispatcher我得到一個異常

後無法前進性反應已經COMMITED

如果我試圖用jQuery訪問的servlet這一切OK,但RequestDispacher不工作,我不能重定向到來自servlet的其他JSP事件。

任何想法爲什麼?

+0

你嘗試過什麼嗎?顯示你做了什麼,它會更容易幫助你。 –

回答

0

如果我在JSP頁面中使用的RequestDispatcher我得到一個異常

cannot forward after responce has been commited 

JSP作爲視圖技術是隱含的HTTP響應的一部分。 JSP中的每個模板行都被寫入響應。爲了能夠改變響應,響應應該是完全不變的,並且是空白的。但在JSP內部並不一定如此。

將該Java代碼移動到將在JSP之前運行的動作或servlet類。無論如何,Java代碼doesn't都屬於JSP文件。