Hello everyone,
I am studying for SCWCD and have been stuck here. I tried the code given on the page 89.The problem is when I click on a particular color and hit submit, the
jsp does not get displayed. Instead a blank screen is displayed. I have kept the result.jsp in Coffee-v1/jsp/result.jsp in the
TomCat directory.
Here is the
servlet. I have used "Coffee" instead of "Beer"
public class CoffeeSelect extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String color = request.getParameter("color");
CoffeeExpert ce = new CoffeeExpert();
List brands = ce.getBrands(color);
/*response.setContentType("text/html");
PrintWriter out = response.getWriter();
// out.println("<h1 align=center>
Coffee Selection Advice</h1>");
out.println("<br>got Color "+color);*/
request.setAttribute("styles", brands);
System.out.println("IN DO POST!!");
RequestDispatcher view = request.getRequestDispatcher("jsp/result.jsp");
System.out.println("IN DO POST***");
response.sendRedirect("jsp/result.jsp");
//view.forward(request, response);
/*Iterator it = brands.iterator();
while(it.hasNext())
{
out.print("<br> try: "+it.next());
}*/
}
}
The result.jsp
<%@ page import="java.util.*"%>
<html>
<body>
<%
List styles = (List)request.getAttribute("styles");
Iterator it = styles.iterator();
while(it.hasNext())
{
out.print("<br>try: "+it.next());
}
%>
</body>
</html>
Can anyone please tell me what is missing in my code??