JSP 表达式语言:JSP EL

JSP 表达式语言

JSP表达式语言(EL)使得访问存储在JavaBean中的数据变得非常简单。JSP EL既可以用来创建算术表达式也可以用来创建逻辑表达式。在JSP EL表达式内可以使用整型数,浮点数,字符串,常量truefalse,还有null

 

JSP EL允许您指定一个表达式来表示属性值。一个简单的表达式语法如下:

 

${expr}

 

 

其中,expr指的是表达式。在JSP EL中通用的操作符是 . {} 。这两个操作符允许您通过内嵌的JSP对象访问各种各样的JavaBean属性。

 

 

EL中的基础操作符

EL表达式支持大部分Java所提供的算术和逻辑操作符:

 

操作符 描述

. 访问一个Bean属性或者一个映射条目

[] 访问一个数组或者链表的元素

( ) 组织一个子表达式以改变优先级

+ 加

- 减或负

* 乘

/ or div 除

% or mod 取模

== or eq 测试是否相等

!= or ne 测试是否不等

< or lt 测试是否小于

> or gt 测试是否大于

<= or le 测试是否小于等于

>= or ge 测试是否大于等于

&& or and 测试逻辑与

|| or or 测试逻辑或

! or not 测试取反

empty 测试是否空值

 

 

JSP EL隐含对象

JSP EL支持下表列出的隐含对象:

 

隐含对象 描述

pageScope page 作用域

requestScope request 作用域

sessionScope session 作用域

applicationScope application 作用域

param Request 对象的参数,字符串

paramValues Request对象的参数,字符串集合

header HTTP 信息头,字符串

headerValues HTTP 信息头,字符串集合

initParam 上下文初始化参数

cookie Cookie

pageContext 当前页面的pageContext

 

 

您可以在表达式中使用这些对象,就像使用变量一样。

 

 

pageContext对象

pageContext对象是JSPpageContext对象的引用。通过pageContext对象,您可以访问request对象。比如,访问request对象传入的查询字符串,就像这样:

 

${pageContext.request.queryString}

 

 

Scope对象

pageScoperequestScopesessionScopeapplicationScope变量用来访问存储在各个作用域层次的变量。

 

举例来说,如果您需要显式访问在applicationScope层的box变量,可以这样来访问:applicationScope.box

 

paramparamValues对象

paramparamValues对象用来访问参数值,通过使用request.getParameter方法和request.getParameterValues方法。

 

举例来说,访问一个名为order的参数,可以这样使用表达式:${param.order},或者${param["order"]}

 

接下来的例子表明了如何访问request中的username参数:

 

<%@ page import="java.io.*,java.util.*" %>

<%

    String title = "Accessing Request Param";

%>

<html>

<head>

<title><% out.print(title); %></title>

</head>

<body>

<center>

<h1><% out.print(title); %></h1>

</center>

<div align="center">

<p>${param["username"]}</p>

</div>

</body>

</html>

 

 

param对象返回单一的字符串,而paramValues对象则返回一个字符串数组。

 

headerheaderValues对象

headerheaderValues对象用来访问信息头,通过使用 request.getHeader方法和request.getHeaders方法。

 

举例来说,要访问一个名为user-agent的信息头,可以这样使用表达式:${header.user-agent},或者${header["user-agent"]}

 

接下来的例子表明了如何访问user-agent信息头:

 

<%@ page import="java.io.*,java.util.*" %>

<%

    String title = "User Agent Example";

%>

<html>

<head>

<title><% out.print(title); %></title>

</head>

<body>

<center>

<h1><% out.print(title); %></h1>

</center>

<div align="center">

<p>${header["user-agent"]}</p>

</div>

</body>

</html>

 

 

 

 

优点:方便快捷的使用隐式对象里面的属性。

缺点:不能代替流程控制,例如for循环/while/switch...,没有代码补全,找BUG不好找。

 

 

 

 

 

package cn.sxt;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

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



/**
 * Servlet implementation class lianxi2
 */
@WebServlet("/lianxi2")
public class lianxi2 extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public lianxi2() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    request.setAttribute("name", "hgehfj");
    request.setAttribute("type", "ewhufefu");
        HttpSession session = request.getSession();
        session.setAttribute("id", session.getId());
        ServletContext servletContext = this.getServletContext();
        if(servletContext.getAttribute("visited")==null){
            servletContext.setAttribute("visited", 1);
        }else{
            
            int i =(int) servletContext.getAttribute("visited")+1;
            servletContext.setAttribute("visited", i);
        }
        request.getRequestDispatcher("File1.jsp").forward(request, response);
    }
    

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

 


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <!--下面两个作用一样--> <!--<h><%=request.getAttribute("name") %></h>--> <h1>${name }</h1> <h1>${type }</h1> <!--下面两个作用一样--> <h1>session的iD:<%=session.getId() %></h1> <p>${id}</p> <h1>${visited }</h1> <%for(int i=0;i<10;i++){ %> <!--http://localhost:8080/hello4/lianxi2?page=11111 ?问号后面的路径--> <h1>获取用户查询的路径字符串:${pageContext.request.queryString}</h1> <%} %> <!--http://localhost:8080/hello4/lianxi2?page=11111 =后面的参数--> <h1>获取页面的参数:${param.page} </h1> <h1>获取页面的参数:${param["page"]} </h1> <!--http://localhost:8080/hello4/lianxi2?page-id=11111 =后面的参数--> <h1>获取页面的参数:${param["page-id"]} </h1> <!-- 获取浏览器的信息 --> <h1>${header["user-agent"] }</h1> </body> </html>

 

posted @ 2019-06-15 20:30  茫茫林海  阅读(443)  评论(0编辑  收藏  举报