在JSP中调用JavaBean示例代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
< !-- 在jSP中使用JavaBean一般遵循以下四步
(1)导入JavaBean的类
(2) 在指定范围内实例化一个JavaBean对象,如果该对象已经存在则直接引用
(3) 对Javabean的实例对象进行操作
(4)从指定范围内移除JavaBean
-->
< %@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
< %@page import="JavaBeanTest.CircleArea" %>
< jsp:useBean id="circle1" class="JavaBeanTest.CircleArea" scope="page"/>
< jsp:useBean id="circle2" class="JavaBeanTest.CircleArea" scope="request"/>
< jsp:useBean id="circle3" class="JavaBeanTest.CircleArea" scope="session"/>
< jsp:useBean id="circle4" class="JavaBeanTest.CircleArea" scope="application" />
<%@ page import="com.test.*" %> //跟java的import一样,导入类就可以用了
<%@ page import="com.test.*" %>
//Test.java
package com.test;
public class Test {
public String getMessage(){
return "Hello";
}
}
//Hello.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ page import="com.test.*" %>
<%@ page import="包名" %> //先导入包
<%
Test t=new Test(); //实例化类就可以了
%>