JavaBean和jsp的开发模型

时间:2023-03-09 20:32:23
JavaBean和jsp的开发模型

1.实体类

 package com.zdsofe.javaBean.work;

 public class Student {

     public String name;
public String sex;
public int age;
public String[] hobby;
public String hobbys; public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public String getHobbys() {
String result="";
if(this.hobby.length>0)
{ for(String str:hobby)
{
result+=str+",";
} }
result=result.substring(0, result.length()-1);
hobbys = result;
return hobbys;
}
public void setHobbys(String hobbys) {
this.hobbys = hobbys;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} }

2.jsp的动作元素

 <%@page import="com.zdsofe.javaBean.work.Student"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body> <!--直接在脚本元素中使用 -->
<%/*
String[] s={"游泳","爬山","跳舞"};
Student student=new Student();
student.setHobby(s);
out.print(student.getHobbys()); */
%> <!-- 利用jsp的动作元素来使用 -->
<% request.setCharacterEncoding("utf-8"); %>
<!-- 创建实体类 -->
<jsp:useBean id="user" class="com.zdsofe.javaBean.work.Student"></jsp:useBean>
<!-- 调用set方法-->
<jsp:setProperty property="*" name="user"/> <%-- <jsp:setProperty property="age" name="user" value="34"/>
<jsp:getProperty property="age" name="user"/> --%> <!-- 调用get方法 -->
<jsp:getProperty property="name" name="user" /><br/>
<jsp:getProperty property="sex" name="user"/><br/>
<jsp:getProperty property="hobbys" name="user"/><br/> </body>
</html>

3.登录界面

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="01.jsp" method="post">
用户名:<input type="text" name="name"/><br/>
性别:<input type="radio" name="sex" value="男"/>男
<input type="radio" name="sex"/ value="女">女<br/>
爱好: <input type="checkbox" name="hobby" value="游泳"/>游泳
<input type="checkbox" name="hobby" value="吃饭" />吃饭
<input type="checkbox" name="hobby" value="睡觉"/>睡觉<br/>
<button type="submit">提交</button>
</form> </body>
</html>