JavaBean的编码规范:
1.必须具有一个公共的无参的构造方法,这个方法可以是编译器自动产生的默认构造方法
2.提供公共的setter方法和getter方法,让外部程序设置和获取javaBean的属性
举个例子:
Book
package cn.itcast.chapter07.javabean;
public class Book {
private double price;
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price=price;
}
}
JavaBean中的属性与成员变量不是同一个概念,通常以方法定义的形式出现
举个例子:
getName()、setName()
package cn.itcast.chapter07.javabean;
public class Student {
private String sid;
private String name;
private int age;
private boolean married;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age=age;
}
public boolean isMarried() {
return married;
}
public void setMarried(boolean married) {
this.married=married;
}
public String getSid() {
return sid;
}
public void setName(String name) {
this.name=name;
}
public void getInfo() {
System.out.print("瞅啥瞅!");
}
}
BeanUtils工具:http://commons.apache.org/proper/commons-beanutils
请自行下载
运用例子:
package cn.itcast.chapter07.beanutils;
public class Person {
private String name;
private int age;
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;
}
}
package cn.itcast.chapter07.beanutils;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
public class BeanUtilsDemo {
public static void main(String[] args) throws Exception{
Person p=new Person();
BeanUtils.setProperty(p, "name", "Jack");
BeanUtils.setProperty(p,"age",10);
String name=BeanUtils.getProperty(p,"name");
String age=BeanUtils.getProperty(p,"age");
System.out.println("我的名字叫" + name + "我今年" + age + "岁了!");
Map<String, Object>map=new HashMap<String, Object>();
map.put("name","张三");
map.put("age", 10);
BeanUtils.populate(p, map);
System.out.println("姓名:" + p.getName() + ",年龄" + p.getAge());
}
}
EL表达式语法:
${表达式}
举个例子:
package cn.itcast.chapter07.servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("username", "itcast");
request.setAttribute("password", "123");
RequestDispatcher dispatcher = request.getRequestDispatcher("/myjsp.jsp");
dispatcher.forward(request, response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
<%@ 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.getAttribute("username") %><br />
密码: <%=request.getAttribute("password") %><br />
<hr />
使用EL表达式:<br />
用户名: ${username}<br />
密码: ${password}<br />
</body>
</html>
EL隐式对象
pageContext对象
<%@ 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>
请求URI为: ${pageContext.request.requestURI}<br />
Content-Type响应头: ${pageContext.response.contentType}<br />
服务器信息为: ${pageContext.servletContext.serverInfo}<br />
Servlet注册名为: ${pageContext.servletConfig.servletName}<br />
</body>
</html>
scopes对象
<%@ 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>
<% pageContext.setAttribute("userName", "itcast"); %>
<% request.setAttribute("bookName","Java Web"); %>
<% session.setAttribute("userName", "itheima"); %>
<% application.setAttribute("bookName","Java 基础"); %>
表达式\${pageScope.userName}的值为:${pageScope.userName}<br />
表达式\${requestScope.bookName}的值为:${requestScope.bookName}<br />
表达式\${sessionScope.userName}的值为:${sessionScope.userName}<br />
表达式\${applicationScope.bookName}的值为:${applicationScope.bookName}<br />
表达式\${userName}的值为:${userName}
</body>
</html>
param对象
<%@ 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 style="text-align:center;">
<form action="${pageContext.request.contextPath}/param.jsp">
num1:<input type="text" name="num1"><br />
num2:<input type="text" name="num"><br />
num3:<input type="text" name="num"><br />
<input type="submit" value="提交" />
<input type="submit" value="重置" /><hr />
num1:${param.num1}<br />
num2:${paramValues.num[0]}<br />
num3:${paramValues.num[1]}<br />
</form>
</body>
</html>
Cookie对象
<%@ 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>
Cookie对象的信息:<br />
${cookie.userName }<br />
Cookie对象的名称和值:<br />
${cookie.userName.name }=${cookie.userName.value }
<% response.addCookie(new Cookie("userName","itcast")); %>
</body>
</html>