JSP:使用EL表达式调用java函数

JSP:使用EL表达式调用java函数

使用一个例子

编写一个使用JSP表达式语言的JSP程序,输入一个数字,提交后输出该数字的九九乘法表。

java代码实现打印9*9乘法表 
Solution.java

package method;

public class Solution {
    public static String Mul99(int n){
        String result="";
        for(int i=1;i<=9;i++){
            int temp=i*n;
            result=result+i+" X "+n+"="+temp+"
"
; } return result; } public static void main(String[] args){ System.out.print(Mul99(4)); } }

在WEB-INF下建立文件 
utils.tld



<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
  version="2.0">

  <description>MY Util 1.1 functions librarydescription>
  <display-name>MY Util functionsdisplay-name>
  <tlib-version>1.1tlib-version>
  <short-name>mfnshort-name>
  <uri>http://www.inspuruptec.com/jsp/jstl/utiluri>

  <function>
    <description>
      mul99
    description>
    <name>mulname>
    <function-class>method.Solutionfunction-class>
    <function-signature>java.lang.String Mul99(int)function-signature>
    <example>
    example>
  function>
taglib>

99.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<%@taglib uri="http://www.inspuruptec.com/jsp/jstl/util" prefix="fn"%>

<html>
    <head>
        <base href="<%=basePath%>">

        <title>My JSP '99.jsp' starting pagetitle>

        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        

    head>

    <body>
        <center>
            <form method="POST">
                输入一个数
                <input type="text" name="num">
                <br>
                <input type="submit" value="99乘法" />
            form>

            ${fn:mul((param.num)) }
        center>
    body>


html>
                                    

你可能感兴趣的:(jsp)