aspose file tools*
The moose likes Servlets and the fly likes Sessoin Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Sessoin" Watch "Sessoin" New topic
Author

Sessoin

len oke
Greenhorn

Joined: Mar 19, 2007
Posts: 4
I want to make ShoppingCartSessionTracking Servlet by adding the following increased functionality:



* Add a button called "Remove from Basket" to the review page entitled "Review Shopping Basket". Along side every item in the "Review Shopping Basket" page add a checkbox.
* If the user unchecks one or more checkboxes and presses the button "Remove from Basket" then they should be removed from the basket and the "Review Shopping Basket" page updated.

Any help is welcomed

public class ShoppingCartSessionTracking extends HttpServlet {
public void doGet(
HttpServletRequest request,
HttpServletResponse response
) throws ServletException, IOException {
response.setContentType("text/html");

PrintWriter aPW = response.getWriter();
aPW.println("<HTML><HEAD><TITLE>Shopping Basket using Session Tracking API" +
"</TITLE></HEAD><BODY>");

String actionString = request.getParameter("submit");

if (actionString != null) {
if (actionString.equals("Add More Items")) {
System.out.println("addToShoppingCart");
addToShoppingCart(request, aPW);
}else if (actionString.equals("Review Shopping Cart")) {
reviewShoppingCart(request, aPW);
System.out.println("reviewShoppingCart");
}
}else {
reviewShoppingCart(request, aPW);
}
aPW.println("</FORM></BODY></HTML>");
}

private static synchronized void addToShoppingCart (HttpServletRequest request,
PrintWriter aPW) {
aPW.println("<FORM ACTION='ShoppingCartSessionTracking' METHOD=GET>");
aPW.println("Add to Basket:<BR><BR>");
aPW.println("<INPUT TYPE='checkbox' NAME='items' VALUE='Socks $4.0'>Socks $4<BR>");
aPW.println("<INPUT TYPE='checkbox' NAME='items' VALUE='Shoes $30.0'>Shoes $30<BR>");
aPW.println("<BR><BR><INPUT TYPE='Submit' NAME='submit' VALUE='Review Shopping Cart'>");
}

private static synchronized void reviewShoppingCart(
HttpServletRequest request,
PrintWriter aPW) {

// get current session object. If there is not one create one
HttpSession aHttpSession = request.getSession(true);

// look for vector of items in HttpSession
Vector itemsVec = (Vector)aHttpSession.getAttribute("cart.items");

if (itemsVec == null) {
// if the vector of items is not in the HttpSession create a new Vector
aHttpSession.setAttribute("cart.items", new Vector());
itemsVec = (Vector)aHttpSession.getAttribute("cart.items");
}

// read in the new items the user wishs to add to their shopping cart
String newItems[] = request.getParameterValues("items");

// if there are new items to add to the shopping cart add them to the itemsVec
if (newItems != null) {
System.out.println("newItems.length = " + newItems.length);
for (int i=0; i<newItems.length; i++) {
itemsVec.addElement(newItems[i]);
}

// update HttpSession. Old Vector replaced
aHttpSession.setAttribute("cart.items", itemsVec);
}

aPW.println("Review Shopping Basket<BR><BR>");

if (itemsVec.size() > 0) {
Enumeration aEnumItems = itemsVec.elements();

float count = 0;
aPW.println("<UL>");

while (aEnumItems.hasMoreElements()) {
// retrieves the price from the item description
// so that all the prices of all the items can be added to
// give a total
String details = (String)aEnumItems.nextElement();
int positionOfPound = details.indexOf("$") + 1;
String numberStr = details.substring(positionOfPound);
float price = Float.parseFloat(numberStr);
count = count + price;
aPW.println("<LI>" + details);
}

aPW.println("<BR><BR>Total: $" + count);
aPW.println("</UL>");
}else {
aPW.println("No items currently in basket");
}
aPW.println("<FORM ACTION='ShoppingCartSessionTracking' METHOD=GET>");
aPW.println("<BR><BR><INPUT TYPE='Submit' NAME='submit' VALUE='Add More Items'>");
}
}
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

I doubt if some one has the patience to look into that code, especially when you are writing HTML string content. This looks more like an assignment or a requirement for a service request. Can you be more specific about the problem please ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

"gpo", you have previously been warned on one or more occasions regarding adjusting your display name to meet JavaRanch standards. This is not optional. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it prior to your next post.

Your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Be aware that accounts with invalid display names are removed.

bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sessoin