test

Форк
0
Форк от lirfrnk/test
/
RemoveBookServlet.java 
78 строк · 3.3 Кб
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.ResponseCode;
13
import com.bittercode.model.UserRole;
14
import com.bittercode.service.BookService;
15
import com.bittercode.service.impl.BookServiceImpl;
16
import com.bittercode.util.StoreUtil;
17

18
public class RemoveBookServlet extends HttpServlet {
19

20
    BookService bookService = new BookServiceImpl();
21

22
    public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
23
        PrintWriter pw = res.getWriter();
24
        res.setContentType("text/html");
25
        if (!StoreUtil.isLoggedIn(UserRole.SELLER, req.getSession())) {
26
            RequestDispatcher rd = req.getRequestDispatcher("SellerLogin.html");
27
            rd.include(req, res);
28
            pw.println("<table class=\"tab\"><tr><td>Please Login First to Continue!!</td></tr></table>");
29
            return;
30
        }
31

32
        try {
33
            String bookId = req.getParameter("bookId");
34
            RequestDispatcher rd = req.getRequestDispatcher("SellerHome.html");
35
            rd.include(req, res);
36
            StoreUtil.setActiveTab(pw, "removebook");
37
            pw.println("<div class='container'>");
38
            if (bookId == null || bookId.isBlank()) {
39
                // render the remove book form;
40
                showRemoveBookForm(pw);
41
                return;
42
            } // else continue
43

44
            String responseCode = bookService.deleteBookById(bookId.trim());
45
            if (ResponseCode.SUCCESS.name().equalsIgnoreCase(responseCode)) {
46
                pw.println("<table class=\"tab my-5\"><tr><td>Book Removed Successfully</td></tr></table>");
47
                pw.println(
48
                        "<table class=\"tab\"><tr><td><a href=\"removebook\">Remove more Books</a></td></tr></table>");
49

50
            } else {
51
                pw.println("<table class=\"tab my-5\"><tr><td>Book Not Available In The Store</td></tr></table>");
52
                pw.println(
53
                        "<table class=\"tab\"><tr><td><a href=\"removebook\">Remove more Books</a></td></tr></table>");
54
            }
55
            pw.println("</div>");
56
        } catch (Exception e) {
57
            e.printStackTrace();
58
            pw.println("<table class=\"tab\"><tr><td>Failed to Remove Books! Try Again</td></tr></table>");
59
        }
60
    }
61

62
    private static void showRemoveBookForm(PrintWriter pw) {
63
        String form = "<form action=\"removebook\" method=\"post\" class='my-5'>\r\n"
64
                + "        <table class=\"tab\">\r\n"
65
                + "        <tr>\r\n"
66
                + "            <td>\r\n"
67
                + "                <label for=\"bookCode\">Enter BookId to Remove </label>\r\n"
68
                + "                <input type=\"text\" name=\"bookId\" placeholder=\"Enter Book Id\" id=\"bookCode\" required>\r\n"
69
                + "                <input class=\"btn btn-danger my-2\" type=\"submit\" value=\"Remove Book\">\r\n"
70
                + "            </td>\r\n"
71
                + "        </tr>\r\n"
72
                + "\r\n"
73
                + "        </table>\r\n"
74
                + "    </form>";
75
        pw.println(form);
76
    }
77

78
}
79

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

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

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

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