分类: Java
2006-03-14 11:50:45
JavaBean传统的应用在于可视化领域,如AWT下应用。现在,JavaBean更多的应用在于不可视化领域,它在服务器端应用方面表现出了越来越强的生命力。不可视化的JavaBean和可视化的JavaBean同样使用属性和事件。不可视化的JavaBean在JSP程序中常用来封装事务逻辑、数据库操作等,可以很好地实现业务编辑和前台程序的分离,使得系统具有更好的健壮和灵活性。
例子:SimpleCalculato.java
package com.starxing;
public class SimpleCalculator {
private String first;
private String second;
private double result;
private String operator;
public void setFirst(String first) {
this.first = first;
}
public void setSecond(String second) {
this.second = second;
}
public void setOperator(String operator) {
this.operator = operator;
}
public String getFirst() {
return first;
}
public String getSecond() {
return second;
}
public String getOperator() {
return operator;
}
public double getResult() {
return result;
}
public void calculate() {
double one = Double.parseDouble(first);
double two = Double.parseDouble(second);
try {
if (operator.equals("+"))
result = one + two;
else if (operator.equals("-"))
result = one - two;
else if (operator.equals("*"))
result = one * two;
else if (operator.equals("/"))
result = one / two;
else
System.out.println("error operator");
} catch (Exception e) {
e.printStackTrace();
}
}
}
calculate.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
计算结果:<%
try
{
calculator.calculate();
out.println(calculator.getFirst()+calculator.getOperator()+calculator.getSecond()+"="+calculator.getResult());
}
catch(Exception e)
{
out.println(e.getMessage());
}
%>