package homework;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Vector;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
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 Controller extends HttpServlet {
private List<DressBean> dresses = new Vector<DressBean>(5);
@Override
public void init() throws ServletException {
dresses.add(new DressBean("Alfred Angelo Pleated Strapless Dress",
"Who says you can’t wear a bridesmaid dress again? This dress hugs your curves in all the right places to give a slimming look. The strapless sweetheart neckline is elegant and perfect for any bridesmaid. This tea length dress is fully lined dress, back zip, and dry clean only and is available in sizes 2 - 20W and comes in all 20 colors.",
149.99, "aa.jpg"));
dresses.add(new DressBean("David's Bridal Sleeveless Chiffon Dress",
"A 'must have' for your bridal party. This dress has a high neckline and keyhole detail on the back. Made from tafeta, this dress features an elastic waist to help flatter any figure. Designed at knee length it is fully lined with a back zip. This dress is available in canary, clover, malibu, and plum and is available in sizes 0 - 30W.",
149.0, "db.jpg"));
dresses.add(new DressBean("Jenny Yoo Organza Strapless Dress",
"A whimsical and charming look that your bridesmaids will love. This dress is breathtaking with the addition of pick-up points at the skirt. Made from organza, it has optional spaghetti straps to make everyone comfortable. This dress is available in all 20 colors and sizes 2 to 20W and 8JB to 16JB.",
159.99, "jy.jpg"));
dresses.add(new DressBean("Lela Rose Chiffon Strapless Dress with Cap Sleeves",
"This ravishing dress features a sweetheart neckline with cap sleeves. Available in floor length or tea length, it includes an optional rhinestone flower waistband. It is available in wheat, silver mist, copper, sunset, and guava in sizes 0 to 20W and maternity sizes.",
140.99, "lr.jpg"));
dresses.add(new DressBean("Donna Morgan Strapless Satin Ballgown",
"Take a step into the old world with the satin ballgown style dress. The illusion neckline and flowered waistband add a unique twist to this dress that everyone will love. Available in dark pacific, cameo, bermuda blue, and carnation and sizes sizes 0 to 30W and 8JB to 16JB.",
188.99, "dm.jpg"));
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
String target = "/viewDresses.jsp";
int index = 0;
Integer position = (Integer) session.getAttribute("position");
if (position != null)
{
index = position.intValue();
}
String button = request.getParameter("button");
if (button == null || "".equals(button)) {
// no button was pressed, so no action will be taken
} else if ("Next".equals(button)) {
index = index + 1;
if (index >= dresses.size()) {
index = 0;
}
} else if ("Prev".equals(button)) {
index = index - 1;
if (index < 0) {
index = dresses.size() - 1;
}
} else if ("List".equals(button)) {
target = "/dressList.jsp";
request.setAttribute("dresses", dresses);
} else {
return;
}
session.setAttribute("position", new Integer(index));
request.setAttribute("dress", dresses.get(index));
ServletContext context = this.getServletContext();
RequestDispatcher dispatcher = context.getRequestDispatcher(target);
dispatcher.forward(request, response);
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}