JavaBean
Posted 猪八戒1.0
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaBean相关的知识,希望对你有一定的参考价值。
一.JavaBean概述
二.JavaBean种类
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="result.jsp" method="post">
<table width="250px" height="100px" border="1px" align="center">
<tr>
<td colspan="2" align="center">
<b>登录页面</b>
</td>
</tr>
<tr>
<td >用户名:</td>
<td>
<input type="text" name="username">
</td>
</tr>
<tr>
<td >密码:</td>
<td>
<input type="password" name="password">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit">
</td>
</tr>
</table>
</form>
</body>
</html>
result.jsp
<%@ page import="model.User" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<%
String username= request.getParameter("username");
String password= request.getParameter("password");
//将数据封装到Javabean中
User user=new User(username,password);
if("admin".equals(user.getUsename())&&"123456".equals(user.getPossword()))
out.print("登录成功");
else
out.print("登录失败");
%>
<br>
<a href="index.jsp">返回</a>
</div>
</body>
</html>
User.java
package model;
public class User
private String usename;
private String possword;
public User(String usename, String possword)
super();
this.usename = usename;
this.possword = possword;
public String getUsename()
return usename;
public void setUsename(String usename)
this.usename = usename;
public String getPossword()
return possword;
public void setPossword(String possword)
this.possword = possword;
三.JavaBean的应用
3.1获取JavaBean属性信息
如果写的类有初始值可以用getProperty获取
stuInfo.jsp
<%@page import="model.Student" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 使用JavaBean 并设置id -->
<jsp:useBean id="stu" class="model.Student"></jsp:useBean>
<!-- getProperty其实调用的时Student类中的get方法 -->
<ul>
<li>
学生名字:<jsp:getProperty property="name" name="stu"/>
</li>
<li>
学生年龄:<jsp:getProperty property="age" name="stu"/>
</li>
<li>
学生性别:<jsp:getProperty property="sex" name="stu"/>
</li>
</ul>
</body>
</html>
没写无参构造函数会调用默认无参构造函数
Student.java
package model;
public class Student
private String name="张三";
private int age=18;
private String sex="男";
public String getName()
return name;
public int getAge()
return age;
public String getSex()
return sex;
3.2 对JavaBean属性赋值
stuInfo.jsp
<%@page import="model.Student" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 使用JavaBean 并设置id -->
<jsp:useBean id="stu" class="model.Student"></jsp:useBean>
<jsp:setProperty property="name" value="李四" name="stu"/>
<jsp:setProperty property="age" value="20" name="stu"/>
<jsp:setProperty property="sex" value="女" name="stu"/>
<!-- getProperty其实调用的时Student类中的get方法 -->
<ul>
<li>
学生名字:<jsp:getProperty property="name" name="stu"/>
</li>
<li>
学生年龄:<jsp:getProperty property="age" name="stu"/>
</li>
<li>
学生性别:<jsp:getProperty property="sex" name="stu"/>
</li>
</ul>
</body>
</html>
Student.java
package model;
public class Student
private String name="张三";
private int age=18;
private String sex="男";
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 String getSex()
return sex;
public void setSex(String sex)
this.sex = sex;
四.综合练习
add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="info.jsp" method="post">
<table align="center" border="1" width="240px" height="120px">
<tr>
<td colspan="2" align="center">
<b>添加图书信息</b>
</td>
</tr>
<tr>
<td>名称:</td>
<td>
<input type="text" name="name">
</td>
</tr>
<tr>
<td>作者:</td>
<td>
<input type="text" name="author">
</td>
</tr>
<tr>
<td>价格:</td>
<td>
<input type="text" name="price">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="添加">
</td>
</tr>
</table>
</form>
</body>
</html>
info.jsp
<%@ page import="model.Book" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//防止乱码
request.setCharacterEncoding("utf-8");
%>
<div align="center">
<jsp:useBean id="book" class="model.Book"></jsp:useBean>
<!-- *表示取所有的 -->
<jsp:setProperty property="*" name="book"/>
<ul>
<li>
图书名:<jsp:getProperty property="name" name="book"/>
</li>
<li>
图书作者:<jsp:getProperty property="author" name="book"/>
</li>
<li>
图书名:<jsp:getProperty property="price" name="book"/>
</li>
</ul>
</div>
</body>
</html>
Book.java
package model;
public class Book
private String name;
private String author;
private double price;
public String getName()
return name;
public void setName(String name)
this.name = name;
public String getAuthor()
return author;
public void setAuthor(String author)
this.author = author;
public double getPrice()
return price;
public void setPrice(double price)
this.price = price;
五.使用JavaBean解决中文乱码
以前做法:
<% request.setCharacterEncoding("utf-8");%>
使用JavaBean解码做法
EncodingCharacter.java
package model;
import java.io.UnsupportedEncodingException;
public class EncodingCharacter
public EncodingCharacter()
//用于解码的方法
public String toString(String str) throws UnsupportedEncodingException
String s="";
if(str!=null||"".equals(str))
s=new String(str.getBytes("ISO-8859-1"),"utf-8");
return s;
email.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="release.jsp" method="post">
<table align="center" width="300px" border="1">
<tr>
<td colspan="2" align="center">
<b>邮件发送</b>
</td>
</tr>
<tr>
<td>标题</td>
<td>
<input type="text" name="title">
</td>
</tr>
<tr>
<td>内容</td>
<td>
<textarea rows="10" cols="30" name="content"></textarea>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>
</body>
</html>
release.jsp
<%@ page import="model.Email" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="email" class="model.Email"></jsp:useBean>
<jsp:useBean id="encoding" class="model.EncodingCharacter"></jsp:useBean>
<jsp:setProperty property="*" name="email"/>
<div align="center">
标题:$encoding.toString(email.title)
<br>
<hr>
内容:$encoding.toString(email.content)
</div>
</body>
</html>
以上是关于JavaBean的主要内容,如果未能解决你的问题,请参考以下文章