test
Форк от lirfrnk/test
1package servlets;
2
3import java.io.IOException;
4import java.io.PrintWriter;
5import java.util.List;
6
7import javax.servlet.RequestDispatcher;
8import javax.servlet.ServletException;
9import javax.servlet.http.HttpServlet;
10import javax.servlet.http.HttpServletRequest;
11import javax.servlet.http.HttpServletResponse;
12
13import com.bittercode.constant.BookStoreConstants;
14import com.bittercode.model.Book;
15import com.bittercode.model.UserRole;
16import com.bittercode.service.BookService;
17import com.bittercode.service.impl.BookServiceImpl;
18import com.bittercode.util.StoreUtil;
19
20public class BuyBooksServlet extends HttpServlet {
21BookService bookService = new BookServiceImpl();
22
23public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
24PrintWriter pw = res.getWriter();
25res.setContentType(BookStoreConstants.CONTENT_TYPE_TEXT_HTML);
26if (!StoreUtil.isLoggedIn(UserRole.CUSTOMER, req.getSession())) {
27RequestDispatcher rd = req.getRequestDispatcher("CustomerLogin.html");
28rd.include(req, res);
29pw.println("<table class=\"tab\"><tr><td>Please Login First to Continue!!</td></tr></table>");
30return;
31}
32try {
33List<Book> books = bookService.getAllBooks();
34RequestDispatcher rd = req.getRequestDispatcher("CustomerHome.html");
35rd.include(req, res);
36StoreUtil.setActiveTab(pw, "cart");
37pw.println("<div class=\"tab hd brown \">Books Available In Our Store</div>");
38pw.println("<div class=\"tab\"><form action=\"buys\" method=\"post\">");
39pw.println("<table>\r\n" +
40" <tr>\r\n" +
41" <th>Books</th>\r\n" +
42" <th>Code</th>\r\n" +
43" <th>Name</th>\r\n" +
44" <th>Author</th>\r\n" +
45" <th>Price</th>\r\n" +
46" <th>Avail</th>\r\n" +
47" <th>Qty</th>\r\n" +
48" </tr>");
49int i = 0;
50for (Book book : books) {
51String bCode = book.getBarcode();
52String bName = book.getName();
53String bAuthor = book.getAuthor();
54double bPrice = book.getPrice();
55int bAvl = book.getQuantity();
56i = i + 1;
57String n = "checked" + Integer.toString(i);
58String q = "qty" + Integer.toString(i);
59pw.println("<tr>\r\n" +
60" <td>\r\n" +
61" <input type=\"checkbox\" name=" + n + " value=\"pay\">\r\n" + // Value is
62// made equal
63// to bcode
64" </td>");
65pw.println("<td>" + bCode + "</td>");
66pw.println("<td>" + bName + "</td>");
67pw.println("<td>" + bAuthor + "</td>");
68pw.println("<td>" + bPrice + "</td>");
69pw.println("<td>" + bAvl + "</td>");
70pw.println("<td><input type=\"text\" name=" + q + " value=\"0\" text-align=\"center\"></td></tr>");
71
72}
73pw.println("</table>\r\n" + "<input type=\"submit\" value=\" PAY NOW \">" + "<br/>" +
74" </form>\r\n" +
75" </div>");
76// pw.println("<div class=\"tab\"><a href=\"AddBook.html\">Add More
77// Books</a></div>");
78} catch (Exception e) {
79e.printStackTrace();
80}
81}
82
83}
84