Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. Tạo web app BookCrud
- 2.0 Tạo bảng Books (BookID, Title, Price) trong DB Demo4 nếu chưa tạo
- 2.1 Mở admin console của Glassfish > Add Connection Pool
- + Name: Demo4
- + Type: javax.sql.DataSource
- + Vendor: MicrosoftSqlServer
- + Introspect: Enabled
- Next
- + user: sa
- + databaseName: Demo4
- + password:
- + serverName: localhost
- + URL: jdbc:sqlserver://localhost:1433;databaseName=Demo4
- 2.2 Add Resource
- + name: jdbc/demo
- + chọn Pool vừa tạo
- 3. Dừng Glassfish.
- Add > Other > Persistence > Entity Classes from Database > chọn dataSource mới tạo > chọn bảng Books > Next
- đặt trong package vn.aptech.entity rồi Finish
- 4. Tạo Servlet ControllerServlet trong vn.aptech.servlet
- Insert Code > Use Entity Manager
- Thêm đoạn sau ở cuối:
- public List<Books> getBooks(){
- List<Books> result = new ArrayList<>();
- try {
- utx.begin();
- Query q = em.createQuery("SELECT o FROM Books o");
- result = q.getResultList();
- utx.commit();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return result;
- }
- public Books getBook(int id) {
- Books b = new Books();
- try {
- utx.begin();
- b = em.find(Books.class, id);
- utx.commit();
- } catch (Exception e) {
- Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", e);
- throw new RuntimeException(e);
- }
- return b;
- }
- public boolean create(Books b){
- try {
- utx.begin();
- em.persist(b);
- utx.commit();
- } catch (Exception e) {
- Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", e);
- throw new RuntimeException(e);
- }
- return true;
- }
- public boolean update(Books b){
- try {
- utx.begin();
- em.merge(b);
- utx.commit();
- } catch (Exception e) {
- Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", e);
- throw new RuntimeException(e);
- }
- return true;
- }
- public boolean delete(int id){
- try {
- utx.begin();
- em.remove(em.find(Books.class, id));
- utx.commit();
- } catch (Exception e) {
- Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", e);
- throw new RuntimeException(e);
- }
- return true;
- }
- 5. New Standard Deployment Descriptor (web.xml), qua tab Pages, gõ ControllerServlet vào phần Welcome files
- 6. Thêm đoạn sau trong phần try ở method processRequest của ControllerServlet
- String action = request.getParameter("action");
- if (action == null) {
- request.setAttribute("books", getBooks());
- request.getRequestDispatcher("viewAll.jsp").forward(request, response);
- } else if (action.equals("add")) {
- response.sendRedirect("add.jsp");
- } else if (action.equals("addProcess")) {
- Books b = new Books();
- b.setBookID(Integer.parseInt(request.getParameter("BookID")));
- b.setTitle(request.getParameter("Title"));
- b.setPrice(Integer.parseInt(request.getParameter("Price")));
- create(b);
- response.sendRedirect("ControllerServlet");
- } else if (action.equals("delete")) {
- String id = request.getParameter("BookID");
- delete(Integer.parseInt(id));
- response.sendRedirect("ControllerServlet");
- } else if (action.equals("update")) {
- String id = request.getParameter("BookID");
- Books b = getBook(Integer.parseInt(id));
- request.setAttribute("book", b);
- request.getRequestDispatcher("update.jsp").forward(request, response);
- } else if (action.equals("updateProcess")) {
- Books b = new Books();
- b.setBookID(Integer.parseInt(request.getParameter("BookID")));
- b.setTitle(request.getParameter("Title"));
- b.setPrice(Integer.parseInt(request.getParameter("Price")));
- update(b);
- HttpSession session = request.getSession();
- session.setAttribute("success", "Done.");
- response.sendRedirect("ControllerServlet?action=update&BookID="+b.getBookID());
- }
- 7. Tạo JSP: viewAll.jsp và đưa đoạn sau vào:
- <h1>Book Index</h1>
- <div><a href="ControllerServlet?action=add">Add new Book</a></div>
- <br/>
- <table border="1" style="margin-top: 5px">
- <thead>
- <tr>
- <th>ID</th>
- <th>Tittle</th>
- <th>Price</th>
- <th colspan="2">Action</th>
- </tr>
- </thead>
- <tbody>
- <%
- List<Books> list = (List<Books>)request.getAttribute("books");
- for (Books b : list) {
- %>
- <tr>
- <td><%= b.getBookID() %></td>
- <td><%= b.getTitle()%></td>
- <td><%= b.getPrice()%></td>
- <td><a href="ControllerServlet?action=update&BookID=<%=b.getBookID()%>">Update</a></td>
- <td><a href="ControllerServlet?action=delete&BookID=<%=b.getBookID()%>" onclick="return confirm('Are you sure to delete this?')">Delete</a></td>this?')">Delete</a></td>
- </tr>
- <%
- }
- %>
- </tbody>
- </table>
- 8. Tạo trang add.jsp và thêm đoạn sau
- <h1>Book - Add new</h1>
- <div><a href="ControllerServlet">Back to Index</a></div>
- <form action="ControllerServlet?action=addProcess" method="POST">
- <table>
- <tbody>
- <tr>
- <td>ID:</td>
- <td><input type="text" name="BookID"></td>
- </tr>
- <tr>
- <td>Title:</td>
- <td><input type="text" name="Title"></td>
- </tr>
- <tr>
- <td>Price:</td>
- <td><input type="number" name="Price"></td>
- </tr>
- <tr>
- <td></td>
- <td><input type="submit" name="btnAdd" value="Add this Book"></td>
- </tr>
- </tbody>
- </table>
- </form>
- 9. Tạo trang update.jsp và thêm đoạn sau
- <h1>Book - Update</h1>
- <p style="color:green">
- <%
- if (session.getAttribute("success") != null) {
- out.print(session.getAttribute("success"));
- session.removeAttribute("success");
- }
- %>
- </p>
- <div><a href="ControllerServlet">Back to Index</a></div>
- <form action="ControllerServlet?action=updateProcess" method="POST">
- <table >
- <tbody>
- <tr>
- <td>ID:</td>
- <td><input type="number" name="BookID" value="${book.bookID}" readonly="true" style="background-color: lightgray"></td>
- </tr>
- <tr>
- <td>Title:</td>
- <td><input type="text" name="Title" value="${book.title}"></td>
- </tr>
- <tr>
- <td>Price:</td>
- <td><input type="number" name="Price" value="${book.price}"></td>
- </tr>
- <tr>
- <td></td>
- <td><input type="submit" name="btnAdd" value="Update this Book"></td>
- </tr>
- </tbody>
- </table>
- </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement