·使用 eclipse 定义一个类 Compute，包为 test。

·把所得到的 Compute.class 文件复制到 jsp_test/ WEB-INF/classes/test 目录。

package test;

public class Compute {

public static int add(String x, String y) {

int a = 0;

int b = 0;

try {

//把字符串转化为整数

a = Integer.parseInt(x);

b = Integer.parseInt(y);

}catch(Exception e) {}

return a + b;

}

}

在 jsp_test/WEB-INF/目录下创建：mytaglib.tld 文件：

<?xml version="1.0" encoding="UTF-8" ?>

<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>A tag library exercising SimpleTag handlers.</description>

<tlib-version>1.0</tlib-version>

<short-name>MyTagLibrary</short-name>

<uri>/MyTagLibrary</uri>

<function>

<description>add x and y</description>

<name>add</name>

<function-class>test.Compute</function-class>

<function-signature>int

add(java.lang.String,java.lang.String)

</function-signature>

</function>

</taglib>

进入 jsp_test 目录，在该目录建立 mypage15.jsp。

<%@page contentType="text/html;charset=gb2312"%>

<%@ page import="java.util.*" %>

<%request.setCharacterEncoding("gb2312");%>

<%@ taglib prefix="my" uri="WEB-INF/mytaglib.tld" %>

<html>

<head>

<title> 学习 JSP </title>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312"><style type="text/css">

<!--

body {

background-color: #CCCCCC;

}

-->

</style></head>

<body>

<table width="474" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#336699">

<tr>

<td width="470" bgcolor="#FFFFFF"><img src="images/rabbit.gif" width="468" height="60"></td>

</tr>

<tr>

<td height="144" bgcolor="#FFFFFF"><div align="center">

<H3>数字加法</H3>

<P>

<FORM action="mypage15.jsp" method="GET">

<div align="left">X =

<input type="text" name="x" value="${param["x"]}">

<BR>

Y =

<input type="text" name="y" value="${param["y"]}">

<input type="submit" value="相加">

</div>

</FORM>

<P>

两者的和是: ${my:add(param["x"],param["y"])}

</div></td>

</tr>

</table>

</body>

</html>

重新启动 Tomcat

在浏览器上打开下面连接

http://localhost:8080/jsp_test/mypage15.jsp