编写两个JSP页面:main.jsp和lader.jsp,将两个JSP页面保存在同一WEB服务目录中。mian.jsp使用include动作标志动作动态加载lader.jsp页面。lader.jsp页面可以计算并显示梯形的面积。当lader.jsp被加载时获取main.jsp页面中include动作标记的param子标记提供的梯形上下底和高的值。
//main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>main.jsp</title>
</head>
<body>
<jsp:include page="lader.jsp">
<jsp:param value="5" name="a"/>
<jsp:param value="6" name="b"/>
<jsp:param value="10" name="h"/>
</jsp:include>
</body>
</html>
//lader.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String strA=request.getParameter("a");
String strB=request.getParameter("b");
String strH=request.getParameter("h");
double a=Double.parseDouble(strA);
double b=Double.parseDouble(strB);
double h=Double.parseDouble(strH);
double area=(a+b)*h/2;
%>
<p> 梯形面积:<%= area %>
</body>
</html>
二、编写一个主程,通过表单输入一个正整数n然后通过动作include插入一个计算1~n的累加和程序
//include.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>include动作</title>
</head>
<body><FONT Size=2>
<table border = 2>
<tr>
<th>请输入正整数n,求1~n的累加和 </th>
</tr>
<tr>
<td><jsp:include page="addn.jsp" /></td>
</tr>
</table>
</FONT>
</body>
</html>
//addn.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<FORM action=""method=post name=form>
<INPUT type ="text"name="ok">
<BR><INPUT TYPE="submit"value="累加和"name=submit>
</FORM>
<%
String a=request.getParameter("ok");
if(a==null){a="0";}
int n=Integer.parseInt(a);
int sum=0;
for(int i=1;i<=n;i++){
sum=sum+i;
}
out.print(sum);
%>
</body>
</html>