title: redirect from jsf?
body: <p>I am working on application with jsp, jstl and jsf for my college project, thats being said, I am as well very new to jsf.</p>
  
  <p>Everything is going great so far. However, I seems to have a problem figuring out how to do redirect from managed bean to page with dinamyc parameters.
  For example <code>article.jsp?article_id=2</code></p>
  
  <p>Can somebody tell me how it is done ?</p>
  
  <p>I been trying to use somethinng like</p>
  
  <pre><code>FacesContext.getCurrentInstance().getExternalContext().dispatch("faces/article.jsp2?article_id=" + articleId);
  </code></pre>
  
  <p>But get error:</p>
  
  <pre><code>javax.servlet.ServletException: #{postComment.postClick}: javax.faces.FacesException: javax.servlet.ServletException: javax.faces.component.UIViewRoot cannot be cast to com.sun.faces.application.StateManagerImpl$TreeNode
      javax.faces.webapp.FacesServlet.service(FacesServlet.java:256)
  </code></pre>
  
  <p>I been trying to use </p>
  
  <pre><code>response.sendRedirect("faces/article.jsp2?article_id=" + articleId);
              return;
  </code></pre>
  
  <p>But again getting an error.</p>
  
  <pre><code>javax.servlet.ServletException: Cannot forward after response has been committed
      javax.faces.webapp.FacesServlet.service(FacesServlet.java:256)
  </code></pre>
  
  <p>Can somebody please tell me how do i redirect from managed java bean when working with jsf ?</p>
  
  <p>Bellow is my code (maybe something wrong with that and thats why redirect not working).</p>
  
  <pre><code>HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
          HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
  
          String articleId = request.getSession().getAttribute("article_id").toString();
          //String articleId  = request.getParameter("article_id");
          String authorName = request.getSession().getAttribute("user_name").toString();
  
          java.util.Calendar calendar = java.util.Calendar.getInstance();
          String commentDate = String.valueOf(calendar.get(java.util.Calendar.DAY_OF_MONTH)) + ".";
          commentDate += String.valueOf(calendar.get(java.util.Calendar.MONTH)) + ".";
          commentDate += String.valueOf(calendar.get(java.util.Calendar.YEAR));
  
           ArrayList error = new ArrayList();
  
          if(commentName.contains("&lt;"))
          {
              error.add("Comment name contains illegal characters");
          }
  
          if(commentBody.isEmpty() &amp;&amp; commentBody.contains("&lt;script"))
          {
              error.add("Your message body contains illegal characters");
          }
  
          if(error.size() &gt; 0)
          {
              request.getSession().setAttribute("error", error);
              error.clear();
              FacesContext.getCurrentInstance().getExternalContext().dispatch("article.jsp2?article_id=" + articleId);
          }
          else
          {
              Comment comment = new Comment();
              comment.setCommentAuthor(authorName);
              comment.setCommentBody(commentBody);
              comment.setCommentDate(commentDate);
              comment.setCommentName(commentName);
              comment.setArticleId(articleId);
  
              DisplayArticleIO addComment = new DisplayArticleIO();
              addComment.postComment(comment);
  //            FacesContext.getCurrentInstance().getExternalContext().dispatch("faces/article.jsp2?article_id=" + articleId);
              response.sendRedirect("faces/article.jsp2?article_id=" + articleId);
              return;
          }
  </code></pre>
  
  <p>Thank you in advance.</p>