test

Форк
0
Форк от lirfrnk/test
/
UpdateBookServlet.java 
96 строк · 4.8 Кб
1
package servlets;
2

3
import java.io.IOException;
4
import java.io.PrintWriter;
5

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

12
import com.bittercode.constant.BookStoreConstants;
13
import com.bittercode.constant.ResponseCode;
14
import com.bittercode.constant.db.BooksDBConstants;
15
import com.bittercode.model.Book;
16
import com.bittercode.model.UserRole;
17
import com.bittercode.service.BookService;
18
import com.bittercode.service.impl.BookServiceImpl;
19
import com.bittercode.util.StoreUtil;
20

21
public class UpdateBookServlet extends HttpServlet {
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(BookStoreConstants.CONTENT_TYPE_TEXT_HTML);
27

28
        if (!StoreUtil.isLoggedIn(UserRole.SELLER, req.getSession())) {
29
            RequestDispatcher rd = req.getRequestDispatcher("SellerLogin.html");
30
            rd.include(req, res);
31
            pw.println("<table class=\"tab\"><tr><td>Please Login First to Continue!!</td></tr></table>");
32
            return;
33
        }
34

35
        RequestDispatcher rd = req.getRequestDispatcher("SellerHome.html");
36
        rd.include(req, res);
37
        StoreUtil.setActiveTab(pw, "storebooks");
38
        pw.println("<div class='container my-2'>");
39

40
        try {
41
            if (req.getParameter("updateFormSubmitted") != null) {
42
                String bName = req.getParameter(BooksDBConstants.COLUMN_NAME);
43
                String bCode = req.getParameter(BooksDBConstants.COLUMN_BARCODE);
44
                String bAuthor = req.getParameter(BooksDBConstants.COLUMN_AUTHOR);
45
                double bPrice = Double.parseDouble(req.getParameter(BooksDBConstants.COLUMN_PRICE));
46
                int bQty = Integer.parseInt(req.getParameter(BooksDBConstants.COLUMN_QUANTITY));
47

48
                Book book = new Book(bCode, bName, bAuthor, bPrice, bQty);
49
                String message = bookService.updateBook(book);
50
                if (ResponseCode.SUCCESS.name().equalsIgnoreCase(message)) {
51
                    pw.println(
52
                            "<table class=\"tab\"><tr><td>Book Detail Updated Successfully!</td></tr></table>");
53
                } else {
54
                    pw.println("<table class=\"tab\"><tr><td>Failed to Update Book!!</td></tr></table>");
55
                    // rd.include(req, res);
56
                }
57

58
                return;
59
            }
60

61
            String bookId = req.getParameter("bookId");
62

63
            if (bookId != null) {
64
                Book book = bookService.getBookById(bookId);
65
                showUpdateBookForm(pw, book);
66
            }
67

68
        } catch (Exception e) {
69
            e.printStackTrace();
70
            pw.println("<table class=\"tab\"><tr><td>Failed to Load Book data!!</td></tr></table>");
71
        }
72
    }
73

74
    private static void showUpdateBookForm(PrintWriter pw, Book book) {
75
        String form = "<table class=\"tab my-5\" style=\"width:40%;\">\r\n"
76
                + "        <tr>\r\n"
77
                + "            <td>\r\n"
78
                + "                <form action=\"updatebook\" method=\"post\">\r\n"
79
                + "                    <label for=\"bookCode\">Book Code : </label><input type=\"text\" name=\"barcode\" id=\"bookCode\" placeholder=\"Enter Book Code\" value='"
80
                + book.getBarcode() + "' readonly><br/>"
81
                + "                    <label for=\"bookName\">Book Name : </label> <input type=\"text\" name=\"name\" id=\"bookName\" placeholder=\"Enter Book's name\" value='"
82
                + book.getName() + "' required><br/>\r\n"
83
                + "                    <label for=\"bookAuthor\">Book Author : </label><input type=\"text\" name=\"author\" id=\"bookAuthor\" placeholder=\"Enter Author's Name\" value='"
84
                + book.getAuthor() + "' required><br/>\r\n"
85
                + "                    <label for=\"bookPrice\">Book Price : </label><input type=\"number\" name=\"price\" placeholder=\"Enter the Price\" value='"
86
                + book.getPrice() + "' required><br/>\r\n"
87
                + "                    <label for=\"bookQuantity\">Book Qnty : </label><input type=\"number\" name=\"quantity\" id=\"bookQuantity\" placeholder=\"Enter the quantity\" value='"
88
                + book.getQuantity() + "' required><br/>\r\n"
89
                + "                    <input class=\"btn btn-success my-2\" type=\"submit\" name='updateFormSubmitted' value=\" Update Book \">\r\n"
90
                + "                </form>\r\n"
91
                + "            </td>\r\n"
92
                + "        </tr>  \r\n"
93
                + "    </table>";
94
        pw.println(form);
95
    }
96
}
97

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

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

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

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