关于javaBean的应用

首先建设相关信息的类,此例以Patient.java为例

package aboutBean;

public class Patient 
{
private String name;
private int age;
private String sex;
private String diagnosis;

public String getName()
{
	return name;
}
	public void setName(String nameString)
	{
		this.name=nameString;
	}
	
	public int getAge()
	{
		return age;
	}
	public void setAge(int ageString)
	{
		this.age=ageString;
	}
	public String getSex()
	{
		return sex;
	}
	public void setSex(String sexString)
	{
		this.sex=sexString;
	}
	
	public String getDiagnosis()
	{
		return diagnosis;
	}
	public void setSymptom(String diagnosisString)
	{
		this.diagnosis=diagnosisString;
	}
	}

此后的getter与setter方法可以右键点击Patient.java,应用source菜单项,再自动生成方法。
index.jsp提供录入信息的方法

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width,initia-scale=1.0,minimum-scale=0.5,maximus-scale=2.0,user-scalable=yes"/>
<meta charset="ISO-8859-1">
<title>useofjavabean to input information of patients</title>
</head>
<body>
<form action="information.jsp" method="post">
<table align="center" width="400" height="200" border="1">
<tr><td align="center"><b>add information of patients</b></td></tr>
<tr><td>name:<input align="center" type="text" name="name"></td></tr>
<tr><td>age:<input type="text" name="age"></td></tr>
<tr><td>sex:<input type="text" name="sex"></td></tr>
<tr><td>diagnosis:<input type="input" name="diagnosis">
<tr><td>submit:<input type="submit" value="Add"></td></tr>
</table>
</form>
</body>
</html>

获取信息并呈现出来

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=0.5,maximus-scale=2.0 user-scalable=yes">
<title>Insert title here</title>
</head>
<body>
<%request.setCharacterEncoding("utf-8"); %>
<jsp:useBean id="patient" class="aboutBean.Patient" scope="page">
<jsp:setProperty name="patient" property="*"/>
</jsp:useBean>
<table align="center" width="600"  border="1">
<tr><td align="center"><b>information of the patients</b></td></tr>
<tr><td align="center">name of the patient:</td><td><jsp:getProperty property="name" name="patient"/></td></tr>
<tr><td align="center">age of the patient:</td><td><jsp:getProperty property="age" name="patient"/></td></tr>
<tr><td align="center">sex of the patient:</td><td><jsp:getProperty property="sex" name="patient"/>
<tr><td align="center">diagnosis of the patient:</td><td><jsp:getProperty property="diagnosis" name="patient"/>
</table>
</body>
</html>
发布了28 篇原创文章 · 获赞 0 · 访问量 336

猜你喜欢

转载自blog.csdn.net/weixin_45003282/article/details/102685452