jsp内置对象和el表达式

九个内置对象
*out jsp的输出流,用来向客户端响应
*page 当前jsp页面, 它的引用数据类型是Object,即真身中有如下代码 Object page=this;
*config 它对应真身中的ServletConfig对象
*pageContext 一个顶九个,这个重要
*request HttpServletRequest
*response HttpServletResponse
*exception Throwable
*session HttpSession
*application ServletContext

1、pageContext
*一个顶九个
*Servlet中有3大域,而JSP中有四大域,它就是最后一个域对象(域对象一般有setAttribute等)
ServletContext:整个应用程序
session:整个会话(一个会话中只能有一个用户)
request:一个请求链!
pageContext:一个jsp页面!这个域是在当前jsp页面和当前jsp页面中使用的标签之间的共享数据
>域对象
>代理其他域:pageContext.setAttribute("xxx","XXX",PageContext.SESSION_SCOPE);响应的getAttribute和remove
>获取其他8个内置对象
>全域查找:PageContext.findAttribute("xxx");从小到大,依次查找

el表达式
1.jsp内置的表达式语言!
*jsp2.0开始,不让再使用Java脚本,而是使用el表达式和动态标签来替代脚本!
*EL替代的是<%= ... %>,也就是说,el只能做输出!
2、el表达式读取四大域
*${xxx}全域查找名为xxx的属性,如果不存在,输出空字符串而不是null
*${pageScope.xxx}、${pageScope.xxx}、${requestScope.xxx}、${sessionScope.xxx}

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.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">
</head> <%
request.setAttribute("xxx","requestScope_xxx");
session.setAttribute("xxx","sessionScope_xxx");
pageContext.setAttribute("xxx","pageScope_xxx");
application.setAttribute("xxx","applicationScope_xxx");
%>
<%
/**
el可以实现全局查找功能。查找范围是从小到大.格式为:${属性名}
也可以指定域进行查找${xxxScope.XXX}
*/
%>
<body>
${xxx }<br/>
${pageScope.xxx }<br/>
${requestScope.xxx }<br/>
${sessionScope.xxx }<br/>
${applicationScope.xxx }<br/> </body>
</html>

3、JavaBean导航
3、EL刻意输出的东西都在11个内置对象中!其中10个是map类型,就是pageContext不是map类型
*我们已经学习了四个
*param:对应参数,它是一个Map,其中key是参数名,value是参数值,适用于单值的参数
*paramValues:对应参数,它是一个Map,其中key参数名,value是多个参数值,适用于多值的参数
*header:对应请求头,它是一个Map,其中key表示头名称,value是单个头值,适用于单值请求头
*headerValues:对应请求头,它是一个Map,其中key表示头名称,value是多个头值
*initParam:获取<context-param>内的参数
<context-param>
<param-name>xxx</param-name>
<param-value>XXX</param-value>
</context-param>
<context-param>
<param-name>yyy</param-name>
<param-value>YYY</param-value>
</context-param>
*cookie:Map<String,Cookie>类型,其中key是cookie的name,value是cookie对象
*pageContext:它是PageContext类型!${pageContext.request.contextPath}

el的javabean导航。两种方式得到map中的数据

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.itcast.domain.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP '01.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>
<%
Address add=new Address("北京","西三旗");
Employee em=new Employee("郭大侠",3000,add);
request.setAttribute("em", em);
%>
<body>
${requestScope.em }<br/>
${requestScope.em.address.street } <!-- 相当于em之后使用em.getAddress().getStreet() --><br/>
${em.show }<!-- 这里是得到javaBean对应属性的值而不是Java类的,需要自己理解 -->
</body>
</html>
package com.itcast.domain; public class Address {
private String city;
private String street;
@Override
public String toString() {
return "Address [city=" + city + ", street=" + street + "]";
}
public Address() {
super();
// TODO Auto-generated constructor stub
}
public Address(String city, String street) {
super();
this.city = city;
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
} }
package com.itcast.domain; public class Employee {
private String name;
private double salary;
private Address address;
@Override
public String toString() {
return "Employee [name=" + name + ", salary=" + salary + ", address="
+ address + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Employee(String name, double salary, Address address) {
super();
this.name = name;
this.salary = salary;
this.address = address;
}
public Employee() {
super();
// TODO Auto-generated constructor stub
}
public String getShow(){
return "haha";
} }

================
EL函数库(由JSTL提供的)
*导入标签库:<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
String toUpperCase(String input)
String toLowerCase(String input)
int indexOf(String input,String substring)
boolean contains(String input,String substring)
boolean containsIngnoreCase(String input,String substring)
boolean startsWith(String input,String substring)
boolean endsWith(String input,String substring)
String substring(String input,int beginIndex,int endIndex)
String substringAfter(String input,String substring)
String substringBefore(String input,String substring)
String escapeXml(String input) :将input中“<”、“>”、“'”、“"”等进行转义,可以防止一部分javascript攻击
String trim(String input)
String replace(String input,String substringBefore,String substringAfter)
String[] split(String input,String delimiters)
int length(Object obj):可以获取字符串、数组、各种集合的长度
String join(String array[],String separator)

自定义el函数库
*写一个Java;类,类中可以定义0~N个方法,但必须是static,有返回值的
*在WEB-INF目录下面创建一个tld文件
*在jsp页面中导入标签

package com.itcast.fn;

public class MyFunction {
//该方法必须是静态方法
public static String fun(){
return "传智播客我的第一个自定义标签库";
}
}
<?xml version="1.0" encoding="UTF-8" ?> <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0"> <description>el自定义标签库</description>
<display-name>JSTL functions</display-name>
<tlib-version>1.0</tlib-version>
<short-name>my-function</short-name>
<uri>http://guodaxia.com/itcast/el/functions</uri> <function>
<name>fun</name>
<function-class>com.itcast.fn.MyFunction</function-class>
<function-signature>String fun()</function-signature>
</function> </taglib>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="it" uri="/WEB-INF/tlds/itcast.tld" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'a.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>
<h1>${it:fun()}</h1>
</body>
</html>
上一篇:自己编写的sublime text 3 插件


下一篇:Java 理论与实践: 处理 InterruptedException(转)