JSTL核心标签库——重定向标签、URL处理标签、网页导入标签

<c:redirect>重定向标签

  相当于HttpServletResponse的sendRedirect()方法

<%@page contentType="text/html" pageEncoding="UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
</head>
<body>

<c:if test="${1 > 2}">
    <c:redirect url="/home.jsp">
        <c:param name="name" value="${param.name}"></c:param>
        <c:param name="age" value="${param.age}"></c:param>
    </c:redirect>
</c:if>

</body>
</html>
index.jsp
<%@ page contentType="text/html" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <title>Home</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
    欢迎 ${param.name} 登陆
</body>
</html>
home.jsp

测试:http://127.0.0.1/home.jsp?name=zs&age=18
响应:欢迎 zs 登陆


<c:url>URL重写标签 

  在用户关闭Cookie功能时,自动用Session ID来进行URL重写。

    <a href="<c:url value="/home.jsp"></c:url>"></a>
    <form action="<c:url value="/home.jsp"></c:url>"></form>

<c:import>标签网页导入标签

包括其他JSP网页至目前网页的方式:
1、通过include指示元素
2、通过标准标签<jsp:include>
3、<c:import>标签,可以看作是<jsp:include>的加强版

Demo:

<%@page contentType="text/html" pageEncoding="UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Hello</title>
</head>
<body>
    <c:import url="/home.jsp">
        <c:param name="name" value="${param.name}"></c:param>
    </c:import>
    <br>
    年龄:${param.age}
</body>
</html>
index.jsp
<%@ page contentType="text/html" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <title>Home</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
    欢迎 ${param.name} 登陆
</body>
</html>
home.jsp

测试:http://127.0.0.1/index.jsp?name=zs&age=18

响应:

欢迎 zs 登陆
年龄:18  

观察:地址栏没有变化;当前页面的内容也输出了

还可以导入其它Web应用程序中的网页:

<%@page contentType="text/html" pageEncoding="UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Hello</title>
</head>
<body>
    年龄:${param.age}
    <%-- charEncoding属性用来指定要导入的网页的编码 --%>
    <c:import url="https://www.baidu.com" charEncoding="UTF-8">
        <c:param name="name" value="${param.name}"></c:param>
    </c:import>
    <br>
</body>
</html>
index.jsp

观察:地址栏没有变化;当前页面的内容也输出了;百度的功能还正常。

原文地址:https://www.cnblogs.com/Mike_Chang/p/10088575.html