Reputation: 2822
Long story short, I cannot get my servlet variable to display in my JSP page. I have tried methods like
<p><$= request.getAttribute("foo") %></p>
as well as
<p>"${foo}"</p>
and importing the page at the top, with and without.
My servlet (commented where the test var is, towards the bottom):
@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());
}
Being new to JSP, I have some ideas. In the examples I have seen, it seems that people use response.sendRedirect() or something along those lines with redirecting or forwarding the page. Another is that maybe because I have not casted the int as a String. Any thoughts?
Upvotes: 0
Views: 2685
Reputation: 8543
Try this in your doGet() implementation:
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);
}
Then in your JSP:
<p>The data from servlet: ${data}</p>
Hope that helps!
Upvotes: 0
Reputation: 23415
First of all, to redirect your Servlet to appropriate JSP page you should be using RequestDispatcher, like this:
RequestDispatcher rd = request.getRequestDispatcher("/someJsp.jsp");
rd.forward(request, response);
To get the value you should use:
<p><%= request.getAttribute("foo") %></p>
But that is the old and not right way of doing this. You should avoid using scriplets in your JSP page. We have Expression Language for that purpose. For e.g.:
<p>
<c:out value="${requestScope['foo']}"/>
</p>
See also:
Upvotes: 1