• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HFSJ page89

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Khadija Lokhandwala
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have got the answer. Apparently, I had placed servlet-api.jar in the WEB-INF/lib folder which the container could not read.

Thank You
 
Ranch Hand
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look no problem if the jar files are added in the WEB-INF/lib directory, they must be addedd in the class path that is more important.Also in your program you commented out the view.forward(req,res) thats y it wasn't getting forwarded.
 
Khadija Lokhandwala
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The container could not read the servlet class.I dont know why it could not read it.I had kept servlet-api.jar and jsp-api.jar in the WEB-INF/lib directory.

I was just trying whether the page could be forward using other methods so that explains why I commented forward() method.

Thank You
 
carina caoor
Ranch Hand
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if the container is not able to read your servlet class then you might not have done the proper servlet mapping in your web.xml or you might not have placed your .class file in WEB-INF/classes..... check your url-pattern properly also if you could not run the whole application try to execute your servlet first, then proceed step by step by adding requestDispatcher, adding html file then bean etc ....
 
Khadija Lokhandwala
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was able to read my servlet. It was probably of a jar conflict since servlet-api is placed in common/lib and I tried to place it in WEB-INF/lib directory of my webApp. I am using eclipse IDE so I just added the servlet-api.jar in my project in eclipse.
 
reply
    Bookmark Topic Watch Topic
  • New Topic