1

所以,我有一个 JSP 表单,它只接受一个查询字符串,将它传递给一个 servlet,然后设置一些 HttpServletRequest 属性并转发给另一个 jsp。出于某种原因,在最终的 jsp 中,所有属性都返回 null,就好像它们没有被设置一样。

猫查询.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>

它调用这个 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);
    }

进而转发到这个 JSP 页面 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>

当我提交查询字符​​串时,结果页面显示“hasResults is null Catweights is null”。谁能告诉我为什么我的属性没有被设置?

4

1 回答 1

2

已解决:属性已正确传递,但需要重新启动我的 tomcat 实例,然后才能更新 servlet 代码中的更改。有点像herp-derp。JSP 页面,与任何 html 页面一样,会自动更新,但您必须在对 servlet 进行更改后重新编译并重新启动 Tomcat 实例。

于 2012-08-14T00:00:03.487 回答