title: How to pass parameters to JSP using response.sendRedirect?
body: <p>I really need to use <code>response.sendRedirect()</code> method because my JSP(search.jsp)  page is actually calling for a servlet <code>&lt;jsp:include page="/searchServlet" /&gt;</code> and in my <code>searchServlet</code> if I use the forward method (to the same page) I get a weird error (maybe because I'm calling it and forwarding all the time, I din't find the reason yet..) however I need to read an attribute in my JSP(search.jsp)  page with a simple message like "Please provide valid data"</p>
  
  <p>However since I'm using my response to redirect to a page, and set this attribute in my request scope, when I try to read it nothing will as expected. I tried to put this attribute in my Session Scope, read and then removing it, it worked (passing wrong data), but only for the first time, if I try to run it providing all data correctly again I'll get the error message since I dind't close my browser to actually remove it from my Session.</p>
  
  <p>So my question is: there is a way to use <code>sendRedirect()</code> and set an attribute to my JSP page together? If not, how to solve this problem? </p>
  
  <p>Thank you!</p>
  
  <p><strong>EDIT 1:</strong></p>
  
  <p>searchServlet:</p>
  
  <pre><code>if (errorMessage != null) {
      /*sendError will generate an HTML error page using the message that you supplied and will override the content type to text/html.
      response.sendError(1, errorMessage);    
      */
      request.getSession().setAttribute("errorMessage",errorMessage);
      response.sendRedirect("charts/search.jsp");
  }
  </code></pre>
  
  <p>search.JSP</p>
  
  <pre><code>&lt;jsp:include page="/searchServlet" /&gt; 
  &lt;c:if test="${sessionScope.errorMessage != null}"&gt;
      &lt;div id="error-Message"&gt;
          &lt;c:out value="${sessionScope.errorMessage}" /&gt;
          &lt;c:remove var="errorMessage" scope="session" /&gt;
      &lt;/div&gt;
  &lt;/c:if&gt;
  </code></pre>
  
  <p><strong>EDIT 2:</strong>
  The error I get if I use the forward() instead of sendRedirect and I never stops, keep looping:</p>
  
  <pre><code>at javax.servlet.ServletRequestWrapper.isAsyncStarted(ServletRequestWrapper.java:390)
  // the error above repeats for more than a thousand times   
  
  at javax.servlet.ServletRequestWrapper.isAsyncStarted(ServletRequestWrapper.java:390)
  at org.apache.catalina.core.ApplicationDispatcher.unwrapRequest(ApplicationDispatcher.java:846)
  at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:822)
  at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:486)
  at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:411)
  at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338)
  at control.SearchCriteriaServlet.doPost(SearchCriteriaServlet.java:99)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
  at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
  at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:604)
  at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:543)
  at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:954)
  at org.apache.jsp.charts.searchChart_jsp._jspService(searchChart_jsp.java:183)
  at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
  at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
  at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
  at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
  at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  </code></pre>
  
  <p><strong>EDIT 3:</strong></p>
  
  <p>My doGet() method:</p>
  
  <pre><code>ArrayList&lt;Machine&gt; foundMachines = MachineDB.getAllMachines();
  request.setAttribute("foundMachineList", foundMachines);
  RequestDispatcher rd = request.getRequestDispatcher("/searchServlet");
  rd.forward(request, response);
  </code></pre>
  
  <p>My doPost() that process all entries and have inside it my code from edit1:</p>