EL、JSTL

发布于:2024-04-03 ⋅ 阅读:(137) ⋅ 点赞:(0)

EL

index.jsp

<%@page import="com.bean.Student"%>
<%@ 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">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>

   <%
   pageContext.setAttribute("username", "page的名字");
   request.setAttribute("username", "request的名字");
   session.setAttribute("username", "session的名字");
   application.setAttribute("username", "application的名字"); 
   Student s =  new Student();
   s.setName("二喜");
   request.setAttribute("student", s);
   
   List<String> list = new ArrayList<String>();
   list.add("三喜");
   list.add("喜欢");
   list.add("学习");
   request.setAttribute("list", list);
   
   Map<String,Object> map = new HashMap();
   map.put("username", "四喜");
   map.put("userpwd", "1234");
   request.setAttribute("map", map);
    %>
    
    
 
<!--El表达式取值   (在不指定默认值时会从最小的作用域中取值) -->
      ${username}<br>
     
      ${pageScope.username}<br>
      ${requestScope.username}<br>
      ${sessionScope.username}<br>
      ${applicationScope.username}<br>
      

      <!-- EL在对象中取值的两种方式 -->
      姓名:${student.name}
         ${student["name"]} <br>
   
 <!-- 集合取值可以通过数组取值 -->
  集合取值:${list}
  ${list[0]}${list[1]}${list[2]} <br>
 
   <!-- map取值的两种方法 -->
 map的值:${map.username}  ${map.userpwd} <br>
          ${map["username"]}  ${map["userpwd"] }       
      <br><br>
      
      

    ====算术运算符====
      ${1+1}<br>
      <!-- 页面报错不会直接抛异常 -->
      ${1/0}<br>
      
 
  ====关系运算符(结果ture或者false)====
    ${1>2}<br>
    
   
====逻辑运算符(结果ture或者false)====
    ${1>2||1<2}<br>
   
====空值判断 empty运算符(结果ture或者false)====
    ${empty username}<br>
    
    <a href = "a.jsp?a=erxi">param传值</a>

  </body>

</html>

JSTL

a.jsp

<%@ 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 '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>
    <form action="b.jsp" method = "post">
    昵称:<input name = "name"><br>
    所在城市:<input name = "city"><br>
    您所使用的开发语言:<input name = "language" type = "checkbox" value = "java">java
    <input name = "language" type = "checkbox" value = "C">C
    <input name = "language" type = "checkbox" value = "C++">C++
    <input name = "language" type = "checkbox" value = "PHP">PHP
    <input name = "language" type = "checkbox" value = "ASP">ASP
    <br>
    <input type = "submit" value = "提交">
    </form>
   
//param取值
    ${param.a}
    <%=request.getParameter("a") %>

(两种方法效果相同)
  </body>
</html>

b.jsp

<%@ 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 'b.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>
    <%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
    
    String name =  request.getParameter("name");
    String city =  request.getParameter("city");
    String[] languages =  request.getParameterValues("language");
    
    List list =  new ArrayList();
    for(String language:languages){
        list.add(language);
    }
    request.setAttribute("name", name);
    request.setAttribute("city", city);
    request.setAttribute("list", list);
    
     %>
     昵称:${name}
     城市:${city}
   您所使用的开发语言:${list[0]}${list[1]}${list[2]}${list[3]}${list[4]}

     
  </body>
</html>

c.jsp

<%@page import="com.bean.Student"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri ="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
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 'c.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>
  <%
  Student s = new Student();
  request.setAttribute("s",s);
   %>
  <body>
 
通用标签-set设置指定范围内的变量值
    <c:set var= "name" value = "二喜" scope="request"></c:set>
    request:${requestScope.name}<br>
    session:${sessionScope.name}
    
   <c:set target="${s}" property = "name" value = "三喜"></c:set>  
    ${s.name}<br>
    
   
通用标签-out:计算表达式并将结果输出显示 
    <c:out value="${s.name }"></c:out><br>
   
   
通用标签-remove删除指定范围内的变量
   <c:remove var="name"/>
   ${name}<br>
   
   
条件标签-if实现Java语言中if语句的功能
   <c:if test="${1<2}">
   1《2是对的
   </c:if><br>

   
  </body>
</html>


网站公告

今日签到

点亮在社区的每一天
去签到