实验三JavaBean技术
一、实验目的:
1、掌握JavaBean创建和使用方法;
2、掌握 Javabean 的编写规则
3、体验 JSP+JavaBean 编程的好处
二、实验原理:
JavaBean 组件是一些可移植、可重用,并可以组装到应用程序中的 Java 类。
将JavaBean与 JSP 语言元素一起使用,可以很好地实现后台业务逻辑和前台表示逻辑的分离,使得JSP页面更加可读、易维护。
三、实验内容:
1、用 JavaBean 实现统计用户访问网站的次数。
package bean;
public class Counter {
private int count;
public int getCount() {
count++;
return count;
}
public void setCount(int count) {
this.count = count;
}
public Counter(){
count=0;
}
}
Count.jsp:
<%--
Created by IntelliJ IDEA.
User: lenovo
Date: 2019/11/25
Time: 23:20
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<jsp:useBean id="counter" scope="application" class="bean.Counter"/>
网页被访问的次数为:
<jsp:getProperty name="counter" property="count"/>
</body>
</html>
2、设计诸如以下页面的简单计算器 (加减乘除),将加减乘除部分写在JavaBean中,在JSP页面中调用显示计算结果。要求:主页面JSP中,设置供客户端输入的文本框。并显示最终计算结果;当除数为零时提示报错。
CalculatorBean .java
package edu.xalead.bean;
import java.math.BigDecimal;
public class CalculatorBean {
private int n1;
private int n2;
private char operator;
private String result;
public int getN1() {
return n1;
}
public void setN1(int n1) {
this.n1 = n1;
}
public int getN2() {
return n2;
}
public void setN2(int n2) {
this.n2 = n2;
}
public char getOperator() {
return operator;
}
public void setOperator(char operator) {
this.operator = operator;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public void calculation(){
BigDecimal n1 = new BigDecimal(this.n1);
BigDecimal n2 = new BigDecimal(this.n2);
switch(this.operator){
case '+':{
this.result = n1.add(n2).toString();
break;
}
case '-':{
this.result = n1.subtract(n2).toString();
break;
}
case '*':{
this.result = n1.multiply(n2).toString();
break;
}
case '/':{
if(n2.intValue()==0){
throw new RuntimeException("被除数不能为0!");
}
/**
* 第一个参数是除数,
* 第二个参数代表保留几位小数,
* 第三个代表的是使用的模式
*BigDecimal.ROUND_UP:直接进位,比如1.21如果保留1位小数,得到的就是1.3
*
*/
this.result = n1.divide(n2,5,BigDecimal.ROUND_HALF_UP).toString();
break;
}
default:
throw new RuntimeException("运算符只能是:+ 、-、*、/");
}
}
}
<%--
Created by IntelliJ IDEA.
User: lenovo
Date: 2019/11/26
Time: 11:02
To change this template use File | Settings | File Templates.
--%>
Calculation.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="showCalculation.jsp" method="post" >
<table width="40%" border="1">
<tr>
<td colspan="2">简单的计算器</td>
</tr>
<tr>
<td>第一个参数</td>
<td>
<input type="text" name="n1">
</td>
</tr>
<tr>
<td>操作符</td>
<td>
<select name="operator">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
</td>
</tr>
<tr>
<td>第二个参数</td>
<td>
<input type="text" name="n2">
</td>
</tr>
<tr>
<td>
<input type="submit" value="提交计算的数值">
</td>
</tr>
</table>
</form>
</body>
</html>
showCalculation.jsp:
<%--
Created by IntelliJ IDEA.
User: lenovo
Date: 2019/11/26
Time: 11:14
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body style="text-align: center" bgcolor="aqua" border="1">
<table style="text-align: center" bgcolor="aqua" border="1">
<jsp:useBean id="calculationBean" class="edu.xalead.bean.CalculatorBean"></jsp:useBean>
<jsp:setProperty name="calculationBean" property="*"></jsp:setProperty>
<%
try{
calculationBean.calculation();
}catch (Exception e){
out.write(e.getMessage());
}
%>
<hr>
计算结果是:
<jsp:getProperty name="calculationBean" property="n1"/>
<jsp:getProperty name="calculationBean" property="operator"/>
<jsp:getProperty name="calculationBean" property="n2"/>
=
<jsp:getProperty name="calculationBean" property="result"/>
<hr>
</table>
</body>
</html>
3.设计一个注册页面 register.jsp,用户填写的信息包括:姓名、性别、 出生年月、 民族、个人介绍等,用户点击注册后将注册信息通过 output.jsp 显示出来。要求编写一个 JavaBean,封装用户填写的注册信息。
User代码:
package edu.xalead.webtest;
public class User {
private String name;
private String email;
private String sex;
private String language;
private String local;
private String introducation;
public User(String name, String email, String sex, String language, String local, String introducation) {
this.name = name;
this.email = email;
this.sex = sex;
this.language = language;
this.local = local;
this.introducation = introducation;
}
public User(){};
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getLocal() {
return local;
}
public void setLocal(String local) {
this.local = local;
}
public String getIntroducation() {
return introducation;
}
public void setIntroducation(String introducation) {
this.introducation = introducation;
}
}
Rejestor.jsp:
<%--
Created by IntelliJ IDEA.
User: lenovo
Date: 2019/11/26
Time: 11:47
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="output.jsp" method="post">
<table align="center">
<tr>
<td align="center" colspan="2">
<h2>用户注册</h2>
<hr>
</td>
</tr>
<tr>
<td align="right">用户名:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td align="right">Email:</td>
<td><input type="text" name="email"/></td>
</tr>
<tr>
<td align="right">性别:</td>
<td><input type="text" name="sex"/></td>
</tr>
<tr>
<td align="right">外语:</td>
<td><input type="text" name="language"/></td>
</tr>
<tr>
<td align="right">地区:</td>
<td><input type="text" name="local"/></td>
</tr>
<tr>
<td align="right">自我介绍:</td>
<td><input type="text" name="introducation"/></td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" value="提交"/>
<input type="reset" value="重置"/>
</td>
</tr>
</table>
</form>
</body>
</html>
Output.jsp:
<%--
Created by IntelliJ IDEA.
User: lenovo
Date: 2019/11/26
Time: 11:58
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<%request.setCharacterEncoding("utf-8");%>
</head>
<body>
<jsp:useBean id="user" class="edu.xalead.webtest.User" scope="page"/>
<jsp:setProperty name="user" property="name" param="name"/>
<jsp:setProperty name="user" property="email" param="email"/>
<jsp:setProperty name="user" property="sex" param="sex"/>
<jsp:setProperty name="user" property="language" param="language"/>
<jsp:setProperty name="user" property="local" param="local"/>
<jsp:setProperty name="user" property="introducation" param="introducation"/>
<table>
<tr>
<td>姓名</td>
<td><jsp:getProperty name="user" property="name"/></td>
</tr>
<tr>
<td>Email</td>
<td><jsp:getProperty name="user" property="email"/></td>
</tr>
<tr>
<td>性别</td>
<td><jsp:getProperty name="user" property="sex"/></td>
</tr>
<tr>
<td>外语</td>
<td><jsp:getProperty name="user" property="language"/></td>
</tr>
<tr>
<td>地区</td>
<td><jsp:getProperty name="user" property="local"/></td>
</tr>
<tr>
<td>自我介绍</td>
<td><jsp:getProperty name="user" property="introducation"/></td>
</tr>
</table>
</body>
</html>
4.购物车(用javabean实现)。
四、实验要求:
1、认真执行每一个步骤,并作好记录。
2、将实验成果简要记录下来,形成实验报告书,内容包括:
实验目的;
实验内容(简要记录) ;
实验中所出现的问题及解决方案;(重点)
实验心得(重点)
五、实验学时:2学时+2学时