2012-08-09 8 views
0

그래서 쿼리 문자열을 가져 와서 서블릿으로 전달한 다음, 일부 HttpServletRequest 속성을 설정하고 다른 JSP로 전달합니다. 어떤 이유로, 최종 JSP에서는 모든 속성이 설정되지 않은 것처럼 null을 반환합니다.JSP에서 Servlet으로 JSP에서 JSP로 속성 전달하기

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는 null입니다. Catweights는 null입니다"라고 표시됩니다. 아무도 내 속성이 설정되지 않은 이유를 말할 수 있습니까?

+0

코드를 다시 확인하십시오. 메소드'catIndex.queryCategory'는 null을 반환합니다. – adatapost

+0

'catIndex.queryCategory (...) {}'메소드의 정의를 게시해야합니다. – Prateek

+0

왜'ServletContext'를 사용하여'RequestDispatcher'를 얻고 있습니까? 'request.getRequestDispatcher (target)'에 의해 직접 액세스 할 수 있습니다. – Logan

답변

2

해결 : 속성이 올바르게 전달되었지만 서블릿 코드의 변경 사항을 업데이트하기 전에 내 Tomcat 인스턴스를 다시 시작해야했습니다. Herp-derp의 종류. JSP 페이지는 HTML 페이지와 마찬가지로 자동으로 업데이트되지만 서블릿을 변경 한 후에 Tomcat 인스턴스를 다시 컴파일하고 다시 시작해야합니다.

+0

당신은 이것을 잊지 않을 것입니다 :) – Jayy