Subject: PRJ321
Workshop 02: Servlet
--------------------------------------------------------------------------------------------------------
RULES: Ws 0 2 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 00: Demo
❖ C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\servletdemo2021\src\java\servletdemo\
HelloWorldServlet.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 servletdemo;
7
8 import java.io.IOException;
9 import java.io.PrintWriter;
10 import javax.servlet.ServletException;
11 import javax.servlet.annotation.WebServlet;
12 import javax.servlet.http.HttpServlet;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15
16 /**
17 *
18 * @author Ly Quynh Tran
19 */
20
21 public class HelloWorldServlet extends HttpServlet {
22
23 /**
24 * Processes requests for both HTTP <code>GET</code> and
<code>POST</code>
25 * methods.
26 *
27 * @param request servlet request
28 * @param response servlet response
29 * @throws ServletException if a servlet-specific error occurs
30 * @throws IOException if an I/O error occurs
31 */
32 protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
33 throws ServletException, IOException {
34 response.setContentType("text/html;charset=UTF-8");
35 try (PrintWriter out = response.getWriter()) {
36 /* TODO output your page here. You may use following sample code.
*/
37 out.println("<!DOCTYPE html>");
38 out.println("<html>");
39 out.println("<head>");
40 out.println("<title>Servlet HelloWorldServlet</title>");
41 out.println("</head>");
42 out.println("<body>");
43 out.println("<h1>Servlet HelloWorldServlet at " +
request.getContextPath() + "</h1>");
44 out.println("</body>");
45 out.println("</html>");
46 }
47 }
48
49 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on
the + sign on the left to edit the code.">
50 /**
51 * Handles the HTTP <code>GET</code> method.
52 *
53 * @param request servlet request
54 * @param response servlet response
55 * @throws ServletException if a servlet-specific error occurs
56 * @throws IOException if an I/O error occurs
57 */
58 @Override
59 protected void doGet(HttpServletRequest request, HttpServletResponse
response)
60 throws ServletException, IOException {
61 // receive the request comming in, and do the response back to the browser
62 // Step 1: set the content type
63 response.setContentType("text/html");
64 // Step 2: get the printWriter
65 PrintWriter out = response.getWriter();
66 // Step 3: generate HTML content
67 out.println("<html><body>");
68 out.println("<h2>Hello World</h2>");
69 out.println("<hr>");
70 out.println("Time on the server is: "+ new java.util.Date());
71 out.println("</html></body>");
72 }
73
74 /**
75 * Handles the HTTP <code>POST</code> method.
76 *
77 * @param request servlet request
78 * @param response servlet response
79 * @throws ServletException if a servlet-specific error occurs
80 * @throws IOException if an I/O error occurs
81 */
82 @Override
83 protected void doPost(HttpServletRequest request, HttpServletResponse
response)
84 throws ServletException, IOException {
85 processRequest(request, response);
86 }
87
88 /**
89 * Returns a short description of the servlet.
90 *
91 * @return a String containing servlet description
92 */
93 @Override
94 public String getServletInfo() {
95 return "Short description";
96 }// </editor-fold>
97
98 }
99
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\servletdemo2021\web\WEB-INF\web.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
3 <servlet>
4 <servlet-name>HelloWorldServlet</servlet-name>
5 <servlet-class>servletdemo.HelloWorldServlet</servlet-class>
6 </servlet>
7 <servlet-mapping>
8 <servlet-name>HelloWorldServlet</servlet-name>
9 <url-pattern>/HelloWorldServlet</url-pattern>
10 </servlet-mapping>
11 <session-config>
12 <session-timeout>
13 30
14 </session-timeout>
15 </session-config>
16 </web-app>
17
❖ Question 01: Reading Form with Servlet – Servlet Demo 02
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\readingHTMLservlet2021\web\student-
form.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 <form action="StudentServlet" method="GET">
15 First name: <input type="text" name="firstName" />
16 <br/><br/>
17 Last name: <input type="text" name="lastName" />
18 <br/><br/>
19 <input type="submit" value="Submit" />
20 </form>
21 </body>
22 </html>
23
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\readingHTMLservlet2021\src\java\servletdem
o\StudentServlet.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 servletdemo;
7
8 import java.io.IOException;
9 import java.io.PrintWriter;
10 import javax.servlet.ServletException;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14
15 /**
16 *
17 * @author Ly Quynh Tran
18 */
19 public class StudentServlet extends HttpServlet {
20
21 /**
22 * Processes requests for both HTTP <code>GET</code> and
<code>POST</code>
23 * methods.
24 *
25 * @param request servlet request
26 * @param response servlet response
27 * @throws ServletException if a servlet-specific error occurs
28 * @throws IOException if an I/O error occurs
29 */
30 protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
31 throws ServletException, IOException {
32 response.setContentType("text/html;charset=UTF-8");
33 try (PrintWriter out = response.getWriter()) {
34 /* TODO output your page here. You may use following sample code.
*/
35 out.println("<!DOCTYPE html>");
36 out.println("<html>");
37 out.println("<head>");
38 out.println("<title>Servlet StudentServlet</title>");
39 out.println("</head>");
40 out.println("<body>");
41 out.println("<h1>Servlet StudentServlet at " + request.getContextPath()
+ "</h1>");
42 out.println("</body>");
43 out.println("</html>");
44 }
45 }
46
47 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on
the + sign on the left to edit the code.">
48 /**
49 * Handles the HTTP <code>GET</code> method.
50 *
51 * @param request servlet request
52 * @param response servlet response
53 * @throws ServletException if a servlet-specific error occurs
54 * @throws IOException if an I/O error occurs
55 */
56 @Override
57 protected void doGet(HttpServletRequest request, HttpServletResponse
response)
58 throws ServletException, IOException {
59 // Step 1: set content type
60 response.setContentType("text/html");
61 // Step 2: get the printwriter
62 PrintWriter out = response.getWriter();
63 // Step 3: generate the HTML content
64 out.println("<html><body>");
65 out.println("The student is confirmed: "
66 + request.getParameter("firstName") + " "
67 + request.getParameter("lastName"));
68 out.println("</body></html>");
69 }
70
71 /**
72 * Handles the HTTP <code>POST</code> method.
73 *
74 * @param request servlet request
75 * @param response servlet response
76 * @throws ServletException if a servlet-specific error occurs
77 * @throws IOException if an I/O error occurs
78 */
79 @Override
80 protected void doPost(HttpServletRequest request, HttpServletResponse
response)
81 throws ServletException, IOException {
82 processRequest(request, response);
83 }
84
85 /**
86 * Returns a short description of the servlet.
87 *
88 * @return a String containing servlet description
89 */
90 @Override
91 public String getServletInfo() {
92 return "Short description";
93 }// </editor-fold>
94
95 }
96
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\readingHTMLservlet2021\src\java\servletdem
o\TestParamServlet.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 servletdemo;
7
8 import java.io.IOException;
9 import java.io.PrintWriter;
10 import javax.servlet.ServletContext;
11 import javax.servlet.ServletException;
12 import javax.servlet.http.HttpServlet;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15
16 /**
17 *
18 * @author Ly Quynh Tran
19 */
20 public class TestParamServlet extends HttpServlet {
21
22 /**
23 * Processes requests for both HTTP <code>GET</code> and
<code>POST</code>
24 * methods.
25 *
26 * @param request servlet request
27 * @param response servlet response
28 * @throws ServletException if a servlet-specific error occurs
29 * @throws IOException if an I/O error occurs
30 */
31 protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
32 throws ServletException, IOException {
33 response.setContentType("text/html;charset=UTF-8");
34 try (PrintWriter out = response.getWriter()) {
35 /* TODO output your page here. You may use following sample code.
*/
36 out.println("<!DOCTYPE html>");
37 out.println("<html>");
38 out.println("<head>");
39 out.println("<title>Servlet TestParamServlet</title>");
40 out.println("</head>");
41 out.println("<body>");
42 out.println("<h1>Servlet TestParamServlet at " +
request.getContextPath() + "</h1>");
43 out.println("</body>");
44 out.println("</html>");
45 }
46 }
47
48 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on
the + sign on the left to edit the code.">
49 /**
50 * Handles the HTTP <code>GET</code> method.
51 *
52 * @param request servlet request
53 * @param response servlet response
54 * @throws ServletException if a servlet-specific error occurs
55 * @throws IOException if an I/O error occurs
56 */
57 @Override
58 protected void doGet(HttpServletRequest request, HttpServletResponse
response)
59 throws ServletException, IOException {
60 // Step 1: set content type
61 response.setContentType("text/html");
62
63 // Step 2: get printwriter
64 PrintWriter out = response.getWriter();
65
66 // Step 3: read configuration params
67 ServletContext context = getServletContext(); // inherit from
HttpServlet
68
69 String maxCartSize = context.getInitParameter("max-shopping-
cart");
70 String teamName = context.getInitParameter("project-team-name");
71
72 // Step 4: generate HTML content
73 out.println("<html><body>");
74
75 out.println("Max cart: " + maxCartSize);
76 out.println("<br/><br/>");
77 out.println("Team name: " + teamName);
78
79 out.println("</body></html>");
80 }
81
82 /**
83 * Handles the HTTP <code>POST</code> method.
84 *
85 * @param request servlet request
86 * @param response servlet response
87 * @throws ServletException if a servlet-specific error occurs
88 * @throws IOException if an I/O error occurs
89 */
90 @Override
91 protected void doPost(HttpServletRequest request, HttpServletResponse
response)
92 throws ServletException, IOException {
93 processRequest(request, response);
94 }
95
96 /**
97 * Returns a short description of the servlet.
98 *
99 * @return a String containing servlet description
100 */
101 @Override
102 public String getServletInfo() {
103 return "Short description";
104 }// </editor-fold>
105
106 }
107
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\readingHTMLservlet2021\web\WEB-
INF\web.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
3 <context-param>
4 <param-name>max-shopping-cart</param-name>
5 <param-value>99</param-value>
6 </context-param>
7 <context-param>
8 <param-name>project-team-name</param-name>
9 <param-value>The Coding</param-value>
10 </context-param>
11 <welcome-file-list>
12 <welcome-file>student-form.html</welcome-file>
13 </welcome-file-list>
14 <servlet>
15 <servlet-name>StudentServlet</servlet-name>
16 <servlet-class>servletdemo.StudentServlet</servlet-class>
17 </servlet>
18 <servlet>
19 <servlet-name>TestParamServlet</servlet-name>
20 <servlet-class>servletdemo.TestParamServlet</servlet-class>
21 </servlet>
22 <servlet-mapping>
23 <servlet-name>StudentServlet</servlet-name>
24 <url-pattern>/StudentServlet</url-pattern>
25 </servlet-mapping>
26 <servlet-mapping>
27 <servlet-name>TestParamServlet</servlet-name>
28 <url-pattern>/TestParamServlet</url-pattern>
29 </servlet-mapping>
30 <session-config>
31 <session-timeout>
32 30
33 </session-timeout>
34 </session-config>
35 </web-app>
36
❖ Question 01: Write Web Application with Servlet
1. Create class Student with corresponding attributes:
• Id int
• Name String
• Gender Boolean
• DOB Date(util)
2. Create student.jsp an entry form for adding new student to the list:
• If user clicks ‘Add’ button, data will submitted to StudentServlet. The Servlet will
add new student to the list then display the updated list
❖ Code
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebMCVServletStudentDemo\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.io.Serializable;
9 import java.text.ParseException;
10 import java.text.SimpleDateFormat;
11 import java.util.Date;
12 import java.util.logging.Level;
13 import java.util.logging.Logger;
14
15 /**
16 *
17 * @author Ly Quynh Tran
18 */
19 public class Student implements Serializable {
20
21 private int id;
22 private String name;
23 private boolean gender;
24 private Date dob;
25
26 public Student() {
27 }
28
29 public Student(Student s) {
30 this(s.id, s.name, s.gender, s.dob);
31 }
32
33 public Student(int id, String name, boolean gender, Date dob) {
34 this.id = id;
35 this.name = name;
36 this.gender = gender;
37 this.dob = dob;
38 }
39
40 public Student(int id, String name, String gender, String dob) {
41 this.id = id;
42 this.name = name;
43 this.gender = gender.equals("M");
44 setDob(dob);
45 }
46
47 public int getId() {
48 return id;
49 }
50
51 public void setId(int id) {
52 this.id = id;
53 }
54
55 public String getName() {
56 return name;
57 }
58
59 public void setName(String name) {
60 this.name = name;
61 }
62
63 public String getGender() {
64 return gender ? "Male" : "Female";
65 }
66
67 public void setGender(String gender) {
68 this.gender = gender.equals("M");
69 }
70
71 public String getDob() {
72 SimpleDateFormat sd = new SimpleDateFormat("dd/MM/yyyy");
73 return sd.format(dob);
74 }
75
76 public void setDob(String dob) {
77 SimpleDateFormat sd = new SimpleDateFormat("dd/MM/yyy");
78
79 try {
80 this.dob = new Date(sd.parse(dob).getTime());
81 } catch (ParseException ex) {
82 Logger.getLogger(Student.class.getName()).log(Level.SEVERE, null,
ex);
83 }
84 }
85
86 @Override
87 public String toString() {
88 return "Student{" + "id=" + id + ", name=" + name + ", gender=" + gender
+ ", dob=" + dob + '}';
89 }
90
91 }
92
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebMCVServletStudentDemo\src\java\model
\StudentList.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.io.Serializable;
9 import java.util.ArrayList;
10 import java.util.function.Predicate;
11
12 /**
13 *
14 * @author Ly Quynh Tran
15 */
16 public class StudentList implements Serializable {
17
18 private ArrayList<Student> list = new ArrayList<>();
19
20 public StudentList() {
21 list.add(new Student(1001, "Nguyễn Trường Xuân", "M", "20/05/1999"));
22 list.add(new Student(1002, "Nguyễn Thị Thanh Xuân", "F",
"20/05/2000"));
23 list.add(new Student(1003, "Nguyễn Minh Xuân", "M", "20/05/2001"));
24 list.add(new Student(1004, "Nguyễn Thanh Xuân", "M", "20/05/2002"));
25 list.add(new Student(1005, "Nguyễn Trường Sinh", "F", "20/05/1998"));
26 }
27
28 public ArrayList<Student> add(Student s) {
29 list.add(s);
30 return list;
31 }
32
33 public ArrayList<Student> delete(int id) {
34 for (int i = 0; i < list.size(); i++) {
35 if (list.get(i).getId() == id) {
36 list.remove(i);
37 }
38
39 }
40 return list;
41 }
42
43 public ArrayList<Student> update(Student student) {
44 for (int i = 0; i < list.size(); i++) {
45 if (list.get(i).getId() == student.getId()) {
46 list.set(i, student);
47 }
48
49 }
50 return list;
51 }
52
53 public Student getStudent(int id) {
54 for (Student student : list) {
55 if (student.getId() == id) {
56 return student;
57 }
58 }
59 return null;
60 }
61
62 public ArrayList<Student> search(Predicate p) {
63 ArrayList<Student> rs = new ArrayList<>();
64 for (Student s : list) {
65 if (p.test(s)) {
66 rs.add(s);
67 }
68 }
69 return rs;
70 }
71
72 public ArrayList<Student> searchByID(int id) {
73 String idd = String.valueOf(id);
74 ArrayList<Student> l = new ArrayList<>();
75 for (int i = 0; i < list.size(); i++) {
76 if (String.valueOf(list.get(i).getId()).contains(idd)) {
77 l.add(list.get(i));
78 }
79 }
80 return l;
81 }
82
83 public ArrayList<Student> searchByName(String name) {
84
85 ArrayList<Student> l = new ArrayList<>();
86 for (int i = 0; i < list.size(); i++) {
87 if (list.get(i).getName().contains(name)) {
88 l.add(list.get(i));
89 }
90 }
91 return l;
92 }
93
94 public ArrayList<Student> getList() {
95 return list;
96 }
97
98 public void setList(ArrayList<Student> list) {
99 this.list = list;
100 }
101
102 }
103
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebMCVServletStudentDemo\src\java\model
\NewMain.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 NewMain {
13
14 /**
15 * @param args the command line arguments
16 */
17 public static void main(String[] args) {
18 // TODO code application logic here
19
20 StudentList studentList = new StudentList();
21 //studentList.delete(1002);
22 Student s = new Student(1002, "Mr B", "F", "20/05/2000");
23 studentList.update(s);
24 System.out.println("" + studentList.getList());
25 }
26
27 }
28
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebMCVServletStudentDemo\web\addStude
nt.jsp
1 <%@page contentType="text/html" pageEncoding="utf-8"%>
2 <%@ include file="/includes/header.jsp" %>
3 <%@ include file="/includes/column_left_home.jsp" %>
4 <jsp:useBean id="slist" class="model.StudentList"
scope="application"></jsp:useBean>
5 <!-- start the middle column -->
6
7 <section>
8 <h1>Add New Student to System</h1>
9 <br><br>
10 <form action="StudentServlet" method="POST">
11 <label>Student ID</label><input type="text" name="id"/><br>
12 <label>Student Name</label><input type="text" name="name"/><br>
13 <label>Student gender</label><input type="radio" name="gender"
value="M"/>Male<input type="radio" name="gender" value="F"/>Female<br>
14 <label>Student DOB</label><input type="text" name="dob"/><br>
15 <input type="submit" value="Add" />
16
17 </form>
18
19
20
21 </section>
22
23 <!-- end the middle column -->
24
25 <%@ include file="/includes/column_right_news.jsp" %>
26 <%@ include file="/includes/footer.jsp" %>
27
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebMCVServletStudentDemo\src\java\contr
oller\StudentServlet.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 java.io.IOException;
9 import java.io.PrintWriter;
10 import java.util.ArrayList;
11 import javax.servlet.RequestDispatcher;
12 import javax.servlet.ServletException;
13 import javax.servlet.http.HttpServlet;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16 import javax.servlet.http.HttpSession;
17 import model.Student;
18 import model.StudentList;
19
20 /**
21 *
22 * @author Ly Quynh Tran
23 */
24 public class StudentServlet extends HttpServlet {
25
26 /**
27 * Processes requests for both HTTP <code>GET</code> and
<code>POST</code>
28 * methods.
29 *
30 * @param request servlet request
31 * @param response servlet response
32 * @throws ServletException if a servlet-specific error occurs
33 * @throws IOException if an I/O error occurs
34 */
35 protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
36 throws ServletException, IOException {
37 response.setContentType("text/html;charset=UTF-8");
38 try (PrintWriter out = response.getWriter()) {
39 /* TODO output your page here. You may use following sample code.
*/
40 out.println("<!DOCTYPE html>");
41 out.println("<html>");
42 out.println("<head>");
43 out.println("<title>Servlet StudentServlet</title>");
44 out.println("</head>");
45 out.println("<body>");
46 out.println("<h1>Servlet StudentServlet at " + request.getContextPath()
+ "</h1>");
47 out.println("</body>");
48 out.println("</html>");
49 }
50 }
51
52 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on
the + sign on the left to edit the code.">
53 /**
54 * Handles the HTTP <code>GET</code> method.
55 *
56 * @param request servlet request
57 * @param response servlet response
58 * @throws ServletException if a servlet-specific error occurs
59 * @throws IOException if an I/O error occurs
60 */
61 @Override
62 protected void doGet(HttpServletRequest request, HttpServletResponse
response)
63 throws ServletException, IOException {
64 response.setContentType("text/html;charset=UTF-8");
65 RequestDispatcher dispatcher =
request.getRequestDispatcher("addStudent.jsp");
66 dispatcher.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 response.setContentType("text/html;charset=UTF-8");
81 HttpSession session = request.getSession();
82 int id = Integer.parseInt(request.getParameter("id"));
83 String name = request.getParameter("name");
84 String gender = request.getParameter("gender");
85 String dob = request.getParameter("dob");
86 Student stu1 = new Student(id, name, gender, dob);
87 StudentList sList = (StudentList)
(request.getSession().getAttribute("sList"));
88 // ArrayList<Student>
sList=(ArrayList<Student>)(request.getSession().getAttribute("sList"));
89 ArrayList<Student> list = new ArrayList<>();
90 list = sList.add(stu1);
91 sList.setList(list);
92 request.getSession().setAttribute("sList", sList);
93 request.getRequestDispatcher("list.jsp").include(request, response);
94
95 }
96
97 /**
98 * Returns a short description of the servlet.
99 *
100 * @return a String containing servlet description
101 */
102 @Override
103 public String getServletInfo() {
104 return "Short description";
105 }// </editor-fold>
106
107 }
108
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebMCVServletStudentDemo\web\list.jsp
1 <%@page contentType="text/html" pageEncoding="utf-8"%>
2 <%@ include file="/includes/header.jsp" %>
3 <%@ include file="/includes/column_left_home.jsp" %>
4 <jsp:useBean id="sList" class="model.StudentList"
scope="session"></jsp:useBean>
5 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
6 <!-- start the middle column -->
7
8 <section id="main-contain"class="column">
9 <h1>Student List</h1>
10 <table boder="1">
11 <thead>
12 <tr>
13 <th>ID</th>
14 <th>Name</th>
15 <th>Gender</th>
16 <th>DOB</th>
17 </tr>
18 <tbody>
19
20 <c:forEach var="st" items = "${sList.getList()}" >
21 <tr>
22 <td>${st.getId()}</td>
23 <td>${st.getName()}</td>
24 <td>
25 <c:if test = "${st.getGender()=='Male'}">
26 <input type="checkbox" checked>
27 </c:if>
28 <c:if test = "${st.getGender()=='Female'}">
29 <input type="checkbox" unchecked>
30 </c:if>
31 </td>
32 <td>${st.getDob()}</td>
33 </tr>
34 </c:forEach>
35 </tbody>
36 </thead>
37 </table>
38
39 </section>
40
41 <!-- end the middle column -->
42
43 <%@ include file="/includes/column_right_news.jsp" %>
44 <%@ include file="/includes/footer.jsp" %>
45