【JSP篇】——4.Jsp动作之useBean,jsp:setProperty,jsp:getProperty使用的一个简单例子

该博客通过一个简单的例子展示了如何在JSP中使用useBean、setProperty和getProperty动作来操作JavaBean。首先创建了一个名为Student的JavaBean类,接着在UseBean.jsp页面中利用这些动作进行访问和修改属性。最终,将工程部署到Tomcat服务器,并给出了访问页面的URL以展示运行效果。
摘要由CSDN通过智能技术生成

1.工程说明

创建一个JavaBean类:Student,然后创建UseBean.jsp使用useBean访问并修改属性

2.运行效果

3.Student类,写在一个test包中

package test;
/*
 * 2018.3.26 Dragon
 * JavaBean规范
 	1.序列化
 	2.无需构造器,默认已写好
 	3.属性需要更改器和访问器
 */
public class Student {
	private String id;
	private String name;
	

	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}

4.UseBean.jsp页面:使用JSP的动作访问Student

<%@ page language="java" import="java.util.*" contentType="text/html;charset=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>My JSP 'UseBean.jsp' starting page</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>
    <h2>useBean,jsp:setProperty,jsp:getProperty使用的一个简单例子</h2> <br>
    <hr>
    
    <!-- 访问当前的JavaBean:Student -->
    <jsp:useBean id="student" class="test.Student" />
    
    <!-- 修改当前的属性 -->
    <jsp:setProperty name="student" property="name" value="小龙"  />
    <jsp:setProperty name="student" property="id" value="5201314" />
    
    <!--  访问当前的属性 ,在屏幕上输出 -->
    <table>
    	<tr>
    		<td>姓名:</td>
    		<td><jsp:getProperty name="student" property="name" /></td>
    	</tr>
    	<tr>
    		<td>学号:</td>
    		<td><jsp:getProperty name="student" property="id" /></td>
    	</tr>
    </table>
  </body>
</html>

5.将工程部署到Tomcat,加载并shurtu输入地址:http://localhost:8080/JspAction/UseBean.jsp效果如2所示。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值