一、
JSP EL
的运算符
类型
|
定义
|
算术型
|
+ - *
/ div %
mod
|
逻辑型
|
and
&& or || not
!
|
关系型
|
==
eq != ne > gt < lt >= ge
<= le
|
条件型
|
a?b:c
|
空
|
empty
|
二、 JSP EL 的基本用法
类型
|
实例
|
基本调用方法
|
JavaBeans
|
${user.username}
${user["username"]}
${user['username']}
|
user.getUsername()
|
数组
|
${sport[1]}
${sport["1"]}
${sport['1']}
|
sport[1]
|
List
|
${phone[2]}
${phone["2"]}
${phone['2']}
|
phone.get(2)
|
Map
|
${phone.home}
${phone["home"]}
${phone['home']}
|
phone.get("home")
|
三、 JSP EL 的内容对象
pageContext 当前页面上下文件对象
pageScope page对象
requestScope request对象
sessionScope session对象
applicationScope application对象
param 得到页面传来的参数
paramValues 得到页面传来的多个参数,返回一个数组
header 获取头信息
headerValues 获取头信息的值
cookie 获取cookie对象的值
initParam 获取设定初始的参数值
pageScope page对象
requestScope request对象
sessionScope session对象
applicationScope application对象
param 得到页面传来的参数
paramValues 得到页面传来的多个参数,返回一个数组
header 获取头信息
headerValues 获取头信息的值
cookie 获取cookie对象的值
initParam 获取设定初始的参数值
例:
<
%=session.getAttribute(
"phone")%>
等价于${sessionScope.phone}
等价于${sessionScope.phone}
四、如何设置 JSP 不使用 JSP EL
1
、当前页面不要用
JSP
EL
<
%@page
isELIgnored
="true"
%>
2
、整个
web
应用都不使用
EL
,修改
web.xml
文件
<
web-app...
>
< jsp-config >
< jsp-property-group >
< url-pattern > *.jsp</ url-pattern >
< el-ignored > true</ el-ignored >
</ jsp-property-group >
</ jsp-config >
</ web-app... >
< jsp-config >
< jsp-property-group >
< url-pattern > *.jsp</ url-pattern >
< el-ignored > true</ el-ignored >
</ jsp-property-group >
</ jsp-config >
</ web-app... >
五、实例
1
、基本运算符的实例
<
%@
page language
="java"
pageEncoding
="UTF-8"
%>
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html >
< head >
< title > My JSP 'elExample1.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 >
< !-- 以下为JSP EL的算术运算实例 -- >
${10+10 }< br >
${10-10 }< br >
${10*10 }< br >
${10/10 }< br >
${10 div 10 }< br >
${10%10 }< br >
${10 mod 10 }< br >
< !-- 以下为想输入原样的表达式,需要用\或者'进行转义 -- >
\${10+10 }< br >
'$'{10+10 }< br >
< !-- 以下为JSP EL的关系运算实例 -- >
${100>200 }< br >
${100 gt 200 }< br >
${100< 200 }<br>
${100 lt 200 }< br >
${100>=200 }< br >
${100 ge 200 }< br >
${100< =200 }<br>
${100 le 200 }< br >
${100==200 }< br >
${100 eq 200 }< br >
${100 !=200 }< br >
${100 ne 200 }< br >
< !-- 以下为比较字符,字符用单引号,字符串用双引号引起 -- >
${'e' eq 'h' }< br >
${"hit" > "him" }< br >
< !-- 以下为逻辑运算符的实例 -- >
${(10>2) && (34>25) }< br >
${(10>2) and (34>25) }< br >
${(10>2) || (34>25) }< br >
${(10>2) or (34>25) }< br >
${!(10>2)}< br >
${not(10>2)}< br >
< !-- empty运算符的应用 empty判断时,若对象为""或是null,则都为true-- >
<%
pageContext.setAttribute("username",null);
pageContext.setAttribute("password","");
pageContext.setAttribute("city","北京");
pageContext.setAttribute("date",new java.util.Date());
%>
< !-- 判断username变量是否为空,以下返回true-- >
${empty username }< br >
< !-- 判断password变量是否为空,以下返回true -- >
${empty password }< br >
< !-- 判断city变量是否为空,以下返回false-- >
${empty city }< br >
< !-- 判断date变量是否为空,以下返回false -- >
${empty date }< br >
</ body >
</ html >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html >
< head >
< title > My JSP 'elExample1.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 >
< !-- 以下为JSP EL的算术运算实例 -- >
${10+10 }< br >
${10-10 }< br >
${10*10 }< br >
${10/10 }< br >
${10 div 10 }< br >
${10%10 }< br >
${10 mod 10 }< br >
< !-- 以下为想输入原样的表达式,需要用\或者'进行转义 -- >
\${10+10 }< br >
'$'{10+10 }< br >
< !-- 以下为JSP EL的关系运算实例 -- >
${100>200 }< br >
${100 gt 200 }< br >
${100< 200 }<br>
${100 lt 200 }< br >
${100>=200 }< br >
${100 ge 200 }< br >
${100< =200 }<br>
${100 le 200 }< br >
${100==200 }< br >
${100 eq 200 }< br >
${100 !=200 }< br >
${100 ne 200 }< br >
< !-- 以下为比较字符,字符用单引号,字符串用双引号引起 -- >
${'e' eq 'h' }< br >
${"hit" > "him" }< br >
< !-- 以下为逻辑运算符的实例 -- >
${(10>2) && (34>25) }< br >
${(10>2) and (34>25) }< br >
${(10>2) || (34>25) }< br >
${(10>2) or (34>25) }< br >
${!(10>2)}< br >
${not(10>2)}< br >
< !-- empty运算符的应用 empty判断时,若对象为""或是null,则都为true-- >
<%
pageContext.setAttribute("username",null);
pageContext.setAttribute("password","");
pageContext.setAttribute("city","北京");
pageContext.setAttribute("date",new java.util.Date());
%>
< !-- 判断username变量是否为空,以下返回true-- >
${empty username }< br >
< !-- 判断password变量是否为空,以下返回true -- >
${empty password }< br >
< !-- 判断city变量是否为空,以下返回false-- >
${empty city }< br >
< !-- 判断date变量是否为空,以下返回false -- >
${empty date }< br >
</ body >
</ html >
2
、用
JSP EL
读取
JavaBean
中的值
<
%@
page language
="java"
pageEncoding
="UTF-8"
%>
< %@ page import ="java.util.*,com.meixin.beans.*" %>
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html >
< head >
< title > My JSP 'elExample1.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 >
< !-- 使用User Bean,设置属性值username -- >
< jsp:useBean id ="user" class ="com.meixin.beans.User" > </ jsp:useBean >
< jsp:setProperty name ="user" property ="username" value ="meixin" />
<%
//建立Profile对象,设置邮件地址
Profile p = new Profile();
p.setEmail("wnight88@sina.com");
//将不同的电话存入Map中,并设置在p对象的属性中
Map< String,String > phone = new HashMap< String,String > ();
phone.put("office","8383838");
p.setPhone(phone);
//建立地址对象,设置城市名
Address address = new Address();
address.setCity("北京");
Address[] addresses = {address};
p.setAddress(addresses);
user.setProfile(p);
%>
< !-- 用JSP EL的级连方式输入值 -- >
< !-- 输出user对象中的username属性值,三种写法等价 -- >
${user.username }< br >
${user["username"] }< br >
${user['username'] }< br >
< !-- 输出user对象中profile属性对象中的phone属性Map中键值为office的值 -- >
${user.profile.phone.office }< br >
${user['rofile']['phone']['office'] }< br >
< !-- 输出user对象中profile属性对象中address数据属性中第0个元素对象中的city的属性值 -- >
${user.profile.address[0].city }< br >
</ body >
</ html >
< %@ page import ="java.util.*,com.meixin.beans.*" %>
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html >
< head >
< title > My JSP 'elExample1.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 >
< !-- 使用User Bean,设置属性值username -- >
< jsp:useBean id ="user" class ="com.meixin.beans.User" > </ jsp:useBean >
< jsp:setProperty name ="user" property ="username" value ="meixin" />
<%
//建立Profile对象,设置邮件地址
Profile p = new Profile();
p.setEmail("wnight88@sina.com");
//将不同的电话存入Map中,并设置在p对象的属性中
Map< String,String > phone = new HashMap< String,String > ();
phone.put("office","8383838");
p.setPhone(phone);
//建立地址对象,设置城市名
Address address = new Address();
address.setCity("北京");
Address[] addresses = {address};
p.setAddress(addresses);
user.setProfile(p);
%>
< !-- 用JSP EL的级连方式输入值 -- >
< !-- 输出user对象中的username属性值,三种写法等价 -- >
${user.username }< br >
${user["username"] }< br >
${user['username'] }< br >
< !-- 输出user对象中profile属性对象中的phone属性Map中键值为office的值 -- >
${user.profile.phone.office }< br >
${user['rofile']['phone']['office'] }< br >
< !-- 输出user对象中profile属性对象中address数据属性中第0个元素对象中的city的属性值 -- >
${user.profile.address[0].city }< br >
</ body >
</ html >
以下为对象的JavaBean的内容
1)
Profile类
package
com.meixin.beans;
import java.util.Date;
import java.util.Map;
public class Profile
{
private String email;
private Date birthday;
private Address[] address;
private Map<String, String> phone;
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this .email = email;
}
public Date getBirthday()
{
return birthday;
}
public void setBirthday(Date birthday)
{
this .birthday = birthday;
}
public Address[] getAddress()
{
return address;
}
public void setAddress(Address[] address)
{
this .address = address;
}
public Map<String, String> getPhone()
{
return phone;
}
public void setPhone(Map<String, String> phone)
{
this .phone = phone;
}
}
import java.util.Date;
import java.util.Map;
public class Profile
{
private String email;
private Date birthday;
private Address[] address;
private Map<String, String> phone;
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this .email = email;
}
public Date getBirthday()
{
return birthday;
}
public void setBirthday(Date birthday)
{
this .birthday = birthday;
}
public Address[] getAddress()
{
return address;
}
public void setAddress(Address[] address)
{
this .address = address;
}
public Map<String, String> getPhone()
{
return phone;
}
public void setPhone(Map<String, String> phone)
{
this .phone = phone;
}
}
2)User类
package
com.meixin.beans;
public class User
{
private Long userID;
private String userName;
private String password;
private Profile profile;
public Long getUserID()
{
return userID;
}
public void setUserID(Long userID)
{
this .userID = userID;
}
public String getUserName()
{
return userName;
}
public void setUserName(String userName)
{
this .userName = userName;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this .password = password;
}
public Profile getProfile()
{
return profile;
}
public void setProfile(Profile profile)
{
this .profile = profile;
}
}
public class User
{
private Long userID;
private String userName;
private String password;
private Profile profile;
public Long getUserID()
{
return userID;
}
public void setUserID(Long userID)
{
this .userID = userID;
}
public String getUserName()
{
return userName;
}
public void setUserName(String userName)
{
this .userName = userName;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this .password = password;
}
public Profile getProfile()
{
return profile;
}
public void setProfile(Profile profile)
{
this .profile = profile;
}
}
3)Address
类
package
com.meixin.beans;
public class Address
{
private String city;
public String getCity()
{
return city;
}
public void setCity(String city)
{
this .city = city;
}
}
public class Address
{
private String city;
public String getCity()
{
return city;
}
public void setCity(String city)
{
this .city = city;
}
}
3
、
实例:输出页面不同范围内属性的值
<
%@
page language
="java"
pageEncoding
="UTF-8"
%>
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html >
< head >
< title > My JSP 'elExample1.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 >
<%
pageContext.setAttribute("username","meixin");
request.setAttribute("username","meixinRequest");
session.setAttribute("username","meixinSession");
application.setAttribute("username","meixinApplication");
%>
< !-- 输出meixin -- >
${pageScope.username }< br >
${pageScope['username'] }< br >
< !-- 输出值为meixinSession -- >
${sessionScope.username }< br >
< !-- 输出值为meixinRequest -- >
${requestScope.username }< br >
< !-- 输出值为meixinApplication -- >
${applicationScope.username }< br >
< !-- 输出值为meixin,此变量系统根据pageContext,request,session,application依次查找 -- >
${username }< br >
</ body >
</ html >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html >
< head >
< title > My JSP 'elExample1.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 >
<%
pageContext.setAttribute("username","meixin");
request.setAttribute("username","meixinRequest");
session.setAttribute("username","meixinSession");
application.setAttribute("username","meixinApplication");
%>
< !-- 输出meixin -- >
${pageScope.username }< br >
${pageScope['username'] }< br >
< !-- 输出值为meixinSession -- >
${sessionScope.username }< br >
< !-- 输出值为meixinRequest -- >
${requestScope.username }< br >
< !-- 输出值为meixinApplication -- >
${applicationScope.username }< br >
< !-- 输出值为meixin,此变量系统根据pageContext,request,session,application依次查找 -- >
${username }< br >
</ body >
</ html >
4 、实例:param 用 于获取上一页面传递的参数值
<
!-- param用于获取上一页面传递来的参数值--
>
${param.username}< br >
${param.password}< br >
${param.username}< br >
${param.password}< br >
5
、实例:
cookie
用于获取
cookie
参数的值
<%
response.addCookie(new Cookie("username","meixin"));
%>
< !-- 输出cookie中user的值,此处输出meixin -- >
${cookie.user.value }
response.addCookie(new Cookie("username","meixin"));
%>
< !-- 输出cookie中user的值,此处输出meixin -- >
${cookie.user.value }
6
、实例:</str
相关推荐
本笔记记录了jsp从入门到精通 , 纯手写 , 如何成为一个成功的web程序员?一个普通的错误是把JSP当作简化的 Java。它不是,(事实上, JSP 是简化的 servlet 。)程序员通常试着没有学习要求的支持技巧而 直接学习 JSP...
jsp学习笔记(java的web开发),还包括EL表达式 和JSTL。可快速入门,也可当手册使用。 更多:http://download.csdn.net/user/daillo/all
el表达式语言,jsp标签库,servlet,过滤器,监听器,基础语法,内置对象,站点配置,常见错误,学习总结
本笔记为我在达内科技学习时所作的笔记,内容涵盖了jsp的基础知识,如基础语法知识,内置对象的介绍及使用,el表达式,标准库等,是jsp 初学者的很好的参考资料
JSP JSTL、EL表达式 SSH 图形随机码 AJax笔记.txt
JSP四大域对象和JSTL、EL表达式
移动开发的小白树懒在学习web的技术,为以后的移动开发打下坚实的基石...
JSP学习笔记..................236 JSP前言.................236 第一章 Introduction to JSP...............238 第二章 Creating,Deploying and Executing a JSP..................239 第三章 JSP Scriplets,...
jsp 基础的学习笔记 第一章语法和el表达式语言的使用 JSP学习要点记录 jsp乱码解决大全 基础开发入门级:JSP与ASP的比较 用EL访问javabean pro jsp第三章第五节代码 .......
JSP学习笔记..................236 JSP前言.................236 第一章 Introduction to JSP...............238 第二章 Creating,Deploying and Executing a JSP..................239 第三章 JSP Scriplets,...
是否还在默默的看着学习路线图,寻找适合自己的学习资源,现在不需要如此麻烦了,动力节点全新整理Javaweb阶段全套教程已经上线,内涵网络通信、HTML、CSS、JavaScript、MySQL、JDBC、JSP、Servlet等技术,Javaweb从...
对JSP,JSTL,MVC和三层架构,EL的学习笔记
包含内容 01web.doc 02Servlet.doc 03request&response.doc... 06jspEL.doc 07自定义标签.doc 08注册案例.doc 09sql.doc 10jdbc.doc 11jdbc.doc 12jdbc.doc 13jdbc.doc 14过滤器.doc 15uploadListener.doc
NULL 博文链接:https://persistc.iteye.com/blog/264072
该笔记由本作者参加完达内科技四个月Java培训后亲自总结整理,涵盖所有Java基础知识,以及Web开发相关Html,Oracle数据库,JDBC,XML,JSP,JavaScript,Ajax,JQuery,JSON,EL,Struts,Hibernate,Spring等一系列...
Jsp语法. EL表达式语言. iBATIS. JDBC编程. JDBC驱动加载. Servlet.过滤器,监听器 struts相关笔记. 等等...... 里面是当年自学JAVA时的一些笔记,内容都是TXT格式的,都是一些基础性的概念总结。
哔哩哔哩上老杜的Servlet笔记。 2020-5-05 Servlet学习 Tomcat 服务器配置 2020-5-08 UML 2020-5-08 Servlet学习 ...2020-5-21 JavaWeb学习 JSP,EL,JSTL,Filter, 2020-5-23 MVC、三层设计 2020-5-24 数据分页展示
\contentsline {chapter}{Contents}{2}{section*.1} {1}Java基础}{17}{chapter.1} {1.1}基本语法}{17}{section.1.1} {1.2}数字表达方式}{17}{section.1.2} {1.3}补码}{19}{section.1.3} {1.3.1}总结}{23}{...
注意这里面需要使用 “” 把 El 表达式给括起来,否则就取不到数据。 JSP获取session的值: 可以直接${sessionScope.变量名},在标签里也是一样。 JSP获取URL的值: var name = "<%=request.getParameter(...
内容包括:MySQL、MySQL约束&多表、MySQL多表&事务、JDBC、数据库连接池、HTML、CSS、JS基础&高级、BootStrap、XML、Tomcat、Servlet&HTTP&Request、Response、Cookie&Session、JSP&EL&JSTL、Filter&Listener、...