} type="package.class"">action for bean
id=”beanName”:定义生成bean组件的名字,不能重名且scope的值也最好一样,否则JSP引擎将认定最先被创建的那个。
scope=”page|request|session|application”:定义bean的活动范围,bean只有在它定义的范围里才能使用,在它的活动范围外将无法访问到它。
TestBean.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>My JSP 'TestBean.jsp' starting page</title>
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
-
- </head>
-
- <body>
-
- <jsp:useBean id="student" scope="page" class="cn.Student" />
- <%
- student.SetStuName("小马");
- %>
- <%=student.AttendClass("English Course") %>
- <% out.println("
"); %>
- <%=student.GetStuName()+"的学号是: "+student.GetStuNo() %>
- </body>
- </html>
Student.java
- package cn;
- public class Student {
- private String StuName;
- private int StuNo;
- private static int No = 20110001;
-
-
- public Student(){
- StuName="DefaultName";
- StuNo = No++;
- }
- public void SetStuName(String name){
- StuName=name;
- }
- public String GetStuName(){
- return StuName;
- }
- public String GetStuNo(){
- return String.valueOf(this.StuNo);
- }
- public String AttendClass(String ClassName){
- return GetStuName()+"Attend "+ClassName;
- }
- }
阅读(807) | 评论(1) | 转发(0) |