1.编写一个UserJsp.jsp页面向用户显示姓名,页面使用useBean标准动作。要求同时使用setProperty动作将用户姓名设置为anne。getProperty动作用于获取anne的名字。
package com.ch5.bean;
public class User {
private String userName;
private String address;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<jsp:useBean id="user" class="com.ch5.bean.User"></jsp:useBean>
<jsp:setProperty property="userName" name="user" value="anne"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.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>
UserName:<jsp:getProperty name="user" property="userName"/><br>
</body>
</html>
2.创建一个JavaBean,用来接受汽车的颜色,以及表示汽车是否安装了空调的布尔值。如果布尔值为真,则汽车安装了空调;如果布尔值为假,则汽车未安装空调。该JavaBean返回颜色和布尔值(完成JavaBean,创建一个页面显示结果)。
package com.ch5.bean;
public class Car {
String color;
boolean airConditioner;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isAirConditioner() {
return airConditioner;
}
public void setAirConditioner(boolean airConditioner) {
this.airConditioner = airConditioner;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="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 'CarJsp.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>
<form action="CarResult.jsp" method="post">
颜色:<input name="color" type="text"/><br/>
是否安装空调:<input name="airConditioner" type="checkbox"/><br/>
<input value="submit" type="submit"/>
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
request.setCharacterEncoding("UTF-8");
%>
<jsp:useBean id="car" class="com.ch5.bean.Car"></jsp:useBean>
<jsp:setProperty property="*" name="car"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'CarJsp.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>
<form action="CarResult.jsp">
颜色:<jsp:getProperty property="color" name="car"/><br/>
是否安装空调::<jsp:getProperty property="airConditioner" name="car"/>
</form>
</body>
</html>