Tải bản đầy đủ (.pdf) (10 trang)

Prj321Minitestsummer2021 (1).Pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (646.61 KB, 10 trang )

COMPUTER FUNDAMENTAL DEPARTMENT
DA NANG

1

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
1. Create database name PRJ321_YourID on MS SQL Server, have 1 table with sample data at
least 2 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
2. Create web application in NetBean:
a. Define Java Bean class link data to table
Magazine, add 2 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
1 /*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6 package context;
7
8 import java.sql.Connection;

9 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 con = 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
1 /*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6 package Model;
7
8 /**
9 *
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
1 /*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6 package Model;
7
8 import context.ConnectDB;
9 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<Magazine> getAll(String Id){
25
try{
26
//Connect database
27
ConnectDB db = ConnectDB.getInstance();
28
Connection con = 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<Magazine> 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 con = 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
1 /*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6 package Controllers;
7
8 import Model.Magazine;
9 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 <code>GET</code> and
<code>POST</code>
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("<!DOCTYPE html>");
39
out.println("<html>");
40
out.println("<head>");

41
out.println("<title>Servlet NewMagazineController</title>");
42
out.println("</head>");
43
out.println("<body>");
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("
method=\"post\">\n

" +
57
"
<input type=\"submit\" value=\"List all magazines\">
\n" +
58
"
</form>");
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("</body>");
71
out.println("</html>");
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 + 3 digits";
78
MagazinesDAO dao = new MagazinesDAO();
79
ArrayList<Magazine> 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 <code>GET</code> 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 <code>POST</code> 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 }// </editor-fold>
133
134 }
135
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WS1_Magazine_DE140248\web\index.jsp
1 <%-2 Document : Product
3 Created on : May 27, 2020, 8:19:33 AM
4 Author : MrEnd
5 --%>
6
7 <%@page import="java.util.ArrayList"%>
8 <%@page import="Model.Magazine"%>
9 <%@page import="Model.MagazinesDAO"%>
10 <%@page contentType="text/html" pageEncoding="UTF-8"%>
11 <!DOCTYPE html>
12 <html>
13 <head>
14
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
15
<title>JSP Page</title>
16 </head>
17 <body>

18

MAGAZINE!


19
<%
20
Integer hitsCount = (Integer)application.getAttribute("hitCounter");
21
if( hitsCount ==null || hitsCount == 0 ) {
22
/* First visit */
23
out.print("Welcome to my website! ");
24
hitsCount = 1;
25
} else {
26
/* return visit */
27
out.print("Welcome back to my website! ");
28
hitsCount += 1;
29
}
30
application.setAttribute("hitCounter", hitsCount);
31
out.println("Total number of visits: "+ hitsCount);
32
%>

33
<table border="1">
34



35
<tr>
36
<td>ID</td>
37
<td>Title</td>
38
<td>Publisher</td>
39
<td>Price</td>
40
</tr>
41
<%
42
MagazinesDAO ls = new MagazinesDAO();
43
ArrayList<Magazine> list = ls.getAll("");
44
for(Magazine m: list){


45
out.print("<tr>"
46
+"<td>" + m.getID()+"</td>"

47
+"<td>" + m.getTitle()+"</td>"
48
+"<td>" + m.getPublisher()+"</td>"
49
+"<td>" + m.getPrice()+"</td>"
50
+"</tr>"
51
);
52
}
53
%>
54
</table>
55
<form action = "Magazine.jsp">
56



57
<input type="submit" value="Add new Magazine">
58
</form>
59 </body>
60 </html>
61
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WS1_Magazine_DE140248\web\Magazine.jsp
1 <%-2 Document : Magazine

3 Created on : Jun 10, 2020, 7:57:10 AM
4 Author : MrEnd
5 --%>
6
7 <%@page contentType="text/html" pageEncoding="UTF-8"%>
8 <!DOCTYPE html>
9 <html>
10 <head>
11
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
12
<title>JSP Page</title>
13 </head>
14 <body>
15

Add new magazine!


16
<%
17
Integer hitsCount = (Integer)application.getAttribute("hitCounter");
18
if( hitsCount ==null || hitsCount == 0 ) {
19
/* First visit */
20
out.print("Welcome to my website! ");
21
hitsCount = 1;
22
} else {

23
/* return visit */
24
out.print("Welcome back to my website! ");
25
hitsCount += 1;
26
}
27
application.setAttribute("hitCounter", hitsCount);
28
out.println("Total number of visits: "+ hitsCount);
29
%>
30
<form action="NewMagazineController" method="post">
31





32
Maz ID:
33
<input type="text" name="id">


34
Title:
35
<input type="text" name="title">


36
Publisher

37
<input type="text" name="publisher">


38
Price:
39
<input type="text" name="price">


40



41
<input type="submit" value="Add User">
42
</form>
43 </body>
44 </html>
45



×