You are on page 1of 16

Subject: PRJ321

Workshop 04: JSP and Java beans


--------------------------------------------------------------------------------------------------------
RULES: Ws 04 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 1:
USE [master]
GO
/****** Object: Database [ELDB] Script Date: 6/10/2020 9:40:26 AM ******/
CREATE DATABASE [ELDB]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'ELDB', FILENAME = N'C:\Program Files\Microsoft SQL
Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\ELDB.mdf' , SIZE =
4096KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N'ELDB_log', FILENAME = N'C:\Program Files\Microsoft SQL
Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\ELDB_log.ldf' , SIZE =
1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
ALTER DATABASE [ELDB] SET COMPATIBILITY_LEVEL = 120
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [ELDB].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [ELDB] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [ELDB] SET ANSI_NULLS OFF
GO
ALTER DATABASE [ELDB] SET ANSI_PADDING OFF
GO
ALTER DATABASE [ELDB] SET ANSI_WARNINGS OFF
GO
ALTER DATABASE [ELDB] SET ARITHABORT OFF
GO
ALTER DATABASE [ELDB] SET AUTO_CLOSE OFF
GO
ALTER DATABASE [ELDB] SET AUTO_SHRINK OFF
GO
ALTER DATABASE [ELDB] SET AUTO_UPDATE_STATISTICS ON
GO
ALTER DATABASE [ELDB] SET CURSOR_CLOSE_ON_COMMIT OFF
GO
ALTER DATABASE [ELDB] SET CURSOR_DEFAULT GLOBAL
GO
ALTER DATABASE [ELDB] SET CONCAT_NULL_YIELDS_NULL OFF
GO
ALTER DATABASE [ELDB] SET NUMERIC_ROUNDABORT OFF
GO
ALTER DATABASE [ELDB] SET QUOTED_IDENTIFIER OFF
GO
ALTER DATABASE [ELDB] SET RECURSIVE_TRIGGERS OFF
GO
ALTER DATABASE [ELDB] SET DISABLE_BROKER
GO
ALTER DATABASE [ELDB] SET AUTO_UPDATE_STATISTICS_ASYNC
OFF
GO
ALTER DATABASE [ELDB] SET DATE_CORRELATION_OPTIMIZATION
OFF
GO
ALTER DATABASE [ELDB] SET TRUSTWORTHY OFF
GO
ALTER DATABASE [ELDB] SET ALLOW_SNAPSHOT_ISOLATION OFF
GO
ALTER DATABASE [ELDB] SET PARAMETERIZATION SIMPLE
GO
ALTER DATABASE [ELDB] SET READ_COMMITTED_SNAPSHOT OFF
GO
ALTER DATABASE [ELDB] SET HONOR_BROKER_PRIORITY OFF
GO
ALTER DATABASE [ELDB] SET RECOVERY FULL
GO
ALTER DATABASE [ELDB] SET MULTI_USER
GO
ALTER DATABASE [ELDB] SET PAGE_VERIFY CHECKSUM
GO
ALTER DATABASE [ELDB] SET DB_CHAINING OFF
GO
ALTER DATABASE [ELDB] SET FILESTREAM(
NON_TRANSACTED_ACCESS = OFF )
GO
ALTER DATABASE [ELDB] SET TARGET_RECOVERY_TIME = 0
SECONDS
GO
ALTER DATABASE [ELDB] SET DELAYED_DURABILITY = DISABLED
GO
EXEC sys.sp_db_vardecimal_storage_format N'ELDB', N'ON'
GO
USE [ELDB]
GO
/****** Object: Table [dbo].[StudentFap] Script Date: 6/10/2020 9:40:26 AM
******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[StudentFap](
[No] [int] NOT NULL,
[Group] [nchar](10) NOT NULL,
[Code] [nchar](10) NOT NULL,
[Name] [nvarchar](50) NOT NULL
) ON [PRIMARY]

GO
INSERT [dbo].[StudentFap] ([No], [Group], [Code], [Name]) VALUES (111,
N'AA ', N'STU001 ', N'Mr A')
INSERT [dbo].[StudentFap] ([No], [Group], [Code], [Name]) VALUES (222,
N'AA ', N'STU002 ', N'Mr B')
INSERT [dbo].[StudentFap] ([No], [Group], [Code], [Name]) VALUES (333,
N'BB ', N'STU003 ', N'Mr C')
USE [master]
GO
ALTER DATABASE [ELDB] SET READ_WRITE
GO

C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebExpressionLDemo\src\java\model
\Student.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 java.sql.Date;
9
10 /**
11 *
12 * @author Ly Quynh Tran
13 */
14 public class Student {
15 private int no;
16 private String group;
17 private String code;
18 private String name;
19
20
21 public Student() {
22 }
23
24 public Student(int no, String group, String code, String name) {
25 this.no = no;
26 this.group = group;
27 this.code = code;
28 this.name = name;
29 }
30
31
32
33 public int getNo() {
34 return no;
35 }
36
37 public void setNo(int no) {
38 this.no = no;
39 }
40
41 public String getGroup() {
42 return group;
43 }
44
45 public void setGroup(String group) {
46 this.group = group;
47 }
48
49 public String getCode() {
50 return code;
51 }
52
53 public void setCode(String code) {
54 this.code = code;
55 }
56
57 public String getName() {
58 return name;
59 }
60
61 public void setName(String name) {
62 this.name = name;
63 }
64
65 @Override
66 public String toString() {
67 return "Student{" + "no=" + no + ", group=" + group + ", code=" + code
+ ", name=" + name + '}';
68 }
69
70
71
72 }
73
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebExpressionLDemo\src\java\db\Da
tabaseInfor.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 db;
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=ELDB;";
15 public static String user = "sa";
16 public static String pass = "abc123";
17 }
18
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebExpressionLDemo\src\java\db\DB
Connect.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 db;
7
8 import java.sql.Connection;
9 import java.sql.DriverManager;
10
11 /**
12 *
13 * @author Ly Quynh Tran
14 */
15 public class DBConnect implements DatabaseInfor {
16
17 private static DBConnect instance;
18
19 public DBConnect() {
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 DBConnect getInstance() {
31 if (instance == null) {
32 instance = new DBConnect();
33 }
34 return instance;
35 }
36 }
37
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebExpressionLDemo\src\java\dao\T
estDB.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.util.ArrayList;
9 import model.Student;
10
11 /**
12 *
13 * @author Ly Quynh Tran
14 */
15 public class TestDB {
16
17 /**
18 * @param args the command line arguments
19 */
20 public static void main(String[] args) {
21 // TODO code application logic here
22 StudentDAO studentDAO = new StudentDAO();
23 ArrayList<Student> listStudent = studentDAO.getAllStudent();
24 System.out.println(""+listStudent);
25
26 }
27
28 }
29
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebExpressionLDemo\src\java\dao\St
udentDAO.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 db.DBConnect;
9 import java.sql.Connection;
10 import java.sql.PreparedStatement;
11 import java.sql.ResultSet;
12 import java.sql.SQLException;
13 import java.sql.Statement;
14 import java.util.ArrayList;
15 import model.Student;
16
17 /**
18 *
19 * @author Ly Quynh Tran
20 */
21 public class StudentDAO {
22
23 public ArrayList<Student> getAllStudent() {
24 ArrayList<Student> listStudent = new ArrayList<>();
25 Connection con = null;
26 DBConnect db = new DBConnect();
27 try {
28 con = db.getInstance().openConnection();
29 Statement stmt = con.createStatement();
30 ResultSet rs = stmt.executeQuery("select * from StudentFap");
31 while (rs.next()) {
32 Student student = new Student();
33 student.setNo(rs.getInt(1));
34 student.setGroup(rs.getString(2));
35 student.setCode(rs.getString(3));
36 student.setName(rs.getString(4));
37
38 listStudent.add(student);
39 }
40 rs.close();
41 stmt.close();
42 con.close();
43 } catch (Exception ex) {
44 ex.printStackTrace();
45 }
46 return listStudent;
47 }
48
49
50
51
52 }
53
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebExpressionLDemo\src\java\contro
ller\AddStudentServlet.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 controller;
7
8 import dao.StudentDAO;
9 import java.io.IOException;
10 import java.io.PrintWriter;
11 import java.sql.Date;
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 import model.Student;
18
19 /**
20 *
21 * @author Ly Quynh Tran
22 */
23 public class AddStudentServlet extends HttpServlet {
24
25 /**
26 * Processes requests for both HTTP <code>GET</code> and
<code>POST</code>
27 * methods.
28 *
29 * @param request servlet request
30 * @param response servlet response
31 * @throws ServletException if a servlet-specific error occurs
32 * @throws IOException if an I/O error occurs
33 */
34 protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
35 throws ServletException, IOException {
36 response.setContentType("text/html;charset=UTF-8");
37 try (PrintWriter out = response.getWriter()) {
38 /* TODO output your page here. You may use following sample code.
*/
39 out.println("<!DOCTYPE html>");
40 out.println("<html>");
41 out.println("<head>");
42 out.println("<title>Servlet AddStudentServlet</title>");
43 out.println("</head>");
44 out.println("<body>");
45 out.println("<h1>Servlet AddStudentServlet at " +
request.getContextPath() + "</h1>");
46 out.println("</body>");
47 out.println("</html>");
48 }
49 }
50
51 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods.
Click on the + sign on the left to edit the code.">
52 /**
53 * Handles the HTTP <code>GET</code> method.
54 *
55 * @param request servlet request
56 * @param response servlet response
57 * @throws ServletException if a servlet-specific error occurs
58 * @throws IOException if an I/O error occurs
59 */
60 @Override
61 protected void doGet(HttpServletRequest request, HttpServletResponse
response)
62 throws ServletException, IOException {
63 StudentDAO studentDAO = new StudentDAO();
64 ArrayList<Student> listStudent = studentDAO.getAllStudent();
65 request.setAttribute("listStudent", listStudent);
66 request.getRequestDispatcher("ShowAllStudent.jsp").forward(request,
response);
67 }
68
69 /**
70 * Handles the HTTP <code>POST</code> method.
71 *
72 * @param request servlet request
73 * @param response servlet response
74 * @throws ServletException if a servlet-specific error occurs
75 * @throws IOException if an I/O error occurs
76 */
77 @Override
78 protected void doPost(HttpServletRequest request,
HttpServletResponse response)
79 throws ServletException, IOException {
80
81 }
82
83 /**
84 * Returns a short description of the servlet.
85 *
86 * @return a String containing servlet description
87 */
88 @Override
89 public String getServletInfo() {
90 return "Short description";
91 }// </editor-fold>
92
93 }
94
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebExpressionLDemo\web\ShowAllSt
udent.jsp
1 <%--
2 Document : ShowAllStudent
3 Created on : 05/10/2020, 9:21:35 PM
4 Author : Ly Quynh Tran
5 --%>
6
7 <%@page import="java.util.ArrayList"%>
8 <%@page import="model.Student"%>
9 <%@page import="dao.StudentDAO"%>
10 <%@page contentType="text/html" pageEncoding="UTF-8"%>
11 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
12
13 <!DOCTYPE html>
14 <html>
15 <head>
16 <meta http-equiv="Content-Type" content="text/html; charset=UTF-
8">
17 <title>Show Result</title>
18
19 </head>
20 <body>
21 <table border="1">
22 <!-- here should go some titles... -->
23 <tr>
24 <th>No</th>
25 <th>Name</th>
26 <th>Code</th>
27 <th>Group</th>
28 </tr>
29 <c:forEach var="thestudent" items="${listStudent}">
30 <tr>
31 <td>
32 <c:out value="${thestudent.no}" />
33 </td>
34 <td>
35 <c:out value="${thestudent.name}" />
36 </td>
37 <td>
38 <c:out value="${thestudent.code}" />
39 </td>
40 <td>
41 <c:out value="${thestudent.group}" />
42 </td>
43 </tr>
44 </c:forEach>
45 </table>
46
47 </body>
48 </html>
49
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebExpressionLDemo\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>TODO supply a title</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="AddStudentServlet">Show List</a>
15 </body>
16 </html>
17

JSTL option
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebExpressionLDemo\web\practiceJS
TL.jsp
1 <%--
2 Document : practiceJSTL
3 Created on : 05/10/2020, 10:37:05 PM
4 Author : Ly Quynh Tran
5 --%>
6
7 <%@page contentType="text/html" pageEncoding="UTF-8"%>
8 <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
9 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
10 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
11
12 <!DOCTYPE html>
13 <html>
14 <head>
15 <meta http-equiv="Content-Type" content="text/html; charset=UTF-
8">
16 <title>JSP Page</title>
17 </head>
18 <body>
19 <!--SQL-->
20 <sql:setDataSource
21 var="db"
22 driver="com.microsoft.sqlserver.jdbc.SQLServerDriver"
23 url="jdbc:sqlserver://localhost:1433; databaseName=ELDB; user =
sa; password = 123456"
24 user="sa"
25 password="abc123"></sql:setDataSource>
26 <sql:query var="result" dataSource="${db}" sql="SELECT * FROM
dbo.StudentFap">
27 </sql:query>
28 <c:forEach items="${result.rows}" var="student">
29 <c:out value="${student.name}"></c:out><br>
30 </c:forEach>
31
32
33 </body>
34 </html>
35

You might also like