title: Passing Attributes from JSP to Servlet to JSP
body: <p>So, I have a JSP form that just takes in a query string, passes it to a servlet, which then sets some HttpServletRequest attributes and forwards to another jsp. For some reason, in the final jsp, all of the attributes return null, as if they hadn't been set.</p>
  
  <p>CatQuery.jsp</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Category Query&lt;/title&gt;
          &lt;meta http-equiv="Content-Type" content="text/html' charset=iso-8859-1"&gt;
  &lt;/head&gt;
  
  &lt;table width="500" border="0" cellspacing="0" cellpadding="0"&gt;
    &lt;tr&gt;
     &lt;td&gt;
       &lt;form name="queryForm" method="post" action="CategoryRetrieve"&gt;
        &lt;td&gt;&lt;div align="left"&gt;Query:&lt;/div&gt;&lt;/td&gt;
        &lt;td&gt;&lt;input type="text" name="queryStr"&gt;&lt;/td&gt;
        &lt;td&gt;&lt;input type="Submit" name="Submit"&gt;&lt;/td&gt;
       &lt;/form&gt;
     &lt;/td&gt;
    &lt;/tr&gt;
   &lt;/table&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>It calls this servlet, CategoryRetrieveServlet.java</p>
  
  <pre><code>public void doPost(HttpServletRequest request, HttpServletResponse response) throws         ServletException, IOException {
              String queryStr  = request.getParameter("queryStr");
  
              CategoryIndex catIndex = new CategoryIndex(indexDir);
              Map&lt;String, Float&gt; weights = catIndex.queryCategory(queryStr, numTopWords, numTopDocs, numCategories);
              if(weights!=null){
                      request.setAttribute("CATWEIGHTS", weights);
                      request.setAttribute("HASRESULTS", "true");
              }
              else {
                      request.setAttribute("HASRESULTS", "false");
              }
  
              ServletContext context = getServletContext();
              RequestDispatcher dispatcher = context.getRequestDispatcher(target);
              dispatcher.forward(request, response);
      }
  </code></pre>
  
  <p>Which, in turn, forwards to this JSP page, CatDisplay.jsp</p>
  
  <pre><code>&lt;%@ page import="java.util.*" %&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Category Search Results&lt;/title&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
  &lt;/head&gt;
  
  &lt;table width="1000" border="5" cellspacing="0" cellpadding="0"&gt;
  &lt;% Map&lt;String, Float&gt; catweights = null;
     catweights=(Map&lt;String,Float&gt;)request.getAttribute("CATWEIGHTS");
  %&gt;
  hasResults is
  &lt;%= request.getAttribute("HASRESULTS") %&gt;
  &lt;% if (catweights==null){ %&gt; Catweights is null
  &lt;% }
     else {
          for (Map.Entry&lt;String,Float&gt; e : catweights.entrySet()){
  %&gt;
          &lt;tr&gt;&lt;td&gt;
  &lt;%=             e.getKey()%&gt;
          &lt;/td&gt;&lt;td&gt;
  &lt;%=             e.getValue()%&gt;
          &lt;/td&gt;&lt;/tr&gt;
  &lt;%      }
      }
  %&gt;
  &lt;/table&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>When I submit a query string, the resulting page says "hasResults is null Catweights is null". Can anybody tell me why my attributes aren't being set?</p>