|
There
might be some exception occured while you are running your
servlet,
a null
pointer might be happening on your doPost try catch block, please check
it.
Please help me to solve this problem. Why is this
servlet does not give any result. But compiling withiout giving any
error. I am using tomcat 3.2.3.
Thank you in advance,
Nilantha Piyasiri
-----------------------------------
import javax.servlet.*; import
javax.servlet.http.*; import java.io.*; import java.util.*;
public class cartExample extends
HttpServlet{ HttpSession session =
null; public void doPost
(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException{
try
{response.setContentType("text/html");
PrintWriter out = new PrintWriter
(response.getOutputStream());
out.println("<html>");
out.println("<head><title>Servlet</title></head>");
out.println("<body>");
session =
request.getSession(true);
Integer itemcount = (Integer)
session.getValue("itemcount");
if (itemcount ==
null){
itemcount = new Integer(0);
}
String [] selected =
request.getParameterValues("item");
System.out.println
(selected.length); if (selected
!=
null){
for (int i= 0; i<selected.length;
i++){
if
(selected[i]!=null){
String name = selected
[i];
System.out.println ("not
null");
itemcount = new Integer
(itemcount.intValue()+1);
session.putValue(Integer.toString(i).trim(),new
String(name));
}
}
session.putValue("item count",
itemcount);
}
System.out.println
(itemcount.intValue());
out.println("The products are");
for (int i=0; i <itemcount.intValue();
i++){ if
(session.getValue(Integer.toString(i).trim())!=null){
String item = (String)
session.getValue(Integer.toString(i).trim());
out.print ("<p>" +
item);
}
} out.println
("</body></html>");
out.close(); }
catch(Exception
e){ System.out.println ("error"
+ e.getMessage()); } } public
void destroy (){ session =
null; } }
|