test

Форк
1
/
StoreBookServlet.java 
95 строк · 4.0 Кб
1
package servlets;
2

3
import java.io.IOException;
4
import java.io.PrintWriter;
5
import java.util.List;
6

7
import javax.servlet.RequestDispatcher;
8
import javax.servlet.ServletException;
9
import javax.servlet.http.HttpServlet;
10
import javax.servlet.http.HttpServletRequest;
11
import javax.servlet.http.HttpServletResponse;
12

13
import com.bittercode.model.Book;
14
import com.bittercode.model.UserRole;
15
import com.bittercode.service.BookService;
16
import com.bittercode.service.impl.BookServiceImpl;
17
import com.bittercode.util.StoreUtil;
18

19
public class StoreBookServlet extends HttpServlet {
20

21
    // book service for database operations and logics
22
    BookService bookService = new BookServiceImpl();
23

24
    public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
25
        PrintWriter pw = res.getWriter();
26
        res.setContentType("text/html");
27

28
        // Check if the customer is logged in, or else return to login page
29
        if (!StoreUtil.isLoggedIn(UserRole.SELLER, req.getSession())) {
30
            RequestDispatcher rd = req.getRequestDispatcher("SellerLogin.html");
31
            rd.include(req, res);
32
            pw.println("<table class=\"tab\"><tr><td>Please Login First to Continue!!</td></tr></table>");
33
            return;
34
        }
35
        try {
36

37
            // Add/Remove Item from the cart if requested
38
            // store the comma separated bookIds of cart in the session
39
            // StoreUtil.updateCartItems(req);
40

41
            RequestDispatcher rd = req.getRequestDispatcher("SellerHome.html");
42
            rd.include(req, res);
43
            pw.println("<div class='container'>");
44
            // Set the active tab as cart
45
            StoreUtil.setActiveTab(pw, "storebooks");
46

47
            // Read the books from the database with the respective bookIds
48
            List<Book> books = bookService.getAllBooks();
49
            pw.println("<div id='topmid' style='background-color:grey'>Books Available In the Store</div>");
50
            pw.println("<table class=\"table table-hover\" style='background-color:white'>\r\n"
51
                    + "  <thead>\r\n"
52
                    + "    <tr style='background-color:black; color:white;'>\r\n"
53
                    + "      <th scope=\"col\">BookId</th>\r\n"
54
                    + "      <th scope=\"col\">Name</th>\r\n"
55
                    + "      <th scope=\"col\">Author</th>\r\n"
56
                    + "      <th scope=\"col\">Price</th>\r\n"
57
                    + "      <th scope=\"col\">Quantity</th>\r\n"
58
                    + "      <th scope=\"col\">Action</th>\r\n"
59
                    + "    </tr>\r\n"
60
                    + "  </thead>\r\n"
61
                    + "  <tbody>\r\n");
62
            if (books == null || books.size() == 0) {
63
                pw.println("    <tr style='background-color:green'>\r\n"
64
                        + "      <th scope=\"row\" colspan='6' style='color:yellow; text-align:center;'> No Books Available in the store </th>\r\n"
65
                        + "    </tr>\r\n");
66
            }
67
            for (Book book : books) {
68
                pw.println(getRowData(book));
69
            }
70

71
            pw.println("  </tbody>\r\n"
72
                    + "</table></div>");
73

74
        } catch (Exception e) {
75
            e.printStackTrace();
76
        }
77
    }
78

79
    public String getRowData(Book book) {
80
        return "    <tr>\r\n"
81
                + "      <th scope=\"row\">" + book.getBarcode() + "</th>\r\n"
82
                + "      <td>" + book.getName() + "</td>\r\n"
83
                + "      <td>" + book.getAuthor() + "</td>\r\n"
84
                + "      <td><span>&#8377;</span> " + book.getPrice() + "</td>\r\n"
85
                + "      <td>"
86
                + book.getQuantity()
87
                + "      </td>\r\n"
88
                + "      <td><form method='post' action='updatebook'>"
89
                + "          <input type='hidden' name='bookId' value='" + book.getBarcode() + "'/>"
90
                + "          <button type='submit' class=\"btn btn-success\">Update</button>"
91
                + "          </form>"
92
                + "    </tr>\r\n";
93
    }
94

95
}
96

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.