EL表达式

jsp中,为了获取servlet域对象中存储的数据,经常需要书写很多Java代码,这样的做法会使得jsp页面混乱,难以维护。为此jsp2.0规范中提供了EL表达式(Expression Language),表达式语言是一种简化的数据访问方式。语法格式为${expression}

  基本算法:参考Tomact服务器jsp示例Basic Arithmetic,Basic Comparisons

  EL表达式只能获取存在4个作用域中的数据。为jsp中获取数据的一种规范

  ${u}原理:pageContext.findAttribute("u");从四个作用域一次查找,知道找到为止

  ${u.attr1.attr2}属性导航

package com.java12.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/el")
public class ElServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ElServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("username", "tom");
		request.setAttribute("password", "12313");
		request.getRequestDispatcher("/el.jsp").forward(request, response);
	}
	protected 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 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>

	用户名:${username }
	密码:${password }
</body>
</html>

表达式语言的内置对象(el隐式对象)

pageContext:代表该页面的pageContext对象,与jsp的pageContext内置对象相同

pageScope:用于获取page范围的属性值

requestScope:用于获取request范围的属性值

sessionScope:用于获取session范围的属性值

applicationScope:用于获取application范围的属性值

param:用于获取请求的参数值http://www.cnblogs.com/coolhwm/archive/2011/11/26/2264598.html

paramValues:用于获取请求的参数值,与header的区别在于,该对象用于获取属性值为数组的属性值

header:用于获取请求头的属性值

headerValues:用于获取请求头的属性值,与header的区别在于,该对象用于获取属性值为数组的属性值

initParam:用于获取请求web应用的初始化参数

cookie:用于获取指定的cookie值

pageContext使用示例:

<%@ 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>
请求的UI为:${pageContext.request.requestURI }
Content-Type响应头:${pageContext.response.contentType }
服务器信息为:${pageContext.servletContext.serverInfo }
Servlet注册名为:${pageContext.servletConfig.servletName }
	用户名:${username }
	密码:${password }
</body>
</html>

web域相关对象实验

attribute.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>

<%
	pageContext.setAttribute("pageContextAttr", "pageContextValue");
	request.setAttribute("requestAttr", "requestValue");
	session.setAttribute("sessionAttr", "sessionValue");
	application.setAttribute("applicationAttr", "applicationValue");
%>
pageContextAttr:<%= pageContext.getAttribute("pageContextAttr") %> <br>
requestAttr:<%= request.getAttribute("requestAttr") %> <br>
sessionAttr:<%= session.getAttribute("sessionAttr") %> <br>
applicationAttr:<%= application.getAttribute("applicationAttr") %> <br>

<a href="attribute2.jsp">attribute2.jsp</a> 

<a href="testAttrServlet">testAttrServlet</a>


</body>
</html>

attribute.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>
pageContextAttr:<%= pageContext.getAttribute("pageContextAttr") %> <br>
requestAttr:<%= request.getAttribute("requestAttr") %> <br>
sessionAttr:<%= session.getAttribute("sessionAttr") %> <br>
applicationAttr:<%= application.getAttribute("applicationAttr") %> <br>

</body>
</html>

TestServlet

package com.java12.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/testAttrServlet")
public class TestAttrServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public TestAttrServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out = response.getWriter();
		
		Object requestAttr = request.getAttribute("requestAttr");
		out.print("<br>");
		out.println("requestAttr:"+requestAttr);
		out.print("<br>");
		
		Object sessionAttr = request.getSession().getAttribute("sessionAttr");
		out.print("<br>");
		out.println("sessionAttr:"+sessionAttr);
		out.print("<br>");
		
		Object applicationAttr = getServletContext().getAttribute("applicationAttr");
		out.print("<br>");
		out.println("applicationAttr:"+applicationAttr);
		out.print("<br>");
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值