静态包含、动态包含和重定向

学习-记录-分享 / 2024-10-23 / 原文

test.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>静态包含和动态包含对比</title>
</head>

<body>
    <p>静态包含:静态相当于复制源码进来</p>
    <%@ include file="static.jsp" %> <!-- 静态包含 -->
    
    <p>动态包含:动态相当于复制执行结果进来</p>
    <jsp:include page="action.jsp" /> <!-- 动态包含 -->
</body>
</html>

static.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.lang.Math"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>static</title>
</head>
<body>
<form method="post" name="form">
	<input type="text" name="redius">
	<input type="submit" name="submit" value="开始传输">
</form>
</body>
</html>

action.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>
<p>接收到:<%=request.getParameter("redius")%>
</body>
</html>

执行结果

将test.jsp更改为

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>重定向</title>
</head>

<body>
    <jsp:forward page="action.jsp" /> 
    <p>no show
</body>
</html>

运行结果为
noshow没有显示,且地址栏是test.Jsp,内容是action.jsp的内容