JSP指令标记和动作标记

1.jsp

<!-- JSP指令标记:page指令。language,import属性。 -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.util.*,java.io.*"%>

<!-- JSP指令标记:include指令。 -->
<%@ include file="Hello.txt"%>


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<!-- HTML注释可在网页看见 -->
	<%--JSP注释不能在网页看见 --%>
	<a href="addition1.jsp">查看应用page指令的contentType属性</a>
</body>
</html>

Hello.txt

<h3>Hello,World!</h3>

Demo:
JSP指令标记和动作标记_第1张图片
2.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>
	<!-- JSP动作标记:include,JSP动态加载一个文件,JSP页面运行时才加载文件 -->
	<jsp:include page="Hello.txt"></jsp:include>

	<!-- JSP动作标记:param -->
	<jsp:include page="addition2.jsp">
		<jsp:param name="num" value="5" />
	</jsp:include>
</body>
</html>

Hello.txt

<h3>Hello,World!</h3>

addition2.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>另一个JSP页面来计算阶乘:</p>
	<%
		int n = Integer.parseInt(request.getParameter("num")), sum = 1;
		for (int i = 2; i <= n; i++)
			sum *= i;
		out.println(sum);
	%>
</body>
</html>

Demo:
JSP指令标记和动作标记_第2张图片
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>
	<!-- JSP动作标记:forward,转向后不继续执行下面语句 -->
	<%
		int r = (int) (Math.random() * 10) + 1;
	%>
	<jsp:forward page="addition3.jsp">
		<jsp:param name="num" value="<%=r%>" />
	</jsp:forward>
	<p>测试forward动作标记转向后是否会继续执行下面的语句</p>
</body>
</html>

addition3.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>
	<%
		int r = Integer.parseInt(request.getParameter("num"));
		out.println("另一个JSP页面得到值: " + r);
	%>
</body>
</html>

Demo:
在这里插入图片描述

你可能感兴趣的:(JSP,JAVA,jsp)