JSP---EL---JSTL

JSP的引入:

  在Servlet中,能够直接获取后台服务器中的数据:获取数据方便 通过拼接字符串的形式,给浏览器响应html:操作麻烦,效率太低。 

如果能够有一个既能直接书写html代码,又能书写Servlet代码的页面。就能够在客户端和服务端直接进行数据交互了。所以引入了JSP。

jsp的三个指令

  1. <%@ 指令%>
  2. <%@ include file=”other02.jsp”%>
    • ==把另外一个页面other02.jsp的所有内容拿过来一起输出。 所有的标签元素都包含进来==
  3. <%@ taglib prefix=”” uri=”“%>
    • ==uri: 标签库路径
      prefix : 标签库的别名==

jsp的动态标签

jsp代码

<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>测试JSP</title>
</head>
<body>
<%--
    获取当前日期,并格式化成字符串。将其存储在request域中
--%>
<%
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");
    String curTime = sdf.format(date);
    request.setAttribute("curTime",curTime);
%>

<%--
    在页面上打印输出当前时间
--%>
当前系统时间:<span style="color: red"><%=request.getAttribute("curTime")%></span>
</body>
</html>
  1. <% %> 中书写的代码被直接解析成java代码;
  2. html部分都被out.write(“”)方法以字符串的形式拼接,然后响应给浏览器;
  3. 页面上输出的内容都在_jspService方法中,这个方法有两个参数request,response。由此可看出JSP本质上就是一个Servlet。

jsp中书写java代码的几种方式

方式1.<%java代码%>

<%@ page contentType="text/html;charset=utf-8" language="java" %>
<html>
<head>
    <title>Demo1</title>
</head>
<body>
    <!--脚本片段-->
    <%
        for(int i = 0;i<10;i++){
            response.getWriter().println("i="+i);
        }
    %>
</body>
</html>

方式2.脚本表达式<% =表达式 %>

为了简化用response对象书写繁琐性
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>DEMO</title>
</head>
<body>
    <% ="这是DEMO示例文本" %>
    <!--和下列表达式等价-->
    <%
        //信息先存在缓存中,然后输出
        out.println("hello out");
        //直接输出。效率更高
        response.getWriter().println("这是示例文本")

    %>
</body>
</html>

方式3.成员变量<% !java代码%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>DEMO</title>
</head>
<body>
    <% ="声明成员变量" %>
    <!--和下列表达式等价-->
    <%!
        public viod method1(){};
        int i = 10;
    %>
</body>
</html>

jsp的使用

jsp是问了简化servlet的书写,和在页面上的表达方式,所以用jsp来动态取值,显示在页面上。

演示:
- 在servlet中向session中存入了一个Student stu {name:张三, age:18}对象;
- 在jsp中取出该对象

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Demo</title>
</head>
<body>
    <% 
        Student student = request.getSession().getAttribute("stu");
        String stu_name = student.getName();
        int age = student.getAge();

        response.getWriter().println("name"+stu_name+"----age"+age);
        //等价于
        <% ="name"+stu_name+"----age"+age %>
    %>
</body>
</html>

EL

  • 全称:Expression Language
  • EL的出现,是为了简化JSP的书写,从【域对象】中查找指定的数据。
  • EL的表达式:${表达式内容}

EL的四大域

域对象 对象名称 说明
page域 pageScope page域指的是当前JSP页面,其中存储的数据只在当前页面有效
request域 requestScope request域:一次请求或请求链中request域
session域 sessionScope session域:一次会话过程中,session域
application域 applicationScope application域:服务启动后整个项目对应的ServletContext域

EL表达式从四大域中取值

域对象 取值方式
page域 ${pageScope.xxx}
request域 ${requestScope.xxx}
session域 ${sessionScope.xxx}
application域 ${applicationScope.xxx}

示例代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--
    分别往request域,session域,servletContext域和pageContext中存值
--%>
<%
    request.setAttribute("requestName","requestScope");
    request.getSession().setAttribute("SessionName","SessionScope");
    request.getServletContext().setAttribute("ServletContextName","servletContextScope");
    pageContext.setAttribute("pageName","PageScope");
%>
<!--使用EL表达式从上述域中取值-->
从request域中取值:${requestScope.requestName}<br/>
从session域中取值:${SessionScope.SessionName}<br/>
从servletContext域中取值:${"ServletContextScope.serletContextName}<br/>
从page域中取值:${pageScope.pageName}

</body>
</html>

EL取值域的查找

当不确定域的时候,从page->request->sesssion->servletContext,从小到大开始寻找。

EL表达式cookie中取值

//servlet 代码

protected void doGet(HttpServletRequest request,HttpServletResponse response)throws Exception{
    //创建Cookie
    Cookie cookie = new Cookie("username","lisi");
    response.addCookie(cookie);
}


//jsp代码
<body>
    姓名:<input type="text" name="username" value="${cookie.username.value}"/>
</body>

EL运算符

  • 同java一致

JSTL

  • 全称The JavaServer Pages Standard Tag Library

    • 引入标签

    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

JSTL的核心标签库

  • JSTL的常用标签
1. <c:if test></c:if>
2. <c:forEach var="i" items="arr" begin="" end="" step="1"></c:forEach>
3. <c:choose></c:choose><c:when><c:otherWise>连用
//<c:if>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    session.setAttribute("user","username");
%>
<c:if test="${user==null}">
    <c:out value="请登录"></c:out>
</c:if>
<c:if test="${user!= null}">
    <c:out value="登录成功"></c:out>
</c:if>
</body>
</html>


//servlet
protected void doGet(HttpServletRequest request ,HttpServletResponse response)throws Exception{
    List<String> list = new ArrayList<>();
    list.add("zhangsan");
    list.add("lisi");
    list.add("wangwu");

    request.getSession.setAttribute(list);
}


//<c:forEach>从域中取出来
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--
使用foreach进行遍历
--%>
<c:forEach items="${list}" var="item" begin="0" end="${list.size()}" step="1">
    <font color="red">${item}</font><br>
</c:forEach>
</body>
</html>

对map集合的遍历

<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2018/8/13
  Time: 17:57
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    int[] arr = {123,456,789};
    request.setAttribute("arr",arr);
    Map<String,String> map = new HashMap<>();
    map.put("one","杨幂");
    map.put("two","关晓彤");
    map.put("three","刘诗诗");
    request.setAttribute("map",map);
%>
<c:forEach items="${arr}" var="item">
    ${item}<br>
</c:forEach>
<c:forEach items="${map}" var="item">
    map的key:${item.key}=${item.value}<br>
</c:forEach>
</body>
</html>

choose标签

protected void doGet(HttpServletRequest request,HttpServletResponse response)throws Exception{
    int id = 1;
    switch (id){
        case 0:
            System.out.println("id为0");
            break;
        case 1:
            System.out.println("id为1");
            break;
        default:
            System.out.println("id未知");
            break;
    }
    request.getSession().setAttribute("id",id);
}

//html<c:choose> 等价于
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <c:choose>
        <c:when test="${id}==0">
            id为0
        </c:when>
         <c:when test="${id}==1">
            id为1
        </c:when>
        <c:otherWise>
            id未知
        </c:otherWise>
    </c:>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_41263632/article/details/81637293
今日推荐