1. Trang chủ
  2. » Luận Văn - Báo Cáo

Prj321Minitestsummer2021 (1).Pdf

10 0 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

DA NANG COMPUTER FUNDAMENTAL DEPARTMENT Mini TEST Fall 2020 1 SUBJECT PRJ321 Duration xx minutes STUDENT INFORMATION Name Roll number Room No Class Develop web – database MVC application with followin[.]

COMPUTER FUNDAMENTAL DEPARTMENT DA NANG Mini TEST- Fall 2020 SUBJECT: PRJ321 Duration: xx minutes STUDENT INFORMATION Name: Roll number: Room No: Class: Develop web – database MVC application with following components and requirements Create database name PRJ321_YourID on MS SQL Server, have table with sample data at least rows (1 mark) Table name: Magazine_YourID Maz ID Magazine Title Publisher Price M001 Desktop Java Application IT House 12.5 M002 Web programming with JSP Youth 8.9 Create web application in NetBean: a Define Java Bean class link data to table Magazine, add following function (2 marks) + method getAll() return all in an ArrayList of Magazine + method newMagazine(Magazine c) will insert c into table Magazine b Create index.jsp display all Magazines and a button Add New Magazine link to Magazine.jsp (2 marks) c Create Magazine.jsp with input form for new Magazine, allow user to check/modify input data before send request to servlet Java bean should use for this purpose (2 marks) d Create servlet to handle request for new Magazine by calling method on Java bean On success of insert new Magazine on database, forward request back to index.jsp with new Magazine appended Otherwise, should display error page (2 marks) e Implement hit counter on all pages (1 mark) Guides C:\Users\Ly Quynh Tran\Documents\NetBeansProjects\WS1_Magazine_DE140248\src\java\context\Co nnectDB.java /* * To change this license header, choose License Headers in Project Properties * To change this template file, choose Tools | Templates * and open the template in the editor */ package context; import java.sql.Connection; import java.sql.DriverManager; 10 11 /** 12 * 13 * @author MrEnd 14 */ 15 public class ConnectDB { 16 17 private static ConnectDB instance; 18 19 public ConnectDB() { 20 } 21 22 public Connection openConnection() throws Exception{ 23 String connectionUrl ="jdbc:sqlserver://QH\\QH:1433;"+ 24 "databaseName=PRJ321_Magazine_DE140248;User=sa;Password=1712;"; 25 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 26 Connection = DriverManager.getConnection(connectionUrl); 27 return con; 28 } 29 30 //Get instance of dbms only one time 31 public static ConnectDB getInstance(){ 32 if(instance==null) instance = new ConnectDB(); 33 return instance; 34 } 35 } 36 C:\Users\Ly Quynh Tran\Documents\NetBeansProjects\WS1_Magazine_DE140248\src\java\Model\Ma gazine.java /* * To change this license header, choose License Headers in Project Properties * To change this template file, choose Tools | Templates * and open the template in the editor */ package Model; /** * 10 * @author MrEnd 11 */ 12 public class Magazine { 13 String ID, title, publisher; 14 double price; 15 16 public Magazine() { 17 } 18 19 public Magazine(String ID, String title, String publisher, double price) { 20 this.ID = ID; 21 this.title = title; 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 } 59 } this.publisher = publisher; this.price = price; public String getID() { return ID; } public String getTitle() { return title; } public String getPublisher() { return publisher; } public double getPrice() { return price; } public void setID(String ID) { this.ID = ID; } public void setTitle(String title) { this.title = title; } public void setPublisher(String publisher) { this.publisher = publisher; } public void setPrice(double price) { this.price = price; } C:\Users\Ly Quynh Tran\Documents\NetBeansProjects\WS1_Magazine_DE140248\src\java\Model\Ma gazinesDAO.java /* * To change this license header, choose License Headers in Project Properties * To change this template file, choose Tools | Templates * and open the template in the editor */ package Model; import context.ConnectDB; import java.sql.Connection; 10 import java.sql.PreparedStatement; 11 import java.sql.ResultSet; 12 import java.sql.Statement; 13 import java.util.ArrayList; 14 15 /** 16 * 17 * @author MrEnd 18 */ 19 public class MagazinesDAO { 20 21 public MagazinesDAO() { 22 } 23 24 public ArrayList getAll(String Id){ 25 try{ 26 //Connect database 27 ConnectDB db = ConnectDB.getInstance(); 28 Connection = db.openConnection(); 29 String sql = "Select * from Magazine_DE140248"; 30 if(Id!="") sql=sql+" where ID='"+Id+"'"; 31 Statement stm = con.createStatement(); 32 ResultSet rs = stm.executeQuery(sql); 33 ArrayList list = new ArrayList(); 34 while(rs.next()){ 35 String id = rs.getString("ID"); 36 String title = rs.getString("Title"); 37 String publisher = rs.getString("Publisher"); 38 double price = rs.getDouble("Price"); 39 Magazine m = new Magazine(id, title, publisher, price); 40 list.add(m); 41 } 42 System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@"+list); 43 return list; 44 } catch(Exception e){ 45 e.printStackTrace(); 46 } 47 return null; 48 } 49 50 public boolean newMagazine(Magazine m) throws Exception{ 51 String sql = "Insert into Magazine_DE140248 values(?,?,?,?)"; 52 try{ 53 ConnectDB db = ConnectDB.getInstance(); 54 Connection = db.openConnection(); 55 PreparedStatement pstmt = con.prepareStatement(sql); 56 pstmt.setString(1, m.getID()); 57 pstmt.setString(2, m.getTitle()); 58 pstmt.setString(3, m.getPublisher()); 59 pstmt.setDouble(4, m.getPrice()); 60 return pstmt.executeUpdate()>0; 61 } catch(Exception ex){ 62 System.out.println(ex); 63 } 64 return false; 65 } 66 } 67 C:\Users\Ly Quynh Tran\Documents\NetBeansProjects\WS1_Magazine_DE140248\src\java\Controllers \NewMagazineController.java /* * To change this license header, choose License Headers in Project Properties * To change this template file, choose Tools | Templates * and open the template in the editor */ package Controllers; import Model.Magazine; import Model.MagazinesDAO; 10 import java.io.IOException; 11 import java.io.PrintWriter; 12 import java.util.ArrayList; 13 import javax.servlet.ServletException; 14 import javax.servlet.http.HttpServlet; 15 import javax.servlet.http.HttpServletRequest; 16 import javax.servlet.http.HttpServletResponse; 17 18 /** 19 * 20 * @author MrEnd 21 */ 22 public class NewMagazineController extends HttpServlet { 23 24 /** 25 * Processes requests for both HTTP GET and POST 26 * methods 27 * 28 * @param request servlet request 29 * @param response servlet response 30 * @throws ServletException if a servlet-specific error occurs 31 * @throws IOException if an I/O error occurs 32 */ 33 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 34 throws ServletException, IOException { 35 response.setContentType("text/html;charset=UTF-8"); 36 try (PrintWriter out = response.getWriter()) { 37 /* TODO output your page here You may use following sample code */ 38 out.println(""); 39 out.println(""); 40 out.println(""); 41 out.println("Servlet NewMagazineController"); 42 out.println(""); 43 out.println(""); 44 out.println("Servlet NewMagazineController at " + request.getContextPath() + ""); 45 String id = request.getParameter("id"); 46 String title = request.getParameter("title"); 47 String publisher = request.getParameter("publisher"); 48 String p = request.getParameter("price"); 49 if(Validation(id,title,publisher,p) == ""){ 50 Magazine m = new Magazine(id,title,publisher,Double.parseDouble(p)); 51 try{ 52 MagazinesDAO dao = new MagazinesDAO(); 53 if (dao.newMagazine(m)){ 54 out.print("New Magazine Inserted!!!"); 55 //forward back to index.jsp 56 out.print("\n" + 57 " \n" + 58 " "); 59 } else{ 60 out.print("FAIL"); 61 } 62 } catch(Exception e){ 63 e.printStackTrace(); 64 out.print("ERROR: "+e.getMessage()); 65 } 66 } else{ 67 out.println(Validation(id,title,publisher,p)); 68 } 69 70 out.println(""); 71 out.println(""); 72 } 73 } 74 75 public String Validation(String id, String title, String publisher, String price){ 76 if(!id.matches("^M\\d{3}$")) 77 return "Maz must follow M + digits"; 78 MagazinesDAO dao = new MagazinesDAO(); 79 ArrayList list = dao.getAll(id); 80 //check if maz id existed in database 81 if(!list.isEmpty()) 82 return "This maz id existed"; 83 if(title=="") 84 return "Please input title"; 85 if(publisher=="") 86 return "Please input publisher"; 87 try{ 88 double p = Double.parseDouble(price); 89 } catch(Exception e){ 90 return "Price must be a number"; 91 } 92 return ""; 93 } 94 95 // 96 /** 97 * Handles the HTTP GET method 98 * 99 * @param request servlet request 100 * @param response servlet response 101 * @throws ServletException if a servlet-specific error occurs 102 * @throws IOException if an I/O error occurs 103 */ 104 @Override 105 protected void doGet(HttpServletRequest request, HttpServletResponse response) 106 throws ServletException, IOException { 107 processRequest(request, response); 108 } 109 110 /** 111 * Handles the HTTP POST method 112 * 113 * @param request servlet request 114 * @param response servlet response 115 * @throws ServletException if a servlet-specific error occurs 116 * @throws IOException if an I/O error occurs 117 */ 118 @Override 119 protected void doPost(HttpServletRequest request, HttpServletResponse response) 120 throws ServletException, IOException { 121 processRequest(request, response); 122 } 123 124 /** 125 * Returns a short description of the servlet 126 * 127 * @return a String containing servlet description 128 */ 129 @Override 130 public String getServletInfo() { 131 return "Short description"; 132 }// 133 134 } 135 C:\Users\Ly Quynh Tran\Documents\NetBeansProjects\WS1_Magazine_DE140248\web\index.jsp 10 11 12 13 14 15 JSP Page 16 17 18 MAGAZINE! 19 33 34 35 36 ID 37 Title 38 Publisher 39 Price 40 41 54 55 56 57 58 59 60 61 C:\Users\Ly Quynh Tran\Documents\NetBeansProjects\WS1_Magazine_DE140248\web\Magazine.jsp 10 11 12 JSP Page 13 14 15 Add new magazine! 16 30 31 32 Maz ID: 33 34 Title: 35 36 Publisher 37 38 Price: 39 40 41 42 43 44 45

Ngày đăng: 21/02/2024, 10:37

Xem thêm: