文本浏览器
文章号 : 14 | 阅读量 : 0+1 | AAW值(?) : 3.00 (仅供参考)
简单JavaBean
发布者 : 管理员-Adler | 发布时间 : 2019-02-15 09:15:13文章号 : 14 | 阅读量 : 0+1 | AAW值(?) : 3.00 (仅供参考)
1. package com.hotpot.main;
2. public class SimpleBean{
3. private String name;
4. private int age;
5. public void setName(String name){
6. this.name = name;
7. }
8. public void setAge(int age){
9. this.age = age;
10. }
11. public String getName(){
12. return this.name;
13. }
14. public int getAge(){
15. return this.age;
16. }
17.}
如果在一个类中只包含属性、setter、getter方法,那么这种类就成为简单JavaBean。
实例
Bean.class
package model; public class Bean { private String message="默认default"; public String getMessage() { return(message); } public void setMessage(String message) { this.message = message; } }
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP 程序</title> </head> <body> <%! private int initVar = 0; private int serviceVar = 0; private int destroyVar = 0; public void jspInit() { initVar++; System.out.println("初始化" + initVar + "次."); } public void jspDestroy() { destroyVar++; System.out.println("被销毁" + destroyVar + "次."); } %> <% //自动刷新 response.setIntHeader("Refresh", 5); serviceVar++; System.out.println("客户端响应" + serviceVar + "次."); String a = "初始化" + initVar + "次."; String b = "响应客户端" + serviceVar + "次."; String c = "销毁" + destroyVar + "次."; String ipadd = "IP: " + request.getRemoteAddr(); String date = new java.util.Date().toLocaleString(); %> <h1>TEST</h1> <h1>测试</h1> <%=a%> <%=b%> <%=c%> <br> <%=ipadd%> <br> <%=date%> <br> <%for (int i = 0; i < 10; i++) {%> <font color="#8a2be2" size="<%=i%>"> TEST </font> <%}%> <%--调用Bean实例--%> <jsp:useBean id="test" class="model.Bean"/> 调用Bean输出: <jsp:setProperty name="test" property="message" value="修改后Edit"/> <jsp:getProperty name="test" property="message"/> </body> </html>运行结果