By GokiSoft.com|
21:12 14/08/2020|
JSP Servlet
[Shade Code] Tìm hiểu EL trong JSP & jstf trong JSP - Lập trình JSP Servlet
I. Lý thuyết
- Biểu thức trong JSP
- JSTL
- Một số lệnh trong JSTL
- Tìm hiểu thông qua trang jsp test
II. Thực hành
- Overview
- Javabean
- Biểu thức trong JSP
- Áp dụng JSTL trong project
- Tìm hiểu thông qua Mini Project
=======================================================
Mini Project
=======================================================
- Tạo 1 trang quản lý danh sách sinh viên
Thuộc tinh sinh viên cần quản lý : rollno, fullname, birthday, address, email
Yêu cầu:
- Route: /quan-ly-sinh-vien
- Servlet: StudentServlet => Logic
- studentList.jsp => hiển thị danh sách sinh viên => 100% jstl để hiển thị thông tin => không được sử dụng lẫn code java
I. Mục 1 => Lý Thuyết
<%--
Document : test
Created on : Aug 14, 2020, 7:22:52 PM
Author : Diep
--%>
<%@page import="modals.User"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>EL & JSTL JSP</title>
</head>
<body>
<h1>Hello World!</h1>
<%
int y = 5;
int z = 7;
int t = y + z;
out.print(t);
request.setAttribute("newT", t);
//request, response, session, out, application
//Su dung bieu thuc => requestScope, responseScope, sessionScope, ...
%>
<h2>Cach 2: <%=t%></h2>
<h2>Cach 3 : ${2+3}</h2>
<h2>Cach 2 New : ${requestScope.newT}</h2>
<c:set var="t" value="<%=t%>" scope="request"></c:set>
<h2>Su dung bien T : ${t}</h2>
<h2>Test T : ${requestScope.t}</h2>
<h2>Test TT <c:out value="${t}"/></h2>
<%
List<User> list = new ArrayList<User>();
list.add(new User("A", "a@gmail.com", "1", "1", "1", "Ha Noi"));
list.add(new User("B", "b@gmail.com", "1", "1", "1", "Ha Noi"));
list.add(new User("C", "c@gmail.com", "1", "1", "1", "Ha Noi"));
%>
<c:set var="list2" value="<%=list%>" scope="application"/>
<table border="1">
<thead>
<tr>
<th>User Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<c:forEach var="item" items="${list2}">
<tr>
<td>${item.name}</td>
<td>${item.email}</td>
<td>${item.address}</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
II. Mục 2 => Thực Hành
/*
* 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 lession5.modals;
import java.io.Serializable;
/**
*
* @author Diep
*/
public class Student implements Serializable{
String rollno, fullname, email, birthday, address;
public Student() {
}
public Student(String rollno, String fullname, String email, String birthday, String address) {
this.rollno = rollno;
this.fullname = fullname;
this.email = email;
this.birthday = birthday;
this.address = address;
}
public String getRollno() {
return rollno;
}
public void setRollno(String rollno) {
this.rollno = rollno;
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
/*
* 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 lession5.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lession5.modals.Student;
/**
*
* @author Diep
*/
public class StudentServlet extends HttpServlet {
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<Student> studentList = new ArrayList<>();
studentList.add(new Student("R001", "A", "a@gmail.com", "1999-01-01", "Ha Noi"));
studentList.add(new Student("R002", "A", "a@gmail.com", "1999-01-01", "Ha Noi"));
studentList.add(new Student("R003", "A", "a@gmail.com", "1999-01-01", "Ha Noi"));
studentList.add(new Student("R004", "A", "a@gmail.com", "1999-01-01", "Ha Noi"));
studentList.add(new Student("R005", "A", "a@gmail.com", "1999-01-01", "Ha Noi"));
request.setAttribute("studentList", studentList);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/lession5/studentList.jsp");
dispatcher.forward(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
<%--
Document : studentList
Created on : Aug 14, 2020, 8:52:51 PM
Author : Diep
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Student Management!</h1>
<table border = "1">
<thead>
<tr>
<th>No</th>
<th>RollNo</th>
<th>Fullname</th>
<th>Email</th>
<th>Birthday</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<c:forEach var="item" items="${studentList}" varStatus="loop">
<tr>
<td>${loop.index + 1}</td>
<td>${item.rollno}</td>
<td>${item.fullname}</td>
<td>${item.email}</td>
<td>${item.birthday}</td>
<td>${item.address}</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)