设计一个页面,用户在上面输入梯形的上底、下底和高的值,提交后显示出梯形的面积和周长,使用JavaBean类封装。要求分别采用标签和代码的形式调用JavaBean类编程实现。设计一个页面,用户在上面输入梯形的上底、下底和高的值,提交后显示出梯形的面积和周长,使用JavaBean类封装。要求分别采用标签和代码的形式调用JavaBean类编程实现。
时间: 2024-02-06 14:11:35 浏览: 167
好的,我已经理解你的要求。以下是实现该功能的代码:
1. JavaBean类 Tixing.java
```java
public class Tixing {
private double shang;
private double xia;
private double gao;
private double zhouchang;
private double mianji;
public double getShang() {
return shang;
}
public void setShang(double shang) {
this.shang = shang;
}
public double getXia() {
return xia;
}
public void setXia(double xia) {
this.xia = xia;
}
public double getGao() {
return gao;
}
public void setGao(double gao) {
this.gao = gao;
}
public double getZhouchang() {
zhouchang = shang + xia + 2 * Math.sqrt(Math.pow((xia - shang) / 2, 2) + Math.pow(gao, 2));
return zhouchang;
}
public double getMianji() {
mianji = (shang + xia) * gao / 2;
return mianji;
}
}
```
2. 标签形式调用的JSP页面 tixing_tag.jsp
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="my" uri="/WEB-INF/tlds/tixing.tld"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>梯形计算器(标签形式调用)</title>
</head>
<body>
<h1>梯形计算器(标签形式调用)</h1>
<my:tixing />
</body>
</html>
```
3. 标签实现 tixing.tld
```xml
<?xml version="1.0" encoding="UTF-8"?>
<taglib 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"
version="2.1">
<tlib-version>1.0</tlib-version>
<short-name>tixing</short-name>
<uri>/WEB-INF/tlds/tixing.tld</uri>
<tag>
<name>tixing</name>
<tag-class>com.example.TixingTag</tag-class>
<body-content>JSP</body-content>
</tag>
</taglib>
```
4. 标签处理类 TixingTag.java
```java
package com.example;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import com.example.Tixing;
public class TixingTag extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
Tixing tixing = new Tixing();
JspWriter out = getJspContext().getOut();
out.println("<form method=\"post\">");
out.println("上底:<input type=\"number\" name=\"shang\" /><br/>");
out.println("下底:<input type=\"number\" name=\"xia\" /><br/>");
out.println("高:<input type=\"number\" name=\"gao\" /><br/>");
out.println("<input type=\"submit\" value=\"提交\" />");
out.println("</form>");
String shang = (String) getJspContext().getAttribute("shang");
String xia = (String) getJspContext().getAttribute("xia");
String gao = (String) getJspContext().getAttribute("gao");
if (shang != null && xia != null && gao != null) {
tixing.setShang(Double.parseDouble(shang));
tixing.setXia(Double.parseDouble(xia));
tixing.setGao(Double.parseDouble(gao));
out.println("<p>周长:" + tixing.getZhouchang() + "</p>");
out.println("<p>面积:" + tixing.getMianji() + "</p>");
}
}
}
```
5. 代码形式调用的JSP页面 tixing_code.jsp
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>梯形计算器(代码形式调用)</title>
</head>
<body>
<h1>梯形计算器(代码形式调用)</h1>
<form method="post">
上底:<input type="number" name="shang" /><br/>
下底:<input type="number" name="xia" /><br/>
高:<input type="number" name="gao" /><br/>
<input type="submit" value="提交" />
</form>
<%
String shang = request.getParameter("shang");
String xia = request.getParameter("xia");
String gao = request.getParameter("gao");
if (shang != null && xia != null && gao != null) {
com.example.Tixing tixing = new com.example.Tixing();
tixing.setShang(Double.parseDouble(shang));
tixing.setXia(Double.parseDouble(xia));
tixing.setGao(Double.parseDouble(gao));
out.println("<p>周长:" + tixing.getZhouchang() + "</p>");
out.println("<p>面积:" + tixing.getMianji() + "</p>");
}
%>
</body>
</html>
```
建议将 Tixing.java 和 TixingTag.java 放在 src/com/example 目录下,tixing_tag.jsp 和 tixing_code.jsp 放在 WebContent 目录下,tixing.tld 放在 WEB-INF/tlds 目录下。记得在 web.xml 中配置标签库。
最后,你可以访问 tixing_tag.jsp 和 tixing_code.jsp 查看效果。
阅读全文