JSP 页面间参数传递 - 使用 <jsp:param> 标签
在 main.jsp 页面中,我们应该使用 <jsp:param> 标签来传递参数给被包含的 circle.jsp 和 ladder.jsp 页面。以下是修改后的代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Main Page</title>
</head>
<body>
<h1>Main Page</h1>
<jsp:include page="circle.jsp">
<jsp:param name="radius" value="5" />
</jsp:include>
<jsp:include page="ladder.jsp">
<jsp:param name="upperBase" value="3" />
<jsp:param name="lowerBase" value="5" />
<jsp:param name="height" value="8" />
</jsp:include>
</body>
</html>
circle.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Circle Page</title>
</head>
<body>
<h2>Circle Page</h2>
<%
double radius = Double.parseDouble(request.getParameter("radius"));
double area = Math.PI * radius * radius;
%>
<p>Radius: <%= radius %></p>
<p>Area of Circle: <%= area %></p>
</body>
</html>
ladder.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Ladder Page</title>
</head>
<body>
<h2>Ladder Page</h2>
<%
double upperBase = Double.parseDouble(request.getParameter("upperBase"));
double lowerBase = Double.parseDouble(request.getParameter("lowerBase"));
double height = Double.parseDouble(request.getParameter("height"));
double area = (upperBase + lowerBase) * height / 2;
%>
<p>Upper Base: <%= upperBase %></p>
<p>Lower Base: <%= lowerBase %></p>
<p>Height: <%= height %></p>
<p>Area of Ladder: <%= area %></p>
</body>
</html>
现在,在 main.jsp 页面中,我们使用 <jsp:param> 标签来为 circle.jsp 和 ladder.jsp 页面传递参数。在 circle.jsp 页面和 ladder.jsp 页面中,我们使用 request.getParameter() 方法来获取传递的参数值,并进行相应的计算和显示。
请确保这三个 JSP 页面在同一 Web 服务目录中,并通过访问 main.jsp 页面来查看结果。
原文地址: https://www.cveoy.top/t/topic/cdOK 著作权归作者所有。请勿转载和采集!