gpt4 book ai didi

java - 无法在 JSP 中显示 servlet 值

转载 作者:行者123 更新时间:2023-12-01 14:51:07 25 4
gpt4 key购买 nike

长话短说,我无法让 servlet 变量显示在 JSP 页面中。我尝试过类似的方法

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

以及

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

并在顶部导入页面,有或没有。

我的servlet(注释了测试变量所在的位置,朝向底部):

@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 转换为字符串。有什么想法吗?

最佳答案

首先,要将 Servlet 重定向到适当的 JSP 页面,您应该使用 RequestDispatcher ,像这样:

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

要获取您应该使用的值:

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

但这是旧的且不正确的方法。您应该避免在 JSP 页面中使用 scriplet。我们有Expression Language为了这个目的。例如:

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

另请参阅:

关于java - 无法在 JSP 中显示 servlet 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14841473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com