EL表达式和JSTL(EL表达式)

javaBean是java开发语言中一个可以重复使用的软件组件。它必须有一个公共的、无参的构造方法这个方法可以是编译器自动产生的默认构造方法;它提供公共的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;
	}
}

建立一个Student类:

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 viid 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工具是Apache软件基金会提供的一套简单、易用的API。
在已建立的lib目录下添加下载的commons-beanutils-1.9.2.jar和Logging的JAR包commons-logging-1.2.jar,并在src目录下建立Person类:

package cn.itcast.chapter07.beanutils;
public class Person{
	private String name;
	private int age;
	private String getName(){
		return age;
	}
	private void setName(String name){
		this.name = name;
	}
	public int getAge(){
		return age;
	}
	public void setAge(int age){
		this.age = age;
	}
}

建立一个BeanUtilsDemo类:

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是Expression Language的缩写,它是一种简单的数据访问语言。
创建一个MyServlet类:

package cn.itcast.chapter07.servlet;
import java.io.*;
import java.servlet.*;
import java.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 viod doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		doGet(request, response);
	}
}

在WebContent目录下编写一个myjsp.jsp文件:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<html>
<head></head>
<body>
	用户名:<% =request.getAttribute("username") %></br>
	密码:<% =request.getAttribute("password") %></br>
	<hr />
	使用EL表达式:<br />
	用户名:${username}<br />
	密码:${password}<br />
</body>
</html>

pageContext隐式对象(创建一个pageContext.jsp):

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<html>
<head></head>
<body>
	请求URI为:${pageContext.request.requestURI}<br />
	Content-Type响应头:${pageContext.response.contentType}<br />
	服务器信息为:${pageContext.servletContext.serverInfo}<>br />
	Servlet注册名为:${pageContext.servletConfig.servletName}<br />
</body>
</html>

param和paramValues对象(创建一个param.jsp):

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<html>
<head></head>
<body style="text-align: center;">
	<from 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 />
		<imput type="submit" value="提交" /><br />
		<imput type="submit" value="重置" /><br />
		num1:<param.num1><br />
		num2:<paramValues.num[0]><br />
		num3:<paramValues.num[1]><br />
	</form>
</body>
</html>

Cookie对象(创建一个cookie.jsp):

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<html>
<head></head>
<body>
	Cookie对象的信息:<br />
	${coolie.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、付费专栏及课程。

余额充值
>