在项目的WebRoot目录下创建并编写input.jsp页面,提供一个表单,在该表单文本框中接收姓名的输入,提交给welcome.jsp页面; ● 在welcome.jsp页面将接收到的姓名存入内置对象session中,若没有输入姓名,则存 入“”,并以EL判断session中存入的姓名是否空串,若是则显示“游客”,否则显示接收到的姓名;然后提供一个表单,要求输入三个数,最后提交给result.jsp页面; ● 在result.jsp页面中接收输入的三个数,若缺少输入则提示要求输入数字并提供超链接返回welcome.jsp页面;否则以EL调用自定义函数triangle判断三个数能否构成-一个三角形,若能则显示这三个数可以构成一个三角形”,并计算其面积;否则显示“这三个数不能构成一个三角形!”; ● 在src目录下创建util.com包,在该包中创建类Triangle,在该类中编辑方法isTriangle,判断三个数能否构成三角形,能则调用area方 法计算器面积并返回信息和计算结果;不能则返回提示信息不能构成三角形; ● 在WebRoot目录中的WEB-INF子目录下创建并编辑myTaglib.tld文件,完成自定义函数的配置。 代码实现
时间: 2024-03-22 17:37:24 浏览: 96
以下是代码实现的示例:
1. input.jsp 页面
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Input Page</title>
</head>
<body>
<form action="welcome.jsp" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
```
2. welcome.jsp 页面
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome Page</title>
</head>
<body>
<%
String name = request.getParameter("name");
if (name == null || name.trim().equals("")) {
name = "";
session.setAttribute("name", name);
} else {
session.setAttribute("name", name);
}
%>
<p>Welcome, ${empty session.name ? "Guest" : session.name}!</p>
<form action="result.jsp" method="post">
<label for="num1">Number 1:</label>
<input type="number" id="num1" name="num1"><br>
<label for="num2">Number 2:</label>
<input type="number" id="num2" name="num2"><br>
<label for="num3">Number 3:</label>
<input type="number" id="num3" name="num3"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
```
3. result.jsp 页面
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="triangle" uri="/WEB-INF/myTaglib.tld" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Result Page</title>
</head>
<body>
<%
String num1 = request.getParameter("num1");
String num2 = request.getParameter("num2");
String num3 = request.getParameter("num3");
if (num1 == null || num1.trim().equals("") || num2 == null || num2.trim().equals("") || num3 == null || num3.trim().equals("")) {
%>
<p>Please enter all three numbers!</p>
<a href="welcome.jsp">Back to Welcome Page</a>
<% } else {
boolean isTriangle = triangle:isTriangle(num1, num2, num3);
if (isTriangle) {
double area = triangle:area(num1, num2, num3);
%>
<p>These three numbers can form a triangle, and the area is ${area}.</p>
<% } else { %>
<p>These three numbers cannot form a triangle!</p>
<% }
} %>
</body>
</html>
```
4. Triangle.java 类
```java
package util.com;
public class Triangle {
public static boolean isTriangle(String num1, String num2, String num3) {
double a = Double.parseDouble(num1);
double b = Double.parseDouble(num2);
double c = Double.parseDouble(num3);
return a + b > c && a + c > b && b + c > a;
}
public static double area(String num1, String num2, String num3) {
double a = Double.parseDouble(num1);
double b = Double.parseDouble(num2);
double c = Double.parseDouble(num3);
double p = (a + b + c) / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
}
}
```
5. myTaglib.tld 文件
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>triangle</short-name>
<uri>/WEB-INF/myTaglib.tld</uri>
<function>
<name>isTriangle</name>
<function-class>util.com.Triangle</function-class>
<function-signature>boolean isTriangle(java.lang.String, java.lang.String, java.lang.String)</function-signature>
</function>
<function>
<name>area</name>
<function-class>util.com.Triangle</function-class>
<function-signature>double area(java.lang.String, java.lang.String, java.lang.String)</function-signature>
</function>
</taglib>
```
注意:以上代码示例仅供参考,实际项目中可能需要根据具体需求进行修改和调整。
阅读全文