Jsp中使用JavaBean

本文介绍了一个使用JSP和JavaBean实现动态网页的具体案例。通过创建Person类作为JavaBean,该类包含姓名、年龄及生日等属性,并在JSP页面中使用jsp:useBean、jsp:setProperty和jsp:getProperty标签进行实例化、赋值和获取属性值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JavaBeanDemo.java文件:

<%@ page language="java" import="java.util.*,com.neu.domain.Person" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>在jsp中使用JavaBean</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
    <jsp:useBean id="p" class="com.neu.domain.Person" scope="page"></jsp:useBean>
    <jsp:setProperty property="name" name="p" value="李四"/>
    <jsp:setProperty property="age" name="p" value="27"/>
    <jsp:setProperty property="birthday" name="p" value="<%=new Date() %>"/>
    <jsp:getProperty property="name" name="p"/>
    <jsp:getProperty property="age" name="p"/>
    <jsp:getProperty property="birthday" name="p"/>
  </body>
</html>

Person.java文件:

package com.neu.domain;

import java.io.Serializable;
import java.util.Date;

public class Person implements Serializable {
    private String name;
    private int age;//基本类型自动转换
    private Date birthday;
    public Person() {
        super();
    }
    public Person(String name, int age, Date birthday) {
        super();
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

}

运行:http://localhost:8080/JspProject/javaBeanDemo.jsp
结果:李四 27 Sat Sep 03 19:41:45 CST 2016

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值