Servlet(五)——EL表达式

目录​​​​​​​

1.EL表达式介绍

2.EL表达式内置对象

3.EL表达式访问4种范围属性

4.EL表达式接受请求参数

5.EL表达式对象操作

6.EL表达式集合操作


1.EL表达式介绍

EL表达式

EL(Expression Language) 是为了使JSP写起来更加简单。

 

2.EL表达式内置对象

 

3.EL表达式访问4种范围属性

寻找值的顺序:page->request->session->application

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
	pageContext.setAttribute("info1", "page范围");
	request.setAttribute("info1", "request范围");
	session.setAttribute("info1", "session范围");
	application.setAttribute("info1", "application范围");
%>
<!-- 最后取的是page范围的值 -->
<h1>${info1 }</h1>
</body>
</html>

 运行结果:

 

4.EL表达式接受请求参数

Param:单个参数 paramValues:一组参数

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<!-- param一个参数传值 -->
<form action="el2.jsp" method="post">
	<input type="text" name="name"/>
	<input type="submit" name="提交el2.jsp"/>
</form>
<!-- paramValues一组参数传值 -->
<form action="el2.jsp" method="post">
	<input type="checkbox" name=hobby value="C语言"/>C语言
	<input type="checkbox" name=hobby value="Java语言"/>Java语言
	<input type="checkbox" name=hobby value="C++语言"/>C++语言
	<input type="submit" name="提交el2.jsp"/>
</form>
</body>
</html>

el2.jsp代码:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<!-- 设置默认编码 -->
<%
	request.setCharacterEncoding("utf-8");
%>
<h1>姓名:${param.name }</h1>
<h1>爱好1:${paramValues.hobby[0] }</h1>
<h1>爱好2:${paramValues.hobby[1] }</h1>
<h1>爱好3:${paramValues.hobby[2] }</h1>
</body>
</html>

运行结果:

 

5.EL表达式对象操作

首先建一个People类:

package com.java.model;

public class People {

	private int id;
	private String name;
	private int age;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	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;
	}
	
}
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@page import="com.java.model.People" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
	People p=new People();
	p.setId(5);
	p.setName("亚瑟");
	p.setAge(20);
	request.setAttribute("p", p);
%>
<h1>姓名:${p.name }</h1>
<h1>编号:${p.id }</h1>
<h1>年龄:${p.age }</h1>
</body>
</html>

运行结果:

 

 

6.EL表达式集合操作

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
	List all=new LinkedList();
	all.add(0,"元素1");
	all.add(1,"元素2");
	all.add(2,"元素3");
	request.setAttribute("all", all);
%>
<h1>${all[0] }</h1>
<h1>${all[1] }</h1>
<h1>${all[2] }</h1>
</body>
</html>

运行结果:

 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值