5
 Aim: HTTP is a stateless protocol. Session is required to maintain the state. The user may add some items to cart from the catalog page. He can check the cart page for items. He may visit catalogue again and select some more items. Here our interest is the selected items should be added to older cart rather than new cart. P!"A#: $atalogue.%ava import java.io.IOExceptio n; import java.io.PrintWr iter; import java.util.ArrayList; import javax.servlet.ServletConf ig; import javax.servlet.ServletExce ption; import javax.servlet.ttp.! ttpServlet; import javax.servlet.ttp.! ttpServlet"e#uest; import javax.servlet.ttp.! ttpServlet"esponse; import javax.servlet.ttp.! ttpSession;  pu$lic class Catalogu e exten%s !ttpServlet & int itemcount'(;  )Overri%e  pu$lic voi% init *ServletConfig config+ tro,s ServletException & super. init*config+; - )Overri%e  pu$lic voi% service *!ttpServlet"e#uest re#!ttpServlet"esponse res+ tro,s IOExceptionServletException & res.setContent/ ype*0tex t1tml0+; PrintWrite r p,'res.getWr iter*+; !ttpSession s're#.getSession*+; ArrayList cart'*ArrayList+s.getAttri$ute *0cart0+; if*cart2'null+ & itemcount'cart.si3e*+; - p,.println *0Select items in cart0+;  p,. println*04$o%y54fiel%set54leg en%5Catalogue41legen% 50 6 04form action'7cart7 meto%'7get750 6 04input type'7cec8$ox7 value'79:L i$le7 name'7$oo8759:L i$le41input54$r50+;

wt exp12

Embed Size (px)

DESCRIPTION

wt exp12

Citation preview

Aim:HTTP is a stateless protocol. Session is required to maintain the state. The user may add some items to cart from the catalog page. He can check the cart page for items. He may visit catalogue again and select some more items. Here our interest is the selected items should be added to older cart rather than new cart.

PROGRAM:

Catalogue.java

import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class Catalogue extends HttpServlet{ int itemcount=0; @Overridepublic void init(ServletConfig config) throwsServletException{ super.init(config);} @Overridepublic void service(HttpServletRequestreq,HttpServletResponse res) throwsIOException,ServletException{ res.setContentType("text/html");PrintWriter pw=res.getWriter();HttpSession hs=req.getSession();ArrayList cart=(ArrayList)hs.getAttribute("cart");if(cart!=null){ itemcount=cart.size();} pw.println("Select items in cart");pw.println("Catalogue"+ ""+ "XML Bible
");pw.println("Artificial Intelligence: A Modern Approach
");pw.println("Beginning Java 2
");pw.println("HTML5: Up and Running
");pw.println("");pw.println("");}}Cart.java

import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import java.util.Iterator;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class Cart extends HttpServlet{ @Override public void init(ServletConfig config) throws ServletException{ super.init(config);} @Override public void service(HttpServletRequestreq,HttpServletResponse res) throwsIOException,ServletException{ res.setContentType("text/html");PrintWriter pw=res.getWriter();HttpSession hs=req.getSession();ArrayList cart=(ArrayList)hs.getAttribute("cart");if(cart==null){ pw.println("No items in your cart");cart=new ArrayList();hs.setAttribute("cart",cart);} String itemselected[];String item;itemselected=req.getParameterValues("book");if(itemselected!=null){ for(int i=0;i