[JSP] – Bài Tập: Sử Dụng EL Và Gọi Static Function Bằng EL Trên JSP

Mục tiêu:

  • Sử dụng EL trên trang jsp
  • Đăng ký sử dụng static function trên website
  • Gọi static funtion từ trang jsp

B1:

Tạo lớp java như sau:

package codes; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.logging.Level; import java.util.logging.Logger; public class staticFunctions { public static int add(int a, int b) { return a + b; } public static String row(int id) { try { String strRow = ""; Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Connection conn = DriverManager.getConnection("" + "jdbc:sqlserver://localhost;database=northwind;user=sa;password=sa;"); PreparedStatement comm = conn.prepareStatement("" + "Select EmployeeID, FirstName, LastName " + "From employees where employeeID=?"); comm.setInt(1, id); ResultSet rs = comm.executeQuery(); if (rs.next()) { strRow += "<tr>"; strRow += "<td>"; strRow += rs.getString("EmployeeID"); strRow += "</td>"; strRow += "<td>"; strRow += rs.getString("FirstName"); strRow += "</td>"; strRow += "<td>"; strRow += rs.getString("LastName"); strRow += "</td>"; strRow += "</tr>"; } return strRow; } catch (Exception ex) { Logger.getLogger(staticFunctions.class.getName()).log(Level.SEVERE, null, ex; return ex.toString(); } } }

B2: Đăng ký static function với webserver – Tạo file TLD (new -> file -> web -> Tag Library Descriptor) – Hiệu chỉnh file như sau

<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"> <tlib-version>1.0</tlib-version> <short-name>myfunction</short-name> <uri>/WEB-INF/tlds/myFunction</uri> <function> <name>add</name> <function-class>codes.staticFunctions</function-class> <function-signature> int add(int, int) </function-signature> </function> <function> <name>row</name> <function-class>codes.staticFunctions</function-class> <function-signature> String row(int) </function-signature> </function> </taglib>

B3: – Đăng ký sử dụng trên trang jsp – Bổ sung thẻ sau vào trang jsp

<%@taglib prefix="fn" uri="/WEB-INF/tlds/myFunction" %>

– Hiệu chỉnh nội dung trang index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="fn" uri="/WEB-INF/tlds/myFunction" %> <%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <h1>Hello World!</h1> ${header} <form action="" method="post"> Frist: <input type="text" name="f" value="${param.f}" /> Second: <input type="text" name="s" value="${param.s}" /> <input type="submit" value="Add" /> </form> <c:if test="${not empty param.f}"> ${fn:add(param.f,param.s)} </c:if> <table> <tr> <td>ID</td> <td>FirstName</td> <td>LastName</td> </tr> <c:forEach begin="1" end="9" var="i" step="2"> ${fn:row(i)} </c:forEach> </table> </body> </html>

B4: chạy và kiểm tra kết quả: p1

Video tham khảo –> 

Chia sẻ:

  • In
  • Email
  • Tweet
Thích Đang tải...

Liên quan

Từ khóa » Gọi Hàm Java Trong Jsp