Passing Attributes from JSP to Servlet to JSP

Adenverd picture Adenverd · Aug 9, 2012 · Viewed 15.6k times · Source

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.

CatQuery.jsp

<html>
<head>
<title>Category Query</title>
        <meta http-equiv="Content-Type" content="text/html' charset=iso-8859-1">
</head>

<table width="500" border="0" cellspacing="0" cellpadding="0">
  <tr>
   <td>
     <form name="queryForm" method="post" action="CategoryRetrieve">
      <td><div align="left">Query:</div></td>
      <td><input type="text" name="queryStr"></td>
      <td><input type="Submit" name="Submit"></td>
     </form>
   </td>
  </tr>
 </table>
</body>
</html>

It calls this servlet, CategoryRetrieveServlet.java

public void doPost(HttpServletRequest request, HttpServletResponse response) throws         ServletException, IOException {
            String queryStr  = request.getParameter("queryStr");

            CategoryIndex catIndex = new CategoryIndex(indexDir);
            Map<String, Float> 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);
    }

Which, in turn, forwards to this JSP page, CatDisplay.jsp

<%@ page import="java.util.*" %>
<html>
<head>
<title>Category Search Results</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<table width="1000" border="5" cellspacing="0" cellpadding="0">
<% Map<String, Float> catweights = null;
   catweights=(Map<String,Float>)request.getAttribute("CATWEIGHTS");
%>
hasResults is
<%= request.getAttribute("HASRESULTS") %>
<% if (catweights==null){ %> Catweights is null
<% }
   else {
        for (Map.Entry<String,Float> e : catweights.entrySet()){
%>
        <tr><td>
<%=             e.getKey()%>
        </td><td>
<%=             e.getValue()%>
        </td></tr>
<%      }
    }
%>
</table>
</html>

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?

Answer

Adenverd picture Adenverd · Aug 14, 2012

SOLVED: The attributes were being passed correctly, but my tomcat instance needed to be restarted before changes in the servlet code would be updated. Kind of a herp-derp. JSP pages, like any html page, will update automatically, but you must recompile AND restart your Tomcat instance after making changes to a servlet.