Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

accessing servlets using submit buttons

Thread view: 
ros - 03 Apr 2007 08:04 GMT
Hi,

I am not very experienced in servlets and am working on making a
simple shopping cart. This application has 3 servlets and one of these
servlets (ReviewShoppingCart) has a form which has 2 submit buttons.
One button should point to AddToShoppingCart servlet and the other
should point to RemoveFromCart.

But I don't really know how to have the if/else code for that. I mean
I tried the code and it is pasted below.

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

           if (actionString.equals("Add More Items")) {
               System.out.println("addToShoppingCart");
               AddToShoppingCart(request, aPW);
           }else if (actionString.equals("Remove From Basket")) {
               RemoveItemsFromCart(request, aPW);
               System.out.println("reviewShoppingCart");
           }

Please help me with this. I would be extremely grateful.
ros
Richard Senior - 03 Apr 2007 12:40 GMT
> I am not very experienced in servlets and am working on making a
> simple shopping cart. This application has 3 servlets and one of these
> servlets (ReviewShoppingCart) has a form which has 2 submit buttons.
> One button should point to AddToShoppingCart servlet and the other
> should point to RemoveFromCart.

An HTML form can only submit to one place:

<form action="myServlet" method="post">

...

    <input type="submit" value="Add to Cart">
    <input type="submit" value="Remove from Cart">

</form>

You could have two forms in the HTML rendered by the ReviewShoppingCart,
 with one button in each and different action paths.

<form action="AddToCart" method="post">

...

    <input type="submit" value="Add to Cart">

</form>

<form action="RemoveFromCart" method="post">

...

    <input type="submit" value="Remove from Cart">

</form>

But I would combine your AddToShoppingCart and RemoveFromCart servlets
into a single servlet and choose your action based on the button that
was pressed:

<form action="EditShoppingCart" method="post">

...

    <input name="action" type="submit" value="Add to Cart">
    <input name="action" type="submit" value="Remove from Cart">

</form>

This will add an additional parameter to the request called "action"
that will be set to "Add to Cart" or "Remove from Cart".

Then in your servlet:

    String action = request.getParameter("action");

    if ("Add to Cart".equals(action)) {
        ...
    }
    else if ("Add to Cart".equals(action)) {
        ...
    }
    else {
        ...
    }
Lew - 03 Apr 2007 13:15 GMT
>> I am not very experienced in servlets and am working on making a
>> simple shopping cart. This application has 3 servlets and one of these
[quoted text clipped - 61 lines]
>         ...
>     }

Take a look at the Model-View-Controller (MVC) pattern, a.k.a. "Front
Controller" or "Dispatch".

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.