Java web 5-2 JavaBean+JSP求任意两数代数和
package beans;
public class Add {
private int shuju1;
private int shuju2;
private int sum;
public Add(){}
public int getShuju1(){return shuju1;}
public void setShuju1(int shuju1){this.shuju1=shuju1;}
public int getShuju2(){return shuju2;}
public void setShuju2(int shuju2){this.shuju2=shuju2;}
public int getSum(){return shuju1+shuju2;}
public void getSum(int sum){this.sum=sum;}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>提交任意2个整数的页面</title>
</head>
<body>
<h3>按下列格式要求,输入两个整数:</h3><br>
<form action="show.jsp" method="post">
加数:<input name="shuju1"><br><br>
被加数:<input name="shuju2"><br><br>
<input type=submit value="提交">
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>利用JavaBean+JSP求两数和</title>
</head>
<body>
<jsp:useBean id="c" class="beans.Add" scope="request"/>
<jsp:setProperty name="c" property="*"/>
<p>调用jsp:getProperty 作标签显示结果值:<br>
<jsp:getProperty name="c" property="shuju1"/>+
<jsp:getProperty name="c" property="shuju2"/>=
<jsp:getProperty name="c" property="sum"/>
</p>
</body>
</html>
运行: