test

Форк
0
Форк от lirfrnk/test
/
AddBookServlet.java 
89 строк · 4.6 Кб
1
package servlets;
2

3
import java.io.IOException;
4
import java.io.PrintWriter;
5
import java.util.UUID;
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.constant.BookStoreConstants;
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 AddBookServlet 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
        String bName = req.getParameter(BooksDBConstants.COLUMN_NAME);
36
        RequestDispatcher rd = req.getRequestDispatcher("SellerHome.html");
37
        rd.include(req, res);
38
        StoreUtil.setActiveTab(pw, "addbook");
39
        pw.println("<div class='container my-2'>");
40
        if(bName == null || bName.isBlank()) {
41
            //render the add book form;
42
            showAddBookForm(pw);
43
            return;
44
        } //else process the add book
45
        
46
 
47
        try {
48
            String uniqueID = UUID.randomUUID().toString();
49
            String bCode = uniqueID;
50
            String bAuthor = req.getParameter(BooksDBConstants.COLUMN_AUTHOR);
51
            double bPrice = Integer.parseInt(req.getParameter(BooksDBConstants.COLUMN_PRICE));
52
            int bQty = Integer.parseInt(req.getParameter(BooksDBConstants.COLUMN_QUANTITY));
53

54
            Book book = new Book(bCode, bName, bAuthor, bPrice, bQty);
55
            String message = bookService.addBook(book);
56
            if ("SUCCESS".equalsIgnoreCase(message)) {
57
                pw.println(
58
                        "<table class=\"tab\"><tr><td>Book Detail Updated Successfully!<br/>Add More Books</td></tr></table>");
59
            } else {
60
                pw.println("<table class=\"tab\"><tr><td>Failed to Add Books! Fill up CareFully</td></tr></table>");
61
                //rd.include(req, res);
62
            }
63
        } catch (Exception e) {
64
            e.printStackTrace();
65
            pw.println("<table class=\"tab\"><tr><td>Failed to Add Books! Fill up CareFully</td></tr></table>");
66
        }
67
    }
68
    
69
    private static void showAddBookForm(PrintWriter pw) {
70
        String form = "<table class=\"tab my-5\" style=\"width:40%;\">\r\n"
71
                + "        <tr>\r\n"
72
                + "            <td>\r\n"
73
                + "                <form action=\"addbook\" method=\"post\">\r\n"
74
                + "                    <!-- <label for=\"bookCode\">Book Code : </label><input type=\"text\" name=\"barcode\" id=\"bookCode\" placeholder=\"Enter Book Code\" required><br/> -->\r\n"
75
                + "                    <label for=\"bookName\">Book Name : </label> <input type=\"text\" name=\"name\" id=\"bookName\" placeholder=\"Enter Book's name\" required><br/>\r\n"
76
                + "                    <label for=\"bookAuthor\">Book Author : </label><input type=\"text\" name=\"author\" id=\"bookAuthor\" placeholder=\"Enter Author's Name\" required><br/>\r\n"
77
                + "                    <label for=\"bookPrice\">Book Price : </label><input type=\"number\" name=\"price\" placeholder=\"Enter the Price\" required><br/>\r\n"
78
                + "                    <label for=\"bookQuantity\">Book Qnty : </label><input type=\"number\" name=\"quantity\" id=\"bookQuantity\" placeholder=\"Enter the quantity\" required><br/>\r\n"
79
                + "                    <input class=\"btn btn-success my-2\" type=\"submit\" value=\" Add Book \">\r\n"
80
                + "                </form>\r\n"
81
                + "            </td>\r\n"
82
                + "        </tr>  \r\n"
83
                + "        <!-- <tr>\r\n"
84
                + "            <td><a href=\"index.html\">Go Back To Home Page</a></td>\r\n"
85
                + "        </tr> -->\r\n"
86
                + "    </table>";
87
        pw.println(form);
88
    }
89
}
90

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

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

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

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