1

したがって、クエリ文字列を取り込んでそれをサーブレットに渡し、それをいくつかの HttpServletRequest 属性を設定して別の jsp に転送する JSP フォームがあります。何らかの理由で、最後の jsp で、すべての属性が設定されていないかのように null を返します。

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>

このサーブレットは、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 インスタンスを再起動する必要がありました。一種のヘルプダープ。JSP ページは、他の HTML ページと同様に自動的に更新されますが、サーブレットに変更を加えた後、Tomcat インスタンスを再コンパイルして再起動する必要があります。

于 2012-08-14T00:00:03.487 に答える