EL
EL(Expression Language),目的是替换JSP页面中的复杂代码
EL表达式语法:
${变量名}
案例
1.创建注册页面
<%@ 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>
<form action="/EL/ElServlet" method="post">
<p>UserName:<input type="text" name="username"></p>
<p>Age:<input type="text" name="age"></p>
<input type="submit" value="SUBMIT">
</form>
</body>
</html>
2.创建Servlet,实现接收提交的数据,存储和转发到下一个页面
package com.kilig.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/ElServlet")
public class ElServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username =request.getParameter("username");
String age =request.getParameter("age");
request.setAttribute("username", username);
request.setAttribute("age", age);
request.getRequestDispatcher("/2.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
3.创建新的jsp页面,接收数据
<%@ 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>
Name:${username}
Age:${age}
</body>
</html>
让我们运行看一看吧:
JSTL
JSP标准标签库(JavaServletPages Standard Tag Library)
JSTL与EL表达式合作实现JSP页面的编码
JSTL开发的准备
在JSP页面添加taglib指令:
<%@ taglib uri="http://java.sun.com/jsp/jstl/sore" prefix="c" %>
- 通用标签:set, out, remove
- 条件标签:if, choose
- 迭代标签:forEach
set:将value值存储到指定范围里
将value值存储到范围为scope的变量variable中
<c:set var="username" value="里斯" scope="scope" />
out:将结果输出显示
<c:out value="value"/>
remove:删除指定域内的数据
<c:remove var="username" scope="session"/>
if标签
test:接判断条件,如果条件为true,则执行标签中的内容
<c:if test="${变量名}">
choose标签
里面包含when ,otherwise两个标签。类似java中的if-else
<c:choose>
<c:when test="${变量名}">true的结果</c:when>
<c:otherwise>false的结果</c:otherwise>
</c:choose>
forEach
<c:forEach items="${集合名}" var="集合">
${集合.变量名}
</c:forEach>