import java.util.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import shopping.CD; public class ShoppingServlet extends HttpServlet { public void init(ServletConfig conf) throws ServletException { super.init(conf); } public void doPost (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { HttpSession session = req.getSession(false); if (session == null) { res.sendRedirect("http://localhost:8080/error.html"); } Vector buylist= (Vector)session.getValue("shopping.shoppingcart"); String action = req.getParameter("action"); if (!action.equals("CHECKOUT")) { if (action.equals("DELETE")) { String del = req.getParameter("delindex"); int d = (new Integer(del)).intValue(); buylist.removeElementAt(d); } else if (action.equals("ADD")) { //any previous buys of same cd? boolean match=false; CD aCD = getCD(req); if (buylist==null) { //add first cd to the cart buylist = new Vector(); //first order buylist.addElement(aCD); } else { // not first buy for (int i=0; i< buylist.size(); i++) { CD cd = (CD) buylist.elementAt(i); if (cd.getAlbum().equals(aCD.getAlbum())) { cd.setQuantity(cd.getQuantity()+aCD.getQuantity()); buylist.setElementAt(cd,i); match = true; } //end of if name matches } // end of for if (!match) buylist.addElement(aCD); } } session.putValue("shopping.shoppingcart", buylist); String url="/jsp/shopping/EShop.jsp"; ServletContext sc = getServletContext(); RequestDispatcher rd = sc.getRequestDispatcher(url); rd.forward(req, res); } else if (action.equals("CHECKOUT")) { float total =0; for (int i=0; i< buylist.size();i++) { CD anOrder = (CD) buylist.elementAt(i); float price= anOrder.getPrice(); int qty = anOrder.getQuantity(); total += (price * qty); } total += 0.005; String amount = new Float(total).toString(); int n = amount.indexOf('.'); amount = amount.substring(0,n+3); req.setAttribute("amount",amount); String url="/jsp/shopping/Checkout.jsp"; ServletContext sc = getServletContext(); RequestDispatcher rd = sc.getRequestDispatcher(url); rd.forward(req,res); } } private CD getCD(HttpServletRequest req) { //imagine if all this was in a scriptlet...ugly, eh? String myCd = req.getParameter("CD"); String qty = req.getParameter("qty"); StringTokenizer t = new StringTokenizer(myCd,""); String album= t.nextToken(); String artist = t.nextToken(); String country = t.nextToken(); String price = t.nextToken(); price = price.replace('$',' ').trim(); CD cd = new CD(); cd.setAlbum(album); cd.setArtist(artist); cd.setCountry(country); cd.setPrice((new Float(price)).floatValue()); cd.setQuantity((new Integer(qty)).intValue()); return cd; } }
<html> <body> <h1> Sorry, there was an unrecoverable error! <br> Please try <a href="/examples/jsp/shopping/EShop.jsp">again</a>. </h1> </body> </html>
部署音乐无界
我假定你正在使用来自sun的最新版本的JavaServer Web Development Kit (JSWDK)来运行这个例子。如果不是,参看资源小节去看看到哪里取得它。假设服务器安装在\jswdk-1.0.1,这是Microsoft Windows系统下的缺省路径,可以象下面这样部署音乐无界应用:
Create shopping directory under \jswdk-1.0.1\examples\jsp Copy EShop.jsp to \jswdk-1.0.1\examples\jsp\shopping Copy Cart.jsp to \jswdk-1.0.1\examples\jsp\shopping Copy Checkout.jsp to \jswdk-1.0.1\examples\jsp\shopping Compile the .java files by typing javac *.java Copy ShoppingServlet.class to \jswdk-1.0.1\webpages\Web-Inf\servlets Create shopping directory under \jswdk-1.0.1\examples\Web-Inf\jsp\beans Copy CD.class to \jswdk-1.0.1\examples\Web-Inf\jsp\beans\shopping Copy error.html to \jswdk-1.0.1\webpages Once your server has been started, you should be able to access the application using http://localhost:8080/examples/jsp/shopping/EShop.jsp as the URL 只要你的服务器启动,你应该可以使用URL http://localhost:8080/examples/jsp/shopping/EShop.jsp来访问这个应用程序。