JSP与JavaBean


JavaBean是一个可重复使用的软件组件,基于Java语言。
可以实现代码的重复利用;易编写、易维护、易使用;可以在任何安装了Java运行的平台上使用,而无须重新编译。

编写和使用JavaBean

编写JavaBean

为了能让使用bean的应用程序构建工具使用JSP动作标记知道bean的属性和方法,在类的命名要遵守以下规则:
(1)getXxx()——获取属性xxx
setXxx()——修改属性xxx
后缀首字母大写
(2)类中定义的方法的访问权限都必须是public
(3)类中必须有一个构造方法是public、无参数的。
源文件须使用package语句给出包名,如:package tom.jiafei
保存源文件时,“保存类型”为“所有文件”,“编码”为“ANSI”

创建与使用bean

<jsp:useBean id=“bean的名字” class=“创建bean的类” scope=“bean有效范围”/>
例如:

  <jsp:useBean id="circle" class="tom.jiafei.Circle" scope="page"/>
  类名要带包名

page bean 有效范围是当前页面,存活到当前页面执行完毕,分配给每个JSP页面的page bean是互不相同的,不同用户的page bean 也是互不相同的。
session bean有效范围是用户的Web服务目录下的各个页面,存活到会话结束,不同用户的page bean 也是互不相同的。
request bean有效范围是用户请求的当前页面,存活到请求结束,不同用户的page bean 也是互不相同的。
application bean有效范围是当前Web服务目录下的各个页面,存活到Tomcat服务器关闭,不同用户的application bean都是相同的。

获取和修改bean的属性

package tom.jiafei
publiv class Goods{
   
	String name="无名";
	double price=0;
	publiv String getName(){
   
		return name;
	}
	public void setName(String newName){
   
		name=newName;
	}
	public double getPrice(){
   
		return price;
	}
	public void setPrice(double newPrice){
   
		price=newPrice;
	}
}
<%@ page contentType="text/html"%>
<%@ page pageEncoding="utf-8"%>
<% request.setCharacterEncoding("utf-8");%>
<jsp:useBean id="phone" class="tom.jiafei.Goods" scope="page"/>
<HTML><body bgcolor=cyan>
<p style="font-family:黑体;font-size:20;color:red">
<jsp:setProperty name="phone" property="name" param="name"/>
<jsp:setProperty name="phone" property="price" param="price"/>
<br><b>名称:<jsp:getProperty name="phone" property=
  • 5
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以通过以下步骤使用JSPJavaBean实现两个复数的加减乘除: 1. 创建一个复数类,例如Complex,该类应该包含实部和虚部属性以及相应的 getter 和 setter 方法。 ```java public class Complex { private double real; private double imag; public Complex(double real, double imag) { this.real = real; this.imag = imag; } public double getReal() { return real; } public void setReal(double real) { this.real = real; } public double getImag() { return imag; } public void setImag(double imag) { this.imag = imag; } } ``` 2. 创建一个JavaBean,例如ComplexBean,该Bean包含两个Complex对象,分别表示两个复数,以及四个方法,分别实现两个复数的加减乘除操作。 ```java public class ComplexBean { private Complex c1; private Complex c2; public Complex getC1() { return c1; } public void setC1(Complex c1) { this.c1 = c1; } public Complex getC2() { return c2; } public void setC2(Complex c2) { this.c2 = c2; } public Complex add() { double real = c1.getReal() + c2.getReal(); double imag = c1.getImag() + c2.getImag(); return new Complex(real, imag); } public Complex sub() { double real = c1.getReal() - c2.getReal(); double imag = c1.getImag() - c2.getImag(); return new Complex(real, imag); } public Complex mul() { double real = c1.getReal() * c2.getReal() - c1.getImag() * c2.getImag(); double imag = c1.getReal() * c2.getImag() + c1.getImag() * c2.getReal(); return new Complex(real, imag); } public Complex div() { double denominator = c2.getReal() * c2.getReal() + c2.getImag() * c2.getImag(); double real = (c1.getReal() * c2.getReal() + c1.getImag() * c2.getImag()) / denominator; double imag = (c1.getImag() * c2.getReal() - c1.getReal() * c2.getImag()) / denominator; return new Complex(real, imag); } } ``` 3. 创建一个JSP页面,例如complex.jsp,该页面包含一个表单,用户可以输入两个复数的实部和虚部,以及一个下拉框,用户可以选择进行加、减、乘、除操作。当用户提交表单时,JSP页面会调用相应的JavaBean方法,并将结果显示在页面上。 ```html <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="com.example.Complex, com.example.ComplexBean" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Complex Calculator</title> </head> <body> <h1>Complex Calculator</h1> <form action="complex.jsp" method="post"> <label for="real1">Real part of complex 1:</label> <input type="text" name="real1" id="real1"><br> <label for="imag1">Imaginary part of complex 1:</label> <input type="text" name="imag1" id="imag1"><br> <label for="real2">Real part of complex 2:</label> <input type="text" name="real2" id="real2"><br> <label for="imag2">Imaginary part of complex 2:</label> <input type="text" name="imag2" id="imag2"><br> <label for="operator">Operation:</label> <select name="operator" id="operator"> <option value="add">Add</option> <option value="sub">Subtract</option> <option value="mul">Multiply</option> <option value="div">Divide</option> </select><br> <input type="submit" value="Calculate"> </form> <%-- Get the form data and perform the calculation --%> <% // Get the form data double real1 = Double.parseDouble(request.getParameter("real1")); double imag1 = Double.parseDouble(request.getParameter("imag1")); double real2 = Double.parseDouble(request.getParameter("real2")); double imag2 = Double.parseDouble(request.getParameter("imag2")); String operator = request.getParameter("operator"); // Create the Complex objects Complex c1 = new Complex(real1, imag1); Complex c2 = new Complex(real2, imag2); // Create the ComplexBean and set the Complex objects ComplexBean bean = new ComplexBean(); bean.setC1(c1); bean.setC2(c2); // Perform the calculation and display the result Complex result = null; switch (operator) { case "add": result = bean.add(); break; case "sub": result = bean.sub(); break; case "mul": result = bean.mul(); break; case "div": result = bean.div(); break; } out.println("Result: " + result.getReal() + " + " + result.getImag() + "i"); %> </body> </html> ``` 以上就是通过JSPJavaBean实现两个复数的加减乘除的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值