2013-02-12 42 views
0

長話短說,我無法讓我的servlet變量顯示在我的JSP頁面中。我試圖像無法在JSP中顯示servlet值

<p><$= request.getAttribute("foo") %></p> 

方法以及

<p>"${foo}"</p> 

和導入頁面的頂部,有和沒有。

我的servlet(評論當測試var爲,朝下方):

@Override 
protected void doGet(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException 
{ 
    //String that gets returned as HTML 
    StringBuilder returnAsHTML = new StringBuilder(); 

    //Cycles through the parameter(s) sent from the serialized form 
    Enumeration<String> parameterNames; 

    //Holds all of the parameter(s) values 
    LinkedList<String> valuesIn = new LinkedList<String>(); 

    //Gets all of the parameter names from the serialized form 
    parameterNames = request.getParameterNames(); 

    //Iterate through the enumeration of the parameters 
    while(parameterNames.hasMoreElements()) 
    { 
     String currentParameter = parameterNames.nextElement(); 
     String currentValue = request.getParameter(currentParameter); 

     //Prevent a blank value from being added from the text fields (which is what they default to 
     //if the user didn't enter anything for "Section:" or "Teacher:"). 
     if(!currentValue.contentEquals("")) 
     { 
      valuesIn.add(currentValue); 
     }//Else doNothing(); 
    } 

    //Set the section to query, if the user wanted one to search for 
    if(request.getParameter("section").isEmpty()) 
    { 
     sectionToQuery = ""; 
    } 
    else 
    { 
     sectionToQuery = request.getParameter("section").toUpperCase(); 
    } 

    //Set the teacher to query, if the user wanted one to search for 
    if(request.getParameter("teacher").isEmpty()) 
    { 
     teacherToQuery = ""; 
    } 
    else 
    { 
     teacherToQuery = request.getParameter("teacher").toUpperCase(); 
    } 

    //THIS BEGINS THE QUERY - BE SURE TO BREAK THIS DOWN TO OTHER METHODS 

    //STAGE 1 See what semesters they are needing, eliminate the ones they don't need. 
    resultSet = determineSemesters(valuesIn); 

    //STAGE 2 See what locations they are needing, eliminate the ones they don't need. 
    determineLocations(resultSet, valuesIn); 

    //STAGE 3 See what sections they are needing, eliminate the ones they don't need. 
    determineSections(resultSet, valuesIn); 

    //STAGE 4 See what instructors they are needing, eliminate the ones they don't need. 
    determineInstructors(resultSet, valuesIn); 

    //STAGE 5 See if they want to include closed classes or not, eliminate what they don't need. 
    determineClosedOrNotClosedCourses(resultSet, valuesIn); 

    //SEARCH IS DONE, the remaining elements in "resultSet" are the product of their search. Find 
    //The enrollment and the credits that is in resultSet. 

    //THIS IS WHERE I AM TESTING IT//////////////// 
    int foo = 20; 
    request.setAttribute("foo", foo); 

    //Check to see if the result set is empty 
    if(resultSet.isEmpty()) 
    { 
     returnAsHTML.setLength(0); 
     returnAsHTML.append("<tr><td colspan='15'><h1>No Results...</h1></tr>"); 
    } 
    else//It's not empty, so style the classes. 
    { 
     //Make sure results are sorted 
     Collections.sort(resultSet); 

     //Style all the classes 
     for(ClassInfo classes : resultSet) 
     { 
      returnAsHTML.append(styleClass(classes)); 
     }//End styling of classes 
    } 

    //Send back the result 
    response.getWriter().write(returnAsHTML.toString()); 

} 

作爲新的JSP中,我有一些想法。在我看到的例子中,似乎人們使用response.sendRedirect()或其他方式重定向或轉發頁面。另一個可能是因爲我沒有鑄造int作爲字符串。有什麼想法嗎?

+0

如果您是Servlets和JSP的新手,這可能對您有所幫助:[開始和中間Servlet和JSP教程](http://courses.coreservlets.com/Course-Materials/csajsp2.html) – informatik01 2013-02-12 22:51:21

+0

因爲我在你的[之前的問題](http://stackoverflow.com/questions/14634842/get-data-from-servlet-to-jsp-without-forward)之一中發表了評論,你繼續走完全錯誤的道路。這個'returnAsHTML'屬於您的servlet必須轉發的JSP文件。請仔細閱讀一個理智的JSP/Servlet教程,以瞭解基本概念。我們關於這些主題的標籤維基頁面是一個好的開始。 – BalusC 2013-02-13 03:06:20

回答

1

首先,重定向你的servlet適當的,你應該使用RequestDispatcher JSP頁面,如下所示:

RequestDispatcher rd = request.getRequestDispatcher("/someJsp.jsp"); 
rd.forward(request, response); 

要得到你應該使用值:

<p><%= request.getAttribute("foo") %></p> 

但是,這是這是做這件事的不正確的方法。您應該避免在JSP頁面中使用腳本。爲此,我們有Expression Language。對於例如爲:

<p> 
    <c:out value="${requestScope['foo']}"/> 
</p> 

參見:

+0

不幸的是,使用RequestDispatcher對象將不起作用。它確實成功發送請求和響應,但仍不會更新。感謝您的幫助,我會繼續挖掘。 – 2013-02-12 21:18:47

0

試試這個在您的doGet()實現:

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
{ 
    Object data = "Some data, can be a String or a Javabean"; 
    request.setAttribute("data", data); 
    request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response); 
} 

然後在你的JSP:

<p>The data from servlet: ${data}</p> 

希望幫助!

+0

感謝您的輸入,從我的理解,這將拋出一個IllegalStateException,因爲我發回一個response.getWriter()等....但如果我把它拿出來,只使用你的代碼,我的網頁沒有任何反應。 : - /問題是我必須從servlet發回一個字符串以顯示爲HTML,這很好用,這就是response.getWriter()。write(returnAsHTML.toString());是在做。但是我也需要兩個int值來顯示。 – 2013-02-12 21:15:53