Giáo trình Nhập môn Công nghệ phần mềm - Bài 1: Introduction to Java Servlet và JSP
Introduction to Software Engineering - Nhập môn Công nghệ phần mềm
Lab 01
Introduction to Java Servlet và JSP
Mục tiêu
- Cài đặt Netbeans tích hợp sẵn Java EE
- Cài đặt các web server và biết cách start, stop, deploy application trên web
server
- Tạo ứng dụng đầu tiên HelloWorld, dùng Servlet và deploy lên Server thực hành
với request và response.
Phần I Bài tập step by step
Bài 1.1
- Chọn Netbeans tích hợp Java EE -> Download
- Nếu cài đặt sẵn trong máy là Netbeans SDK (chưa có Java EE): Chọn Tools-
>Plugins->Available Plugins-> Tìm và chọn Java WEB and EE -> Install
Software Engineering Department - SoICT/HUST
Trang 1 / 19
Introduction to Software Engineering - Nhập môn Công nghệ phần mềm
- Trong quá trình cài đặt cần cài thêm các web servers tích hợp với Netbeans.
Netbeans Java EE hỗ trợ tích hợp sẵn GlashFish và Apache Tomcat.
Software Engineering Department - SoICT/HUST
Trang 2 / 19
Introduction to Software Engineering - Nhập môn Công nghệ phần mềm
- Chọn Customize -> Chọn GlashFish và Apache Tomcat (hoặc 1 trong 2) -> Install
Bước 2: Nếu không có sẵn GlashFish/Tomcat trong Netbeans, cài đặt GlashFish hoặc/và
Apache Tomcat như sau:
2.1. Cài đặt GlashFish 4.x và cấu hình trong Netbeans
trường hợp chưa có sẵn Java EE 7 thì download glashfish-4.1.zip
- Unzip file, open Terminal (Command-Line in Windows), di chuyển tới thư mục đã
unzip, ví dụ, unzip vào C:/glashFish4 → cd C:/glashFish4/bin
- Sau đó gõ tiếp lệnh: asadmin start-domain để start server
GlashFish)
Software Engineering Department - SoICT/HUST
Trang 3 / 19
Introduction to Software Engineering - Nhập môn Công nghệ phần mềm
- Cấu hình GlashFish trong Netbeans:
o Mở Netbeans
o Chọn Services Tab -> Chuột phải Servers -> Add Server
Software Engineering Department - SoICT/HUST
Trang 4 / 19
Introduction to Software Engineering - Nhập môn Công nghệ phần mềm
Software Engineering Department - SoICT/HUST
Trang 5 / 19
Introduction to Software Engineering - Nhập môn Công nghệ phần mềm
- Start GlashFish trên Netbeans:
Services Tab -> Servers -> Expand Servers -> Chọn GlashFish Server -> Chuột phải
chọn Star Server
Software Engineering Department - SoICT/HUST
Trang 6 / 19
Introduction to Software Engineering - Nhập môn Công nghệ phần mềm
- Chuột phải GlashFish server trong Services tab -> View Domain Admin Console
Bây giờ hãy tạo một ứng dụng web và chọn Server là GlassFish.
STEP 2.1.1: Tạo ứng dụng web trên Netbeans
File -> New Project -> Java Web - > Web Application
Software Engineering Department - SoICT/HUST
Trang 7 / 19
Introduction to Software Engineering - Nhập môn Công nghệ phần mềm
Đặt tên project là: HelloWorld.
Chọn server GlassFish và Java EE 7
Finish
STEP 2.1.2: Tạo servlet và deploy lên server
Tạo package servlet cho project.
Software Engineering Department - SoICT/HUST
Trang 8 / 19
Introduction to Software Engineering - Nhập môn Công nghệ phần mềm
New Servlet -> HelloWorld.
Chọn Add information to deployment descriptor.
Software Engineering Department - SoICT/HUST
Trang 9 / 19
Introduction to Software Engineering - Nhập môn Công nghệ phần mềm
Mở file web.xml trong thư mục Configuration Files của project, thông tin cấu hình của
Servlet đã được thêm vào web.xml
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>servlet.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
Trong config này chúng ta thấy tên của Servlet và java class tương ứng. Sau đó là url-
pattern tương ứng với tên của servlet.
Trong file HelloWorld.java chúng ta sẽ thấy 1 số method được sinh tự động bởi Netbeans.
Build and run project, ứng dụng sẽ được deploy lên GlassFish server và chúng ta sẽ thấy
nó trong thư mục Applications của server:
Software Engineering Department - SoICT/HUST
Trang 10 / 19
Introduction to Software Engineering - Nhập môn Công nghệ phần mềm
Muốn undeploy ứng dụng trên GlassFish, chúng ta vào Services tab, mở GlassFish
server, chọn tên ứng dụng và undeploy.
2.2. Cài đặt Server Apache Tomcat
Deployer version.
- Un zip file ra thư mục C:/tomcat8
Software Engineering Department - SoICT/HUST
Trang 11 / 19
Introduction to Software Engineering - Nhập môn Công nghệ phần mềm
- Start/Stop Tomcat bằng dòng lệnh: Mở Command Line -> cd C:/tomcat8 [Trên
Unix/Linux/Mac, cd thư_mục_chứa_Tomcat] -> cd bin
- Trên windows:
- Start Tomcat: gõ startup.bat
- Stop Tomcat: gõ shutdown.bat
- Trên Unix/Linux/Mac:
- Start Tomcat: gõ ./startup.sh
- Stop Tomcat: gõ ./shutdown.sh
- Cấu hình Tomcat và start, stop từ Netbeans tương tự như cấu hình GlashFish.
Vì GlashFish và Tomcat mặc định cổng HTTP port là 8080. Do đó khi một ứng dụng đang
chạy trên GlassFish thì run/start Tomcat sẽ bị xung đột.
Config lại cổng của Tomcat như sau: Services tab -> Tomcat Server -> Properties
Software Engineering Department - SoICT/HUST
Trang 12 / 19
Introduction to Software Engineering - Nhập môn Công nghệ phần mềm
Ví dụ thay Server Port: 8085.
Một ứng dụng web có thể thiết lập lại Server bằng cách chọn project -> Properties ->
Run -> Chúng ta đặt lại server cho ứng dụng trong list các server.
Chuột phải project HelloWorld -> Properties -> Run -> Chọn Tomcat Server thay cho
GlassFish
Software Engineering Department - SoICT/HUST
Trang 13 / 19
Introduction to Software Engineering - Nhập môn Công nghệ phần mềm
Clean and Build -> Deploy
Mở Services tab -> Servers -> Tomcat -> Mở thư mục Web Applications, ứng dụng
HelloWorld đã được deploy lên Tomcat Server.
Servlet HelloWorld sẽ được gọi và trả về kết quả như phần trước.
Để undeploy ứng dụng, chọn Services tab -> Servers -> Tomcat Server -> Web
Applications -> Chọn ứng dụng muốn undeploy -> Undeploy.
Software Engineering Department - SoICT/HUST
Trang 14 / 19
Introduction to Software Engineering - Nhập môn Công nghệ phần mềm
Ngoài ra, chúng ta cũng có thể undeploy hoặc deploy ứng dụng từ Web Console của
Tomcat.
Chọn Manage App
Để deploy lại ứng dụng, ở Deploy tab, chúng ta có thể chọn đường dẫn tới file .war của
ứng dụng. File .war của ứng dụng nằm trong thư mục build/dist của project.
Bài 1.2: Tạo form nhập và hiển thị Hello + name
Quay trở lại với HelloWorld project đã tạo ra ở bài 1.
Mục tiêu:
- Biết cách thực hành với form trên jsp.
- Biết lấy về tham số từ form và hiển thị lại trên response.
Bài này chúng ta thực hành với GlassFish server. Chọn lại server của ứng dụng
HelloWorld là GlassFish.
STEP 1: Sửa lại file index.html để tạo form nhập liệu
<html>
<head>
<title>TODO supply a title</title>
Software Engineering Department - SoICT/HUST
Trang 15 / 19
Introduction to Software Engineering - Nhập môn Công nghệ phần mềm
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
</head>
<body>
<form method="get" action="hello.jsp">
<input type="text" id="username" name="username" />
<input type="submit" value="Go" />
</form>
</body>
</html>
Chúng ta set action của form là một trang jsp khác có tên là hello.jsp.
STEP 2: Tạo trang hello.jsp xử lý dữ liệu nhập được từ form
File -> New JSP -> hello.jsp
<%@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>Hello <%= request.getParameter("username") %></h1>
</body>
</html>
Chúng ta dùng: request.getParameter(“username”) để lấy về thông tin từ input text
của form. Để hiển thị, sử dụng jsp scriptlet <%= %> để lấy value từ dòng code java
trên trang jsp.
Thay vì khai báo action=”hello.jsp” chúng ta có thể khai báo action của form là một
servlet.
Sửa lại index.html -> action="HelloWorld"
Software Engineering Department - SoICT/HUST
Trang 16 / 19
Introduction to Software Engineering - Nhập môn Công nghệ phần mềm
Chú ý: HelloWorld là name của servlet (trong web.xml) đã config ở trên.
Mở HelloWorld.java -> Xoá toàn bộ code tự sinh của Netbeans.
Override method doGet của HttpServlet.
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorld extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code.
*/
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet HelloServlet</title>");
out.println("</head>");
out.println("<body>");
String username = (String) request.getParameter("username");
out.println("<h1>Hello " + username + "!</h1>");
out.println("</body>");
out.println("</html>");
}
}
}
Tương tự, chúng ta cũng lấy giá trị nhập liệu từ form bằng:
request.getParameter(“username”).
Để ghi lên response và trả về response, chúng ta dùng request.getWriter() trả về object
của class PrintWriter.
Software Engineering Department - SoICT/HUST
Trang 17 / 19
Introduction to Software Engineering - Nhập môn Công nghệ phần mềm
Clean and Build ứng dụng web với hai trường hợp, submit form lên trang hello.jsp và
submit form lên servlet. Chúng ta đều có kết quả như sau:
Software Engineering Department - SoICT/HUST
Trang 18 / 19
Introduction to Software Engineering - Nhập môn Công nghệ phần mềm
Phần II: Bài tập tự làm
Bài 1.3
Tiếp tục bài trước, hãy tạo 1 form chứa Username, User-email. Sau khi người dù ng
nhập và ấn OK, hãy hiển thị tên username và tên user-email.
Em hãy giải thích cơ chế hoạt động từ khi người dùng điền thông tin vào form và submit
form đến khi trả về kết quả.
HẾT
Software Engineering Department - SoICT/HUST
Trang 19 / 19
Bạn đang xem tài liệu "Giáo trình Nhập môn Công nghệ phần mềm - Bài 1: Introduction to Java Servlet và JSP", để tải tài liệu gốc về máy hãy click vào nút Download ở trên
File đính kèm:
giao_trinh_nhap_mon_cong_nghe_phan_mem_bai_1_introduction_to.pdf

