This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes HFSJ page89 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "HFSJ page89" Watch "HFSJ page89" New topic
Author

HFSJ page89

Khadija Lokhandwala
Ranch Hand

Joined: Sep 11, 2007
Posts: 33
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??


If you ask me anything I don't know, I'm not going to answer.<br />--Yogi Berra
Khadija Lokhandwala
Ranch Hand

Joined: Sep 11, 2007
Posts: 33
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
carina caoor
Ranch Hand

Joined: Jun 23, 2007
Posts: 300

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

Joined: Sep 11, 2007
Posts: 33
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

Joined: Jun 23, 2007
Posts: 300

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

Joined: Sep 11, 2007
Posts: 33
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: HFSJ page89
 
Similar Threads
HFSJ struts Example pgm
HFSJ: Can't compile second version of BeerSelect.java
help needed with tomcat/requestDispatcher - example form HeadFirst servlets and jsp
Why this EL code part is not working
Compiling result.jsp (page 89 HFSJ)