accessing servlets using submit buttons

R

ros

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
 
R

Richard Senior

ros said:
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 {
...
}
 
L

Lew

Richard said:
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 ("Remove from Cart".equals(action)) {
...
}
else {
...
}

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top