通过请求转发、在目标页通过EL获取目标页数据:
<--首页:发送请求-->
<%@ 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>
<a href="./DataServlet?name=Tom">链接</a>
</body>
</html>
一、EL表达式:
- 基本数据类型&字符串
package servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DataServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
System.out.println(name);
request.setAttribute("score", 100);//int类型
request.setAttribute("address", "郑州市高新区");//字符串
request.getRequestDispatcher("target.jsp").forward(request, response);
}
}
目标页:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="niu" %>
<!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>
${score }
${address }
</body>
</html>
结果:
- 自定义类型和数组
自定义类型代码如下:
package servlet;
public class Student {
private String id;
private String name;
private String mobile;
private String address;
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getMobile() {
return mobile;
}
public String getAddress() {
return address;
}
public Student(String id, String name, String mobile, String address) {
super();
this.id = id;
this.name = name;
this.mobile = mobile;
this.address = address;
}
}
package servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DataServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
System.out.println(name);
request.setAttribute("student", new Student("803","Tom","150398650457","中国河南"));
request.setAttribute("names",new String[] {"ahui","ahao","achong","abiao"});
request.getRequestDispatcher("target.jsp").forward(request, response);
}
}
目标页:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="niu" %>
<!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>
<br/>
${student.id } ${student.name } ${student.mobile } ${student.address }
<br/>
${names[0] } ${names[1] } ${names[2] } ${names[3] }
<br/>
<niu:forEach var="name" items="${names }">
${name }
</niu:forEach>
</body>
</html>
结果:
- list集合和set集合:由于两者相似,只演示list集合。
package servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DataServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
System.out.println(name);
List<Integer> scores =new ArrayList<Integer>();
scores.add(90);
scores.add(95);
scores.add(100);
request.setAttribute("scores",scores );
request.getRequestDispatcher("target.jsp").forward(request, response);
}
}
目标页:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="niu" %>
<!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>
<br/>
${scores[0] } ${scores[1] } ${scores[2] } ${scores[3] }
<br/>
<niu:forEach var="score" items="${scores }">
${score }
</niu:forEach>
</body>
</html>
- map集合:
package servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DataServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
System.out.println(name);
/*Map集合获取元素方法有两种:
1、${集合名['key值'] }
2、EL标签循环获取*/
HashMap<String,String> firstmap =new HashMap<String,String>();
firstmap.put("1", "100");
firstmap.put("2", "200");
firstmap.put("3", "300");
request.setAttribute("firstmap",firstmap);
HashMap<Integer,String> secondmap =new HashMap<Integer,String>();
secondmap.put(1, "100一");
secondmap.put(2, "200二");
secondmap.put(3, "300三");
request.setAttribute("secondmap",secondmap);
request.getRequestDispatcher("target.jsp").forward(request, response);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>
<!--操作firstmap集合数据-->
${firstmap['1'] }
${firstmap['2'] }
${firstmap['3'] }
<br/>
<c:forEach var="elem" items="${firstmap }">
${elem }
<br/>
</c:forEach>
<!--操作secondmap集合数据观察结果-->
${secondmap['1'] }
${secondmap['2'] }
${secondmap['3'] }
<br/>
<c:forEach var="elem" items="${secondmap }">
${elem }
<br/>
</c:forEach>
</body>
</html>
这里发现如果key不是基本类型的话,就不能使用直接获取的方式进行获取,而是必须通过循环的方式。
二、EL表达式中的运算符运用
- 算术运算符
运算符号:+、-、*、/或div(没有按照最高精度保留结果的特点)、%或mod
- 关系运算符
运算符符号:= =或eq、!=或ne、<或lt(即less than)、>或gt(即great than)、<= 或 le、>= 或 ge
- 逻辑运算符
运算符符号:&& 或 and、|| 或 or、! 或 not(注意:只能对boolean型数据运算)
- empty:判断是否为空
package servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DataServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
System.out.println(name);
request.setAttribute("a", 1);
request.setAttribute("b", 2);
request.setAttribute("c", 3);
request.setAttribute("exp1", true);
request.setAttribute("exp2", false);
request.setAttribute("d", "");
request.getRequestDispatcher("target.jsp").forward(request, response);
}
}
target页;
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>
<span>算数运算符结果</span>
1+2=${a+b }
<br/>
3%2=${c mod b }
<span>关系运算符结果</span>
<br/>
${a==b }
<br/>
${a<b }
<br/>
${a>b }
<br/>
<span>逻辑运算符结果</span>
<br/>
${exp1 && exp2 }
<br/>
${exp1 || exp2 }
<br/>
${!exp2 }
<br/>
<span>empty运算符结果</span>
<br/>
${empty d }
<br/>
${not empty d }
</body>
</html>
结果如下: