Workshop03 PRJ321 Tran PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Subject: PRJ321

Workshop 03: JSP and Java beans


--------------------------------------------------------------------------------------------------------
RULES: Ws 03 should be submited on the LMS system
Contact me @ https://www.facebook.com/quynhtran.ly.94
--------------------------------------------------------------------------------------------------------

Students kindly practice these questions in class and at home based on the
Lecturer’s guidance. Thank you :-D
 Question 0: EL

JSP Scriplet!
CODE

FINISH
scriptingElements!
CODE
FINISH

ArrayList!
CODE

FINISH
 Question 01: Write JSP Application with JDBC

1. Create a Database
 UserTBL(username, password,displayname)
 Add few records to the table

Username Password DisplayName


Admin Admin Administrator
Mod Mod Moderator

2. Create a login.jsp as following:

3. When user click login


 IF username and password correct -> redirect user to Detail.jsp . Otherwise
stays in login.jsp
 If User try to access Detail.jsp without login -> redirect user back to the
login.jsp. However if user checked to ‘Remember me’ in the previous login.
System must allow user to access the page without requiring login again.
4. The detail page displays the attr DisplayName of the corresponding username:
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebLoginJSPJDBCDemo\web\index.jsp
1 <%--
2 Document : index
3 Created on : 09/09/2020, 1:38:40 PM
4 Author : Ly Quynh Tran
5 --%>
6
7 <%@page contentType="text/html" pageEncoding="UTF-8"%>
8 <%@ include file="/includes/header.jsp" %>
9 <%@ include file="/includes/left.jsp" %>
10 <%@ include file="/includes/navbar.jsp" %>
11
12
13 <section>
14 <div class="templatemo-content-container">
15 <div class="templatemo-content-widget white-bg">
16 <h2 class="margin-bottom-10">Student Management System</h2>
17 <div class="panel panel-default no-border">
18 <div class="panel-heading border-radius-10">
19 <h2>Login</h2>
20 </div>
21 <br><br>
22 <form name="myForm" action="details.jsp" method="POST">
23 <table border="0">
24 <tbody>
25 <tr>
26 <td>User Name: </td>
27 <td><input type="text" name="username" value=""
size="30" /></td>
28 </tr>
29 <tr>
30 <td>Password: </td>
31 <td><input type="text" name="password" value=""
size="30" /></td>
32 </tr>
33 </tbody>
34 </table>
35 <hr>
36 <input type="submit" value="Login" name="submit" />
37 </form>
38
39 <div class="panel-body">
40 <div class="templatemo-flex-row flex-content-row margin-
bottom-30">
41 <div class="col-1">
42 <div id="gauge_div" class="templatemo-chart"></div>
43 <h3 class="text-center margin-bottom-5"></h3>
44 <p class="text-center"></p>
45 </div>
46 </div>
47 </div>
48 </div>
49 </section>
50 <%@ include file="/includes/footer.jsp" %>
51

C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebLoginJSPJDBCDemo\web\details.js
p
1 <%--
2 Document : index
3 Created on : 09/09/2020, 1:38:40 PM
4 Author : Ly Quynh Tran
5 --%>
6
7 <%@page import="model.UserService"%>
8 <%@page import="model.User"%>
9 <%@page contentType="text/html" pageEncoding="UTF-8"%>
10 <%@ include file="/includes/header.jsp" %>
11 <%@ include file="/includes/left.jsp" %>
12 <%@ include file="/includes/navbar.jsp" %>
13
14
15 <section>
16 <div class="templatemo-content-container">
17 <div class="templatemo-content-widget white-bg">
18 <h2 class="margin-bottom-10">Student Management System</h2>
19 <div class="panel panel-default no-border">
20 <div class="panel-heading border-radius-10">
21 <h2>Login Status</h2>
22 </div>
23 <h2 class="margin-bottom-10">
24 <%
25
26 String username = request.getParameter("username");
27 String password = request.getParameter("password");
28 User user = new User(username, password);
29 UserService service = new UserService();
30 user = service.login(username, password);
31 if (user == null) {
32 out.println("Cant find");
33
34 } else {
35 out.println("Hello " + user.getDisplayname());
36 }
37
38 %>
39 </h2>
40
41
42 <div class="panel-body">
43 <div class="templatemo-flex-row flex-content-row margin-
bottom-30">
44 <div class="col-1">
45 <div id="gauge_div" class="templatemo-chart"></div>
46 <h3 class="text-center margin-bottom-5"></h3>
47 <p class="text-center"></p>
48 </div>
49 </div>
50 </div>
51 </div>
52 </section>
53 <%@ include file="/includes/footer.jsp" %>
54

C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebLoginJSPJDBCDemo\src\java\dao\
ConnectDB.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 dao;
7
8 import java.sql.Connection;
9 import java.sql.DriverManager;
10
11 /**
12 *
13 * @author Ly Quynh Tran
14 */
15 public class ConnectDB implements DatabaseInfor {
16
17 private static ConnectDB instance;
18
19 public ConnectDB() {
20 }
21
22 public Connection OpenConnection() throws Exception {
23
24 Class.forName(driverName);
25 Connection con = DriverManager.getConnection(url, user, pass);
26 return con;
27 }
28 //Get instance of dbms only one time
29 public static ConnectDB getInstance(){
30 if(instance==null) instance = new ConnectDB();
31 return instance;
32 }
33 }
34

C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebLoginJSPJDBCDemo\src\java\dao\
DatabaseInfor.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 dao;
7
8 /**
9 *
10 * @author Ly Quynh Tran
11 */
12 public interface DatabaseInfor {
13
14 public static String
driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
15 public static String
url="jdbc:sqlserver://127.0.0.1:1433;databaseName=UserLogin;";
16 public static String user="sa";
17 public static String pass="abc123";
18 }
19

C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebLoginJSPJDBCDemo\src\java\dao\
TestDB.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 dao;
7
8 import java.sql.Connection;
9 import java.util.ArrayList;
10 import java.util.List;
11 import model.User;
12 import model.UserService;
13
14 /**
15 *
16 * @author Ly Quynh Tran
17 */
18 public class TestDB {
19
20 /**
21 * @param args the command line arguments
22 */
23 public static void main(String[] args) throws Exception {
24 // TODO code application logic here
25
26 ConnectDB db = ConnectDB.getInstance();
27 Connection con = db.OpenConnection();
28 UserDAO userDAO = new UserDAO();
29 List<User> l = new ArrayList<>();
30 l = userDAO.findAll();
31 System.out.println(""+l);
32 UserService us = new UserService();
33 User s = us.login("Mod", "Mod");
34 System.out.println(""+s);
35 }
36
37 }
38

C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebLoginJSPJDBCDemo\src\java\dao\
UserDAO.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 dao;
7
8 import java.sql.Connection;
9 import java.sql.PreparedStatement;
10 import java.sql.ResultSet;
11 import java.util.ArrayList;
12 import java.util.List;
13 import model.User;
14
15 /**
16 *
17 * @author Ly Quynh Tran
18 */
19 public class UserDAO {
20
21 private List<User> list = new ArrayList<>();
22
23 public UserDAO() {
24 }
25
26 public User getUser(String name) {
27
28 String username, password, displayname;
29 User user = null;
30 String sql = "Select * from UserTBL where username='" + name + "'";
31 try {
32 ConnectDB db = ConnectDB.getInstance();
33 Connection con = db.OpenConnection();
34 PreparedStatement pstmt = con.prepareStatement(sql);
35 ResultSet rs = pstmt.executeQuery();
36 if (rs.next()) {
37 username = rs.getString("username");
38 password = rs.getString("password");
39 displayname = rs.getString("displayname");
40 user = new User(username, password, displayname);
41 con.close();
42 System.out.println("Get user sucessfully");
43 } else {
44 System.out.println("This user doesnot exist");
45 }
46
47 } catch (Exception e) {
48 }
49 return user;
50 }
51
52 public List<User> findAll() {
53
54 String sql = "Select * from UserTBL";
55 try {
56 ConnectDB db = ConnectDB.getInstance();
57 Connection con = db.OpenConnection();
58 PreparedStatement pstmt = con.prepareStatement(sql);
59 ResultSet rs = pstmt.executeQuery();
60 while (rs.next()) {
61 User user = new User();
62 user.setUsername(rs.getString("username"));
63 user.setUsername(rs.getString("password"));
64 user.setUsername(rs.getString("displayname"));
65
66 list.add(user);
67 }
68 con.close();
69 return list;
70 } catch (Exception ex) {
71 System.out.println(ex);
72 }
73 return null;
74 }
75
76 public List<User> getList() {
77 return list;
78 }
79
80 public void setList(List<User> list) {
81 this.list = list;
82 }
83
84 }
85

C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebLoginJSPJDBCDemo\src\java\mod
el\User.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 dao.UserDAO;
9
10 /**
11 *
12 * @author Ly Quynh Tran
13 */
14 public class User {
15 private String username;
16 private String password;
17 private String displayname;
18
19 public User() {
20 }
21
22 public User(String username, String password, String displayname) {
23 this.username = username;
24 this.password = password;
25 this.displayname = displayname;
26 }
27
28 public User(String username, String password) {
29 this.username = username;
30 this.password = password;
31
32 }
33 public String getUsername() {
34 return username;
35 }
36
37 public void setUsername(String username) {
38 this.username = username;
39 }
40
41 public String getPassword() {
42 return password;
43 }
44
45 public void setPassword(String password) {
46 this.password = password;
47 }
48
49 public String getDisplayname() {
50 return displayname;
51 }
52
53 public void setDisplayname(String displayname) {
54 this.displayname = displayname;
55 }
56
57 @Override
58 public String toString() {
59 return "User{" + "username=" + username + ", password=" + password
+ ", displayname=" + displayname + '}';
60 }
61
62
63 }
64

C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebLoginJSPJDBCDemo\src\java\mod
el\UserService.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 dao.UserDAO;
9
10 /**
11 *
12 * @author Ly Quynh Tran
13 */
14 public class UserService {
15
16 public UserService() {
17 }
18 public User login(String username, String password){
19 User u= new UserDAO().getUser(username);
20
21 if(u.getPassword().trim().equals(password)) return u;
22 return null;
23 }
24 }
25
Link to download the template
https://drive.google.com/drive/u/1/folders/1pdGm-q1Ps64HMjBVaadb5xxvslAcXjnA

 Question 02: Write JSP Application with JDBC: Product DAO


C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebProductJSPJDBCDemo\web\index.
html
1 <!DOCTYPE html>
2 <!--
3 To change this license header, choose License Headers in Project Properties.
4 To change this template file, choose Tools | Templates
5 and open the template in the editor.
6 -->
7 <html>
8 <head>
9 <title>Show List </title>
10 <meta charset="UTF-8">
11 <meta name="viewport" content="width=device-width, initial-
scale=1.0">
12 </head>
13 <body>
14 <a href="ShowListController">Show List</a>
15 </body>
16 </html>
17

C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebProductJSPJDBCDemo\web\inputp
roduct.jsp
1 <%--
2 Document : inputproduct
3 Created on : 28/09/2020, 11:05:05 AM
4 Author : Ly Quynh Tran
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 <h1>Hello World!</h1>
16 </body>
17 </html>
18

C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebProductJSPJDBCDemo\web\showli
st.jsp
1 <%--
2 Document : showlist
3 Created on : 28/09/2020, 10:48:27 AM
4 Author : Ly Quynh Tran
5 --%>
6
7 <%@page import="java.util.List"%>
8 <%@page import="entity.Product"%>
9 <%@page import="Model.ProductsDAO"%>
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>Show List</title>
16 </head>
17 <body>
18 <table border="1">
19 <tr bgcolor="00FF7F">
20 <th><b>Code</b></th>
21 <th><b>Name</b></th>
22 <th><b>Price</b></th>
23
24 </tr>
25 <%-- Fetching the attributes of the request object
26 which was previously set by the servlet
27 "StudentServlet.java"
28 --%>
29 <%List<Product> l = (List<Product>) request.getAttribute("RS");
30 for (Product p : l) {%>
31 <%-- Arranging data in tabular form
32 --%>
33 <tr>
34 <td><%=p.getCode()%></td>
35 <td><%=p.getName()%></td>
36 <td><%=p.getPrice()%></td>
37
38 </tr>
39 <%}%>
40 </table>
41 </body>
42 </html>
43
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebProductJSPJDBCDemo\src\java\In
putProductController.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
7 import java.io.IOException;
8 import java.io.PrintWriter;
9 import javax.servlet.ServletException;
10 import javax.servlet.http.HttpServlet;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13
14 /**
15 *
16 * @author Ly Quynh Tran
17 */
18 public class InputProductController extends HttpServlet {
19
20 /**
21 * Processes requests for both HTTP <code>GET</code> and
<code>POST</code>
22 * methods.
23 *
24 * @param request servlet request
25 * @param response servlet response
26 * @throws ServletException if a servlet-specific error occurs
27 * @throws IOException if an I/O error occurs
28 */
29 protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
30 throws ServletException, IOException {
31 response.setContentType("text/html;charset=UTF-8");
32 try (PrintWriter out = response.getWriter()) {
33 /* TODO output your page here. You may use following sample code.
*/
34 out.println("<!DOCTYPE html>");
35 out.println("<html>");
36 out.println("<head>");
37 out.println("<title>Servlet InputProductController</title>");
38 out.println("</head>");
39 out.println("<body>");
40 out.println("<h1>Servlet InputProductController at " +
request.getContextPath() + "</h1>");
41 out.println("</body>");
42 out.println("</html>");
43 }
44 }
45
46 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods.
Click on the + sign on the left to edit the code.">
47 /**
48 * Handles the HTTP <code>GET</code> method.
49 *
50 * @param request servlet request
51 * @param response servlet response
52 * @throws ServletException if a servlet-specific error occurs
53 * @throws IOException if an I/O error occurs
54 */
55 @Override
56 protected void doGet(HttpServletRequest request, HttpServletResponse
response)
57 throws ServletException, IOException {
58 processRequest(request, response);
59 }
60
61 /**
62 * Handles the HTTP <code>POST</code> method.
63 *
64 * @param request servlet request
65 * @param response servlet response
66 * @throws ServletException if a servlet-specific error occurs
67 * @throws IOException if an I/O error occurs
68 */
69 @Override
70 protected void doPost(HttpServletRequest request,
HttpServletResponse response)
71 throws ServletException, IOException {
72 processRequest(request, response);
73 }
74
75 /**
76 * Returns a short description of the servlet.
77 *
78 * @return a String containing servlet description
79 */
80 @Override
81 public String getServletInfo() {
82 return "Short description";
83 }// </editor-fold>
84
85 }
86
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebProductJSPJDBCDemo\src\java\Sh
owListController.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
7 import Model.ProductsDAO;
8 import entity.Product;
9 import java.io.IOException;
10 import java.io.PrintWriter;
11 import java.util.ArrayList;
12 import java.util.List;
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 Ly Quynh Tran
21 */
22 public class ShowListController 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 ShowListController</title>");
42 out.println("</head>");
43 out.println("<body>");
44 out.println("<h1>Servlet ShowListController at " +
request.getContextPath() + "</h1>");
45 out.println("</body>");
46 out.println("</html>");
47 }
48 }
49
50 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods.
Click on the + sign on the left to edit the code.">
51 /**
52 * Handles the HTTP <code>GET</code> method.
53 *
54 * @param request servlet request
55 * @param response servlet response
56 * @throws ServletException if a servlet-specific error occurs
57 * @throws IOException if an I/O error occurs
58 */
59 @Override
60 protected void doGet(HttpServletRequest request, HttpServletResponse
response)
61 throws ServletException, IOException {
62 ProductsDAO productsDAO = new ProductsDAO();
63
64 List<Product> list = new ArrayList<>();
65 list=productsDAO.showProduct("Dell");
66 request.setAttribute("RS", list);
67 request.getRequestDispatcher("showlist.jsp").include(request,
response);
68 }
69
70 /**
71 * Handles the HTTP <code>POST</code> method.
72 *
73 * @param request servlet request
74 * @param response servlet response
75 * @throws ServletException if a servlet-specific error occurs
76 * @throws IOException if an I/O error occurs
77 */
78 @Override
79 protected void doPost(HttpServletRequest request,
HttpServletResponse response)
80 throws ServletException, IOException {
81 processRequest(request, response);
82 }
83
84 /**
85 * Returns a short description of the servlet.
86 *
87 * @return a String containing servlet description
88 */
89 @Override
90 public String getServletInfo() {
91 return "Short description";
92 }// </editor-fold>
93
94 }
95
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebProductJSPJDBCDemo\src\java\M
odel\ProductsDAO.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 entity.Product;
10 import java.sql.Connection;
11 import java.sql.ResultSet;
12 import java.sql.Statement;
13 import java.util.ArrayList;
14 import java.util.List;
15
16 /**
17 *
18 * @author Ly Quynh Tran
19 */
20 public class ProductsDAO {
21
22 public ProductsDAO() {
23 }
24
25 public List<Product> showProduct(String productName) {
26 try {
27 //Connect database
28 ConnectDB db = ConnectDB.getInstance();
29 Connection con = db.openConnection();
30 String sql = "Select * from Products";
31 if (productName.length() > 0) {
32 sql += " where Name like '%" + productName + "%' ";
33 }
34 Statement stm = con.createStatement();
35 ResultSet rs = stm.executeQuery(sql);
36 List<Product> list = new ArrayList<>();
37 while (rs.next()) {
38 String code = rs.getString("Code");
39 String name = rs.getString("Name");
40 float price = rs.getFloat("Price");
41 Product sp = new Product(code, name, price);
42 list.add(sp);
43 }
44 con.close();
45 return list;
46
47 } catch (Exception e) {
48 e.printStackTrace();
49 }
50 return null;
51 }
52 }
53
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebProductJSPJDBCDemo\src\java\M
odel\TestDB.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 Ly Quynh Tran
11 */
12 public class TestDB {
13
14 /**
15 * @param args the command line arguments
16 */
17 public static void main(String[] args) {
18 // TODO code application logic here
19 ProductsDAO pd = new ProductsDAO();
20 pd.showProduct("Dell");
21 }
22
23 }
24
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebProductJSPJDBCDemo\src\java\co
ntext\ConnectDB.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 Ly Quynh Tran
14 */
15 public class ConnectDB implements DatabaseInfor {
16
17 private static ConnectDB instance;
18
19 public ConnectDB() {
20 }
21
22 public Connection openConnection() throws Exception {
23 Class.forName(driverName);
24 Connection con = DriverManager.getConnection(url, user, pass);
25 return con;
26
27 }
28
29 //Get instance of dbms only one time
30 public static ConnectDB getInstance() {
31 if (instance == null) {
32 instance = new ConnectDB();
33 }
34 return instance;
35 }
36 }
37
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebProductJSPJDBCDemo\src\java\co
ntext\DatabaseInfor.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 /**
9 *
10 * @author Ly Quynh Tran
11 */
12 public interface DatabaseInfor {
13 public static String
driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
14 public static String
url="jdbc:sqlserver://127.0.0.1:1433;databaseName=ProductDB;";
15 public static String user="sa";
16 public static String pass="abc123";
17 }
18
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebProductJSPJDBCDemo\src\java\en
tity\Product.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 entity;
7
8 /**
9 *
10 * @author Ly Quynh Tran
11 */
12 public class Product {
13
14 private String code;
15 private String name;
16 private float price;
17
18 public Product() {
19 }
20
21 public Product(String code, String name, float price) {
22 this.code = code;
23 this.name = name;
24 this.price = price;
25 }
26
27 public String getCode() {
28 return code;
29 }
30
31 public String getName() {
32 return name;
33 }
34
35 public float getPrice() {
36 return price;
37 }
38
39 public void setCode(String code) {
40 this.code = code;
41 }
42
43 public void setName(String name) {
44 this.name = name;
45 }
46
47 public void setPrice(float price) {
48 this.price = price;
49 }
50 }
51

Student is required to do the InputProduct function

JAVA BEANS

C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebProductJSPJDBCDemo\web\showli
stbeans.jsp
1 <%--
2 Document : showlistbeans
3 Created on : 28/09/2020, 8:04:15 PM
4 Author : Ly Quynh Tran
5 --%>
6
7 <%@page contentType="text/html" pageEncoding="UTF-8"%>
8 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
9 <!DOCTYPE html>
10 <html>
11 <head>
12 <jsp:useBean id="p" class="Model.ProductsDAO"
scope="request"></jsp:useBean>
13 <meta http-equiv="Content-Type" content="text/html; charset=UTF-
8">
14 <title>Show List</title>
15 </head>
16 <body>
17 <h1>PRODUCT!</h1>
18 <table border="1">
19 <tr>
20 <td>Code</td>
21 <td>Name</td>
22 <td>Price</td>
23 </tr>
24 <c:forEach items="${p.showProduct(null)}" var="i">
25 <tr>
26 <td>${i.code}</td>
27 <td>${i.name}</td>
28 <td>${i.price}</td>
29 </tr>
30 </c:forEach>
31 </table>
32
33 </body>
34 </html>
35

You might also like