package [Link].
controller;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
@WebServlet("/books/*") // Servlet mapping path
public class BookServlet extends HttpServlet {
private BookDAO bookDAO = new BookDAO(); // If not using a service, use DAO
directly
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException {
String path = [Link]();
if (path == null || [Link]("/")) {
// Fetch books from DAO
List<Book> bookList = [Link]();
[Link]("books", bookList);
[Link]("/[Link]").forward(req, resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException {
String title = [Link]("title");
String author = [Link]("author");
double price = [Link]([Link]("price"));
String image = [Link]("image");
// Create and save new book
Book newBook = new Book();
[Link](title);
[Link](author);
[Link](price);
[Link](image);
boolean isAdded = [Link](newBook);
if (isAdded) {
[Link]("/books/"); // Redirect after adding the book
} else {
[Link]("errorMessage", "Failed to add book.");
[Link]("/[Link]").forward(req, resp);
}
}
}