第7章 EL表达式

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="提交" />&nbsp;&nbsp;
<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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值