JSP——使用自定义标记,编写标签处理类Rect.java,编写相应的tld文件,编写jsp页面,该jsp页面使用自定义标记,完成计算打印输出长方形面积的功能。

1.创建web项目three,新建包sun.moon,新建rect.java文件

2.WebRoot下的WEB-INF下新建tlds文件夹,新建my.tld文件

3.新建MyJsp.jsp文件

4.rect.java文件代码如下

package sun.moon;

 

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.tagext.SimpleTagSupport;

import java.io.IOException;

public class rect extends SimpleTagSupport {

private String Length;

private String Width;

public void doTag() throws JspException,IOException{

getJspContext().getOut().write("Length="+this.Length+"," +

"Width="+this.Width+getArea(Double.parseDouble(Length),Double.parseDouble(Width)));

}

public void setLength(String Length)

{

this.Length=Length;

}

public void setWidth(String Width)

{

this.Width=Width;

}

public String getArea(double a,double b)

{

if(a>0&&b>0)

{

double area=a*b;

return "<br>长方形的面积:"+area;

}else{

return("<br>"+a+","+b+","+"不能构成一个长方形,无法计算面积");

}

}

}

5.my.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>

         简单标记自定义标记库

      </description>

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

<short-name>my</short-name>

<uri>http://myWeb.com/jsp/myTags</uri>

<tag>

<description>Outputs a colored tile</description>

<name>my1</name>

<tag-class>sun.moon.rect</tag-class>

<body-content>empty</body-content>

<attribute>

<name>length</name>

<required>true</required>

</attribute>

<attribute>

<name>width</name>

<required>true</required>

</attribute>

</tag>

</taglib>

 

6.MyJsp.jsp文件代码如下

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

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

<html>

  <body>

<me:my1 length="10" width="10"/>

  </body>

</html>

 

7.浏览器显示效果

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值