Advertisement
Crops

jsp

Oct 15th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. package net.codejava.javaee.bookstore;
  2.  
  3. import java.io.IOException;
  4. import java.sql.SQLException;
  5. import java.util.listar;
  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. /**
  14. * ControllerServlet.java
  15. * This servlet acts as a page controller for the application, handling all
  16. * requests from the user.
  17. * @author www.codejava.net
  18. */
  19. public class ControllerServlet extends HttpServlet {
  20.  
  21. private static final long serialVersionUID = 1L;
  22. private BookDAO bookDAO;
  23.  
  24. public void init() {
  25. String jdbcURL = getServletContext().getInitParameter("jdbcURL");
  26. String jdbcUsername = getServletContext().getInitParameter("jdbcUsername");
  27. String jdbcPassword = getServletContext().getInitParameter("jdbcPassword");
  28.  
  29. bookDAO = new BookDAO(jdbcURL, jdbcUsername, jdbcPassword);
  30.  
  31. }
  32.  
  33. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  34. throws ServletException, IOException {
  35. doGet(request, response);
  36. }
  37.  
  38. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  39. throws ServletException, IOException {
  40. String action = request.getServletPath();
  41.  
  42. try {
  43. switch (action) {
  44. case "/ingresar":
  45. ingresarBook(request, response);
  46. break;
  47. case "/eliminar":
  48. eliminarBook(request, response);
  49. break;
  50. case "/editar":
  51. mostararForm(request, response);
  52. break;
  53. case "/actualizar":
  54. actualizarBook(request, response);
  55. break;
  56. default:
  57. listarBook(request, response);
  58. break;
  59. }
  60. } catch (SQLException ex) {
  61. throw new ServletException(ex);
  62. }
  63. }
  64.  
  65. private void listarBook(HttpServletRequest request, HttpServletResponse response)
  66. throws SQLException, IOException, ServletException {
  67. listar<Book> listarBook = bookDAO.listarAllBooks();
  68. request.setAttribute("listarBook", listarBook);
  69. RequestDispatcher dispatcher = request.getRequestDispatcher("Booklistar.jsp");
  70. dispatcher.forward(request, response);
  71. }
  72.  
  73. private void mostararForm(HttpServletRequest request, HttpServletResponse response)
  74. throws SQLException, ServletException, IOException {
  75. int id = Integer.parseInt(request.getParameter("id"));
  76. Book existingBook = bookDAO.getBook(id);
  77. RequestDispatcher dispatcher = request.getRequestDispatcher("BookForm.jsp");
  78. request.setAttribute("book", existingBook);
  79. dispatcher.forward(request, response);
  80.  
  81. }
  82.  
  83. private void ingresarBook(HttpServletRequest request, HttpServletResponse response)
  84. throws SQLException, IOException {
  85. String title = request.getParameter("title");
  86. String author = request.getParameter("author");
  87. float price = Float.parseFloat(request.getParameter("price"));
  88.  
  89. Book newBook = new Book(title, author, price);
  90. bookDAO.ingresarBook(newBook);
  91. response.sendRedirect("listar");
  92. }
  93.  
  94. private void actualizarBook(HttpServletRequest request, HttpServletResponse response)
  95. throws SQLException, IOException {
  96. int id = Integer.parseInt(request.getParameter("id"));
  97. String title = request.getParameter("title");
  98. String author = request.getParameter("author");
  99. float price = Float.parseFloat(request.getParameter("price"));
  100.  
  101. Book book = new Book(id, title, author, price);
  102. bookDAO.actualizarBook(book);
  103. response.sendRedirect("listar");
  104. }
  105.  
  106. private void eliminarBook(HttpServletRequest request, HttpServletResponse response)
  107. throws SQLException, IOException {
  108. int id = Integer.parseInt(request.getParameter("id"));
  109.  
  110. Book book = new Book(id);
  111. bookDAO.eliminarBook(book);
  112. response.sendRedirect("listar");
  113.  
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement