JSP中 EL&JSTL

【JSP的脚本】
Ø <%! %> :翻译成Servlet中的成员内容. 定义变量,方法,类. -- 不建议.
Ø <% %> :翻译成Servlet中service方法内部的内容. 定义类,变量
Ø <%= %> :翻译成Servlet中service方法中out.print();

  • 设置全局的错误友好页面:
    * 在web.xml中设置:

    404
    /404.jsp


    500
    /500.jsp

Ø pageContext内置对象 :
* 获得其他的8个内置对象 :编写通用性代码或者框架的时候.

JSP的四个域范围:
* PageScope :当前页面中有效. pageContext PageContext
* RequestScope :一次请求范围. request HttpServletRequest
* SessionScope :一次会话范围. session HttpSession
* ApplicationScope :应用范围 application ServletContext

EL 表达式
Ø 使用EL表达式:
* 语法:${ EL表达式 }
【EL操作WEB开发的常用对象11个】

EL功能三:操作WEB开发常用的对象



接收请求的参数


<%= request.getParameter("id") %>
<%= request.getParameter("name") %>
<%= Arrays.toString(request.getParameterValues("hobby")) %>



${ param.id }
${ param.name }
${ paramValues.hobby[0] }
${ paramValues.hobby[1] }

获取请求头


<%= request.getHeader("User-Agent") %>


${ header["User-Agent"] }

获取全局初始化参数


${ initParam.username }

获取Cookie中的值


${ cookie.history.value }

获取PageContext中的对象


IP地址:${ pageContext.request.remoteAddr }
工程路径:${ pageContext.request.contextPath }

【EL执行运算】

EL的功能二:执行运算


EL执行算数运算


<%
pageContext.setAttribute("n1", "10");
pageContext.setAttribute("n2", "20");
pageContext.setAttribute("n3", "30");
pageContext.setAttribute("n4", "40");
%>
${ n1 + n2 + n3 }

EL执行逻辑运算


${ n1 < n2 } - ${ n1 lt n2 }

${ n1 > n2 } - ${ n1 gt n2 }

${ n1 <= n2 } - ${ n1 le n2 }

${ n1 >= n2 } - ${ n1 ge n2 }

${ n1 == n2 } - ${ n1 eq n2 }

EL执行关系运算

${ n1 ${ n1 ${ !(n1 < n2) } - ${ not(n1EL执行三元运算 ${ n1 < n2 ? "正确":"错误" }

empty运算

${ user == null } - ${ empty user } ${ user != null } - ${ not empty user }
JSP中 EL&JSTL_第1张图片
image.png

 JSTL的标签库:包含了五类标签.

  • core(核心标签),fmt(国际化标签),xml(XML标签),sql(SQL标签),fn(JSTL提供EL函数库)
     使用JSTL:
  • 引入JSTL的相关的jar包.
  • 在页面中引入标签库.<%@ taglib uri=”” prefix=””%>
    【JSTL的核心标签的用法】
  • if
  • forEach
    【JSTL的提供EL的函数库】

    JSTL提供的EL的函数库


    ${ fn:contains("Hello World","Hello") }
    ${ fn:length("HelloWorld") }
    ${ fn:toLowerCase("ABCDE") }

    ${ i }

你可能感兴趣的:(JSP中 EL&JSTL)