1 / 36

JSP PRGRAMMING (JAVA SERVER PAGE) @Huỳnh Ngọc Tín - 09/2007

JSP PRGRAMMING (JAVA SERVER PAGE) @Huỳnh Ngọc Tín - 09/2007. References. Marty Hall. Core Servlet and Java Server Page. Sun Micro System. Prentice Hall PTR; 1 edition 2000. JavaServerPages Specification Version 2.0. (java.sun.com). Contents. Khái niệm JSP (Java Server Page) Cú pháp JSP

steffi
Download Presentation

JSP PRGRAMMING (JAVA SERVER PAGE) @Huỳnh Ngọc Tín - 09/2007

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JSP PRGRAMMING (JAVA SERVER PAGE) @Huỳnh Ngọc Tín - 09/2007

  2. References • Marty Hall. Core Servlet and Java Server Page. Sun Micro System. Prentice Hall PTR; 1 edition 2000. • JavaServerPages Specification Version 2.0. (java.sun.com)

  3. Contents • Khái niệm JSP (Java Server Page) • Cú pháp JSP • Nhúng file và chuyển hướng giữa các trang jsp • Các đối tượng mặc định • HTML form, xử lý Form • So sánh JSP & Servlet • Đối tượng Session • Ví dụ minh họa

  4. JAVA SERVER PAGES ? • JavaServer Pages (JSP) is the Java 2 Platform, Enterprise Edition (J2EE) technology for building applications for generating dynamic web content, such as HTML, DHTML, XHTML, and XML. JSP technology enables the easy authoring of web pages that create dynamic content with maximum power and flexibility.

  5. JAVA SERVER PAGES? • Thuận lợi khi dùng JSP: • Tạo trang web động • Những trang HTML dễ viết và bảo trì dùng các công cụ thiết Web. • Công việc được tách biệt và phân công rõ ràng trong nhóm phát triển. JSP = HTML + Java code

  6. JSP Compiler Request CSDL Web Browser Response Servlet tạm Trang JSP HTTPService How to run JSP file

  7. JSP Syntax • Thẻ bọc mã lệnh java:<% %> <html> <b> <%out.println(“Last update: “);%> </b> <br> <%java.util.Date date = new java.util.Date(); out.println(date); %> </html>

  8. JSP Syntax <%out.println("<html>"); out.println("<b>"); out.println("Last update:"); out.println("</b>"); out.println("<br>"); java.util.Date date = new java.util.Date(); out.println(date); out.println("</html>"); %>

  9. JSP Syntax • Chú thích trong jsp Dùng kiểu chú thích // và /* …*/ Dùng <%-- --%>: bỏ qua tất cả code java và html bên trong <!-- HTML Comment --> <html> <%-- <b> <% out.println(“Last update: “); %></b> <br> <% java.util.Date date = new java.util.Date(); out.println(date); %> --%> </html>

  10. JSP Syntax • Kết xuất giá trị biến hay hàm: <%= %> • Khai báo biến & phương thức: <%! %> • Ví dụ 1: <%! int count = 0; %> <%! String des = “Increase count var” ; %> <%= des %> <%= ++count %> Lưu ý: biểu thức nằm trong thẻ <%= không được tận cùng bằng dấu chấm phẩy (;) như lệnh trong java

  11. JSP Syntax • Ví dụ 2: … <%! public int maxNumber (int x, int y) { if (x>y) return x; else return y; } %> …

  12. JSP Syntax • Khai báo thư viện: <%@ page %> Ví dụ: <%@ page import = “java.sql.*, java.util.*” %> • Nhúng file: <%@ include file=“filename” %> Dùng để đem nội dung file “html” hay “jsp” khác bên ngoài vào trang hiện hành. • Chuyển hướng các giữa các trang jsp <jsp:forward page="./loginOK.jsp"></jsp:forward>

  13. JSP Syntax – Include file • Ví dụ: (nhúng trang jsp, html) <HTML> <H1> Include Page </H1> <br> <% out.println(“Today is “); %> <%@include file=“currentDate.jsp” %> </HTML>

  14. JSP Syntax - sendRedirect • Ví dụ: chuyển hướng đến trang thể hiện kết quả login • … • if (loginResult == true) • response.sendRedirect("./loginOK.jsp"); • else • response.sendRedirect("./loginFail.jsp"); • … • Lưu ý: có thể chuyển hướng từ một trang jsp đến 1 trang html, jsp hay một servlet khác.

  15. JSP Syntax – <jsp:forward/> Hoặc <% … if (loginResult == true) { %> <jsp:forward page="./loginOK.jsp"></jsp:forward> <% } else { %> <jsp:forward page="./loginFail.jsp"></jsp:forward> <% } … %>

  16. JSP Syntax - <jsp:useBean> • Chúng ta có thể tạo các lớp đối tượng java và gọi chúng từ trang jsp. • <jsp:useBean> • Ví dụ: <jsp:useBean id = “test” class=“SimpleBean”> </jsp:useBean> Hoặc <jsp:useBean id = “test” class=“SimpleBean” /> … <% SimpleBean test = new SimpleBean(); %>

  17. JSP Syntax - <jsp:useBean> public class SimpleBean { private String msg = “No Message”; public String getMessage(String msg) { return (msg); } public void setMessage(String msg) { this.msg = msg; } }

  18. JSP Syntax - <jsp:useBean> <jsp:useBean id="test" class="MyBeans.SimpleBean"/> <% MyBeans.SimpleBean temp = new MyBeans.SimpleBean(); String sT = temp.getMessage(); out.println(sT); temp.setMessage("Hello Class"); out.println(temp.getMessage()); %>

  19. JSP Syntax - <jsp:setProperty> • <jsp:setProperty> • Dùng để gọi một phương thức nào đó của Bean. • Ví dụ: để gọi phương thức setMessage của SimpleBean <jsp:setProperty name = “test” property = “msg” value = “hello class” />

  20. Default Objects • Đối tượng out: xuất phát từ lớp PrintWriter • Đối tượng request: xuất phát từ lớp HttpServletRequest • Đối tượng response: tương tự đối tượng out, nhưng xuất phát từ lớp HttpServletResponse dùng để đưa kết xuất về client. • Đối tượng session: HttpSession, dùng để theo dõi kết nối và lưu vết phiên làm việc giữa client & server.

  21. HttpSession Object • javax.servlet.http Interface HttpSession • Do server sinh ra • The servlet container dùng interface này để tạo ra một phiên làm việc (session) giữa HTTP Client & HTTP server. • Các biến trong session lưu thông tin về người dùng và dùng chung cho tất cả các trang của ứng dụng • Session sẽ hủy khi người dùng hủy kết nối hoặc session hết hạn

  22. HttpSession Object • Có thể định thời gian • Các Methods cơ bản void setAttribute(String name, Object value) Binds an object to this session, using the name specified ObjectgetAttribute(String name) Returns the object with the specified name ObjectgetValue(String name) Tương tự getAttribute void putValue(String name, Object value) Tương tự setAttribute

  23. HTML Form - Submit • <html> • <body> • <h1>Login</h1> • <Form action="formHandler.jsp" method="get"> • Username: <Input type="Text" name="Username"> <br> Password: <Input type=“Password" name="Password"> <br> <Input type="Submit" value="Submit"> • </Form> • </body> • </html>

  24. HTML Form - Submit • Truyền dữ liệu bằng phương thức GET • Dữ liệu sau đường dẫn URL là dấu chấm hỏi “?” cùng với những cặp giá trị name=value. • Ví dụ: http://localhost:8080/formHandler.jsp?Username=admin&Password=123456 • Truyền dữ liệu bằng phương thức POST • Không xuất hiện cặp name=value trên thanh địa chỉ URL

  25. HTML Form – Request object <html> <head> <title>Form Handler</title> </head> <body> <% String strUsername = request.getParameter("Username"); String strPassword = request.getParameter("Password"); %> <b>Thong tin nguoi dung da nhap:</b><br> Username:<%=strUsername%><br> Password:<%=strPassword%><br> </body> </html> Phương thức getParameter của đối tượng request cho phép lấy về giá trị của biến trên form tên Username, Password.

  26. HTML Form – Multivalue form variables • <html> <head> <title>Form Handler</title></head> • <body> <p>Xử lý biến Form nhiều trị<p> • <Form action = “FormVarMultiValue.jsp” method=“get”> • <Input type="text" name="MultiValueVar"> <br> • <Input type="text" name="MultiValueVar"> <br> • <Input type="text" name="MultiValueVar"> <br> • <Input type="text" name="MultiValueVar"> <br> • <Input type="text" name="MultiValueVar"> <br> • <input type="submit" value="Submit"> • </Form> • </body></html>

  27. HTML Form – Multivalue form variables <html> <body> Các giá trị của biến MultiValueVar: <% String names[] = request.getParameterValues ("MultiValueVar"); for(int i=0;i<names.length;i++) { out.println("<br>" + names[i]); } %> </body> </html>

  28. HTML Form – Parameter List • … • <% • java.util.Enumeration params = request.getParameterNames(); • while (params.hasMoreElements()) { • String paramName = params.nextElement(); • String paramValues[] = request.getParameterValues(paramName); • … • } • %> • …

  29. HTML Form – Processing • HTML nhận dữ liệu từ người dùng & gọi JSP xử lý • JSP nhận dữ liệu từ người dùng & gọi JSP khác xử lý • JSP nhận dữ liệu từ người dùng & đảm trách luôn xử lý

  30. HTML Form – Processing • Ví dụ 1: HTML nhập liệu & JSP xử lý • File loginForm.html • <html> <body> • <h1>Login</h1> • <Form action=“../JSP/loginHandler.jsp" method=“post"> • Username: <Input type="Text" name="Username"> <br> Password: <Input type=“Password" name="Password"> <br><Input type="Submit" value="Submit"> • </Form> • </body> </html>

  31. HTML Form – Processing • File xử lý loginHandler.jsp • <%@page import = "java.sql.*, java.util.*" %> • <html> <head><title>Form Handler</title></head> • <body> • <% String strUsername = request.getParameter("Username"); • String strPassword = request.getParameter("Password"); • Connection con = null; • Statement statment = null; • ResultSet resultSet = null; • boolean loginResult = false;

  32. HTML Form – Processing String sUserNameDB; String sPasswordDB; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con = DriverManager.getConnection ("jdbc:odbc:qlsv;UID=sa;PWD=sa"); statement = con.createStatement(); resultSet = statment.executeQuery("Select * From account");

  33. HTML Form – Processing • if (resultSet != null) { • while (resultSet.next()) { • sUserNameDB= resultSet.getString(1); • sPasswordDB = resultSet.getString(2); • if ((strUsername.equals(sUserNameDB)) && (strPassword.equals(sPasswordDB))) { loginResult = true; • break; • } • } • }

  34. HTML Form – Processing if (loginResult == true) out.println("Thanh cong"); else out.println("Khong thanh cong"); } catch (Exception e) { System.out.println(e); } finally { resultSet.close(); statment.close(); con.close(); } %> </body> </html>

  35. HTML Form – Processing • Sinh viên tự làm các trường hợp: • JSP nhận dữ liệu từ người dùng & gọi JSP khác xử lý • JSP nhận dữ liệu từ người dùng & đảm trách luôn xử lý

  36. Exercises • 1. Viết và chạy các ví dụ trong bài giảng • 2. Xây dựng các trang jsp cho phép • - Thực hiện chức năng login • - Thêm, xóa, sửa thông tin người dùng • Liệt kê danh sách người dùng (phân mỗi trang hiển thị tối đa thông tin của 5 người) • 3. Xây dựng trang giỏ hàng thực hiện chức năng đặt mua hàng trên mạng.

More Related