import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.io.PrintWriter;
import java.io.IOException;
import java.text.DecimalFormat;
public class ShoppingServ extends HttpServlet
{
String link;
String[] books;
String[] music;
String[] computers;
String<ArrayList> selitems; // JAVA.STRING.LANG doesnot take parameters error
String<ArrayList> cartitems ;// JAVA.STRING.LANG doesnot take parameters error
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException,java.util.NoSuchElementException
{
doPost(request,response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException,java.util.NoSuchElementException
{
books=request.getParameterValues("books");
music=request.getParameterValues("music");
computers=request.getParameterValues("computers");
link=request.getParameter("link");
String rem = request.getParameter("rem");
String num = request.getParameter("num");
HttpSession session=request.getSession(true);
session.setMaxInactiveInterval(2*60*60);
response.setContentType("html/text");
PrintWriter out=response.getWriter();
if(link.equals("books"))
{
booksPage(response);
}
else if(link.equals("music"))
{
musicPage(response);
}
else if(link.equals("computers"))
{
computerPage(response);
}
else if(link.equals("items"))
{
String page=request.getParameter("items");
String items[]={" "};
if(page.equals("books"))
{
if(books!=null){
items=books;
}
}
else if(page.equals("music"))
{
if(music!=null)
{
items=music;
}
}
else if(page.equals("computers"))
{
if(computers!=null){
items=computers;
}
}
out.println("<html>");
out.println("<head>");
out.println("<title>Added Items</title>");
out.println("</head>");
out.println("<body bgcolor='#99CC66'>");
out.println("<blockquote>");
out.println("<p> </p>");
out.println("<p><font color='#666666' size='+2'>The following items were added to your shopping cart successfully!!</font></p>");
out.println("<p> </p>");
try{
selitems= new ArrayList();
for(int i=0;i<items.length;i++)
{
selitems.add(items[i]);
StringTokenizer stkn = new StringTokenizer(items[i],"$");
out.println("---"+stkn.nextToken());
out.println("<br>");
out.println("<br>");
while(stkn.hasMoreTokens())
{
out.println("Price of Item is $ "+stkn.nextToken());
}
out.println("<br>");
out.println("<br>");
out.println("<br>");
out.println("<br>");
}
session.setAttribute("selitems",selitems);
cartitems=(ArrayList)session.getAttribute("cartitems");
if(cartitems!=null)
{
ArrayList<String> tempitems=(ArrayList)session.getAttribute("selitems");
if(tempitems!=null)
{
for(String temp:tempitems)
{
cartitems.add(temp);
}
}
session.setAttribute("cartitems",cartitems);
session.removeAttribute("selitems");
}
else{
cartitems = new ArrayList();
ArrayList<String> tempitems = (ArrayList)session.getAttribute("selitems");
for(String temp: tempitems)
{
cartitems.add(temp);
}
session.setAttribute("cartitems", cartitems);
session.removeAttribute("selitems");
}
}
catch(Exception e)
{
out.println("Please Select Any Item");
}
out.println("<p> </p>");
out.println("<p> </p>");
out.println("<hr>");
out.println("<p><a href='shop.fun?link=cart' target='rightFrame' style='text-decoration:none'>[View Cart]</a>    ");
out.println("<a href='shop.fun?link=books' target='rightFrame' style='text-decoration:none'> [Go to Books Page]</a>    ");
out.println("<a href='shop.fun?link=music' target='rightFrame' style='text-decoration:none'> [Go to Music Page]</a>    ");
out.println("<a href='shop.fun?link=computers' target='rightFrame' style='text-decoration:none'> [Go to Computers Page]</a>");
out.println("</p>     </p>");
out.println("<p> </p>");
out.println("</blockquote>");
out.println("</body>");
out.println("</html>");
}
else if(link.equals("cart"))
{
out.println("<html>");
out.println("<head>");
out.println("<title>Shopping Cart</title>");
out.println("</head>");
out.println("<body bgcolor='#99CC66'>");
out.println("<blockquote>");
out.println("<hr>");
out.println("<p><font color='#666666' size='+1'> Items you have selected: </font></p>");
out.println("<p> </p>");
out.println("<div align='center'>");
try{
out.println("<table width='60%' border='0' cellpadding='8'>");
out.println("<tr align='center'>");
out.println("<th width='65%' scope='col'>  Item</th>");
out.println("<th width='10%' scope='col'>  Price</th>");
out.println("<th width='25%' scope='col'> </th>");
out.println("</tr>");
cartitems=(ArrayList)session.getAttribute("cartitems");
double total=0.00;
if(rem!=null)
{
if(rem.equals("xyz"))
{
cartitems.remove(Integer.parseInt(num));
session.setAttribute("cartitems",cartitems);
}
}
for(String s:cartitems)
{
out.println("<tr align='left'>");
StringTokenizer st = new StringTokenizer(s, "$");
out.println("<td> " + st.nextToken() + "</td>");
while (st.hasMoreTokens())
{
String tot = st.nextToken();
out.println("<td> " + tot + "</td>");
total = total + Double.parseDouble(tot);
}
out.println("<td><A href='shop.fun?link=cart&rem=xyz&num=" + cartitems.indexOf(s) +"'");
out.println("target='rightFrame'>[remove item]</A></td>");
out.println("</tr>");
}
out.println("</table>");
out.println("<p> </p>");
out.println("<p> </p>");
out.println("<B>Total:    " + total + "</B>");
out.println("<p> </p>");
out.println("<form method='post' action='shop.fun?link=orderplaced'>");
out.println("<label>");
out.println("<input type='submit' name='reset' value='Check out'>");
out.println("</label>");
out.println("</form>");
}
catch(Exception e){
out.println("Please Select Any Item");
}
out.println("<p> </p>");
out.println("<hr>");
out.println("<p> </p>");
out.println("</div>");
out.println("</blockquote>");
out.println("</body>");
out.println("</html>");
}
if(link.equals("orderplaced"))
{
out.println("<html>");
out.println("<head>");
out.println("<title>Shopping Cart</title>");
out.println("</head>");
out.println("<body bgcolor='#99CC66'>");
out.println("<blockquote>");
out.println("<hr>");
out.println("<p> </p>");
out.println("<p> </p>");
out.println("<p> </p>");
out.println("<p> </p>");
out.println("<p> </p>");
out.println("<div align='center'>");
out.println("<font size = '7' color='374780'><strong>You Have Successfully Placed The Order</strong></font>");
session.invalidate();
out.println("<p> </p>");
out.println("<p> </p>");
out.println("<p> </p>");
out.println("<p> </p>");
out.println("<hr>");
out.println("<p> </p>");
out.println("</div>");
out.println("</blockquote>");
out.println("</body>");
out.println("</html>");
}
}
void booksPage(HttpServletResponse response) throws ServletException,IOException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Books</title>");
out.println("</head>");
out.println("<body bgcolor='#99CC66'>");
out.println("<blockquote>");
out.println("<p> ");
out.println("<font color='#666666' size='+4'>Shop for Books");
out.println("</font>");
out.println("</p>");
out.println("<hr>");
out.println("</p>");
out.println("<div align='right'>");
out.println("<font color='blue' size='+2'>");
out.println("<p><a href='shop.fun?link=cart' target='rightFrame' style='text-decoration:none'>");
out.println("[View Cart]</a></p>");
out.println("</font>");
out.println("</div>");
out.println("<font color='#000033' size='+1'>");
out.println("<form name='booksshopping' method='post' action='shop.fun'>");
out.println("<p>");
out.println("<input type='hidden' name='link' value='items'>");
out.println("<input type='hidden' name='items' value='books'>");
out.println("<label>");
out.println("<input type='checkbox' name='books' value='Java Servlet Programming $29.95'>");
out.println("Java Servlet Programming [$29.95]</label>");
out.println("<br>");
out.println("<br>");
out.println("<label>");
out.println("<input type='checkbox' name='books' value='Oracle Database Programming $48.95'>");
out.println("Oracle Database Programming [$48.95]</label>");
out.println("<br>");
out.println("<br>");
out.println("<label>");
out.println("<input type='checkbox' name='books' value='System Analysis with UML $14.95'>");
out.println("System Analysis with UML [$14.95]</label>");
out.println("<br>");
out.println("<br>");
out.println("<label>");
out.println("<input type='checkbox' name='books' value='Object Oriented Software Programming $35.99'>");
out.println("Object Oriented Software Programming [$35.99]</label>");
out.println("<br>");
out.println("<br>");
out.println("<label>");
out.println("<input type='checkbox' name='books' value='Java Web Services $27.99'>");
out.println("Java Web Services [$27.99]<br>");
out.println("<br>");
out.println("<br>");
out.println("<div align='center'>");
out.println("<input type='submit' name='cart' value='Add to Cart'>");
out.println("</div>");
out.println("</label>");
out.println("<br>");
out.println("</p>");
out.println("</form>");
out.println("<p> </p>");
out.println("</blockquote>");
out.println("</body>");
out.println("</html>");
}
void musicPage(HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Music</title>");
out.println("</head>");
out.println("<body bgcolor='#99CC66'>");
out.println("<blockquote>");
out.println("<p> ");
out.println("<font color='#666666' size='+4'>Shop for Music");
out.println("</font>");
out.println("</p>");
out.println("<hr>");
out.println("</p>");
out.println("<div align='right'>");
out.println("<font color='#660000' size='+2'>");
out.println("<p><a href='shop.fun?link=cart' target='rightFrame' style='text-decoration:none'>[View Caart]</a></p>");
out.println("</font>");
out.println("</div>");
out.println("<font color='#000033' size='+1'>");
out.println("<form name='musicshopping' method='post' action='shop.fun'>");
out.println("<p>");
out.println("<input type='hidden' name='link' value='items'>");
out.println("<input type='hidden' name='items' value='music'>");
out.println("<label>");
out.println("<input type='checkbox' name='music' value='Hallowed be thy name by Iron Maiden $26.99'>");
out.println("Hallowed be thy name by Iron Maiden [$26.99]</label>");
out.println("<br>");
out.println("<br>");
out.println("<label>");
out.println("<input type='checkbox' name='music' value='Dream of mirrors by Iron Maiden $10.95'>");
out.println("Dream of mirrors by Iron Maiden [$10.95]</label>");
out.println("<br>");
out.println("<br>");
out.println("<label>");
out.println("<input type='checkbox' name='music' value='Fatal Tragedy by Dream Theatre $9.97'>");
out.println("Fatal Tragedy by Dream Theatre [$9.97]</label>");
out.println("<br>");
out.println("<br>");
out.println("<label>");
out.println("<input type='checkbox' name='music' value='Fade to Black by Metallica $13.98'>");
out.println("Fade to Black by Metallica [$13.98]</label>");
out.println("<br>");
out.println("<br>");
out.println("<label>");
out.println("<input type='checkbox' name='music' value='Hotel California by Eagles $27.99'>");
out.println("Hotel California by Eagles [$27.99]<br>");
out.println("<br>");
out.println("<br>");
out.println("<div align='center'>");
out.println("<input type='submit' name='cart' value='Add to Cart'>");
out.println("</div>");
out.println("</label>");
out.println("<br>");
out.println("</p>");
out.println("</form>");
out.println("<p> </p>");
out.println("</blockquote>");
out.println("</body>");
out.println("</html>");
}
void computerPage(HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Computers</title>");
out.println("</head>");
out.println("<body bgcolor='#99CC66'>");
out.println("<blockquote>");
out.println("<p> ");
out.println("<font color='#666666' size='+4'>Shop for Computers");
out.println("</font>");
out.println("</p>");
out.println("<hr>");
out.println("</p>");
out.println("<div align='right'>");
out.println("<font color='#660000' size='+2'>");
out.println("<p><a href='shop.fun?link=cart' target='rightFrame' style='text-decoration:none'>[View Cart]</a></p>");
out.println("</font>");
out.println("</div>");
out.println("<font color='#000033' size='+1'>");
out.println("<form name='computersshopping' method='post' action='shop.fun'>");
out.println("<p>");
out.println("<input type='hidden' name='link' value='items'>");
out.println("<input type='hidden' name='items' value='computers'>");
out.println("<label>");
out.println("<input type='checkbox' name='computers' value='Apple MacBook Pro with 13.3\" Display $1299.99'>");
out.println("Apple MacBook Pro with 13.3\" Display [$1299.99]</label>");
out.println("<br>");
out.println("<br>");
out.println("<label>");
out.println("<input type='checkbox' name='computers' value='Asus Laptop with Centrino 2 Black $949.95'>");
out.println("Asus Laptop with Centrino 2 Black [$949.95]</label>");
out.println("<br>");
out.println("<br>");
out.println("<label>");
out.println("<input type='checkbox' name='computers' value='HP Pavilion Laptop with Centrino 2 DV7 $1199.95'>");
out.println("HP Pavilion Laptop with Centrino 2 DV7 [$1199.95]</label>");
out.println("<br>");
out.println("<br>");
out.println("<label>");
out.println("<input type='checkbox' name='computers' value='Toshiba satellite Laptop with Centrino 2 - Copper $899.99'>");
out.println("Toshiba satellite Laptop with Centrino 2 - Copper [$899.99]</label>");
out.println("<br>");
out.println("<br>");
out.println("<label>");
out.println("<input type='checkbox' name='computers' value='Sony VAIO Laptop with Core 2 Duo Cosmopolitan $779.99'>");
out.println("Sony VAIO Laptop with Core 2 Duo Cosmopolitan [$779.99]<br>");
out.println("<br>");
out.println("<br>");
out.println("<div align='center'>");
out.println("<input type='submit' name='cart' value='Add to Cart'>");
out.println("</div>");
out.println("</label>");
out.println("<br>");
out.println("</p>");
out.println("</form>");
out.println("<p> </p>");
out.println("</blockquote>");
out.println("</body>");
out.println("</html>");
}
}
selitems and cartitems don't take parameters,I'm kinda stuck with this error!