JavaWeb -- JSP+JavaBean模式

SUN公司推出JSP技术后,同时也推荐了两种web应用程序的开发模式,一种是JSP+JavaBean模式,一种是Servlet+JSP+JavaBean模式
JSP+JavaBean模式适合开发业务逻辑不太复杂的web应用程序,这种模式下,JavaBean用于封装业务数据,JSP即负责处理用户请求,又显示数据
 
 
Jsp: calculator.jsp
<%@ page language="java" import="java.util.*"  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>简单计算器</title>
</head>
<body style="text-align: center;">

	<jsp:useBean id="calculator" class="com.kevin.Calculator"></jsp:useBean>
	<jsp:setProperty property="*" name="calculator"/>
	<%
		calculator.operation(); 
	%>
	<br />-----------------------------------------------------------<br />
	<jsp:getProperty property="input1" name="calculator"/>
	<jsp:getProperty property="oper" name="calculator"/>
	<jsp:getProperty property="input2" name="calculator"/>=	
	<jsp:getProperty property="result" name="calculator"/>
	<br />-----------------------------------------------------------<br />

	<form action="/WebTest4/calculator.jsp" method="post"> 
	<table width="40%" border="1">
		<tr>
			<td colspan="2">简单计算器</td>			
		</tr>
		
		<tr>
			<td>第一个参数</td>
			<td><input type="text" name="input1" /> </td>
		</tr>
		
		<tr>
			<td>运算符</td>
			<td>
				<select name="oper">
					<option value="+">+</option>
					<option value="-">-</option>
					<option value="*">*</option>
					<option value="/">/</option>
				</select>
			</td>
		</tr>
		
		<tr>
			<td>第二个参数</td>
			<td><input type="text" name="input2" /> </td>
		</tr>
		
		<tr style="text-align: right;">
			<td colspan="2" ><input type="submit" name="bt1" value="计算"/></td>			
		</tr>
	</table>
	</form>

</body>
</html>

javabean类
package com.kevin;

import java.math.BigDecimal;

import javax.management.RuntimeErrorException;

public class Calculator {
	private String input1="0";;
	private String input2="0";
	private String oper="+";
	private String result="0";
	
	public void operation()
	{
		BigDecimal first = new BigDecimal(input1);
		BigDecimal second = new BigDecimal(input2);
		
		System.out.println("oper: " + oper);
		
		if( oper.equals("+") )
			result = first.add(second).toString();
		else if( oper.equals("-") )
			result = first.subtract(second).toString();	
		else if( oper.equals("*") )
			result = first.multiply(second).toString();
		else if( oper.equals("/") )
		{
			if( second.doubleValue()==0 )
			{
				result = "除数不能为0";
				return;
			}
			if( second.doubleValue() > first.doubleValue() )
			{
				result = "除数不要大于被除数";
				return;
			}
			result = first.divide(second).toString();
		}
		else
			throw new RuntimeException("元算符异常");
				
	}
	
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	public String getInput1() {
		return input1;
	}
	public void setInput1(String input1) {
		this.input1 = input1;
	}
	public String getInput2() {
		return input2;
	}
	public void setInput2(String input2) {
		this.input2 = input2;
	}
	public String getOper() {
		return oper;
	}
	public void setOper(String oper) {
		this.oper = oper;
	}
	
}

原文地址:https://www.cnblogs.com/xj626852095/p/3648061.html