- 写一个页面求阶乘和求加法,阶乘使用include指令嵌入算法页面,加法使用include动作嵌入算法页面。
新建首页index.jsp,写好一些必要的布局
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
<style>
form {
width: 800px;
height: 300px;
margin: 100px auto;
border-radius: 10px;
background-color: #ffaaff;
}
.container {
width: 300px;
height: 100px;
position: relative;
top: 20px;
}
span {
font-size: 20px
}
.input-box {
width: 200px;
height: 30px;
border-radius: 10px;
}
.container2 {
height: 50px;
padding-top: 20px;
}
.button{
width: 150px;
height: 40px;
border-radius: 30px;
background-color: #aaaaff;
}
</style>
</head>
<body>
<form action="index.jsp" method="get">
<div class="container">
<span>求阶乘:</span>
<input type="text" name="num" id="num" class="input-box">
<div style="height: 30px;padding-top: 10px;font-size: 20px;"><%@ include file="include.jsp"%></div>
</div>
<div class="container2">
<span>求加法:</span>
<input type="text" name="num1" id="num1" class="input-box">+
<input type="text" name="num2" id="num2" class="input-box">
<div style="height: 30px;padding-top: 10px;font-size: 20px;"><jsp:include page="answer.jsp"></jsp:include></div>
</div><br><br>
<input type="submit" value="计算" class="button">
</form>
</body>
</html>
再建一个include.jsp(名字可以自取),用来写阶乘算法
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String s = request.getParameter("num");
if (s==null){
s="1";
}
int num=Integer.parseInt(s);
double t=1;
for (int i=1;i<=num;i++)
t=t*i;
out.print(num+"的阶乘为:"+t);
%>
再建一个answer.jsp,用来写加法算法
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
String o = request.getParameter("num1");
Integer num1 = 0;
if (o != null) {
num1 = Integer.parseInt(o);
}
String h = request.getParameter("num2");
Integer num2 = 0;
if (h != null) {
num2 = Integer.parseInt(h);
}
Integer result = num1 + num2;
out.print("两数相加的结果为:"+result);
%>
</body>
</html>
运行结果: