4--Include instructions in JSP

Static inclusion

The static include instruction is to insert a file containing text or code when the JSP is compiled. The include process is static, and the included file can be a JSP file, an HTML file, a text file, or a java program.
The syntax of static inclusion:
<%@include file=”file path to be included”%>
defines 3 included files: info.htm, info.jsp, info.txt
Example 1: info.htm

<html>
<head>
<title>Insert title here</title>
</head>
<body>
	<h2>
		<font style="color:red;">info.html</font>
	</h2>
</body>
</html>

Example 2: info.jsp

<%@page language="java" contentType="text/html" pageEncoding="GBK"%>
<%@page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
	<h2>
		<font style="color:green;">info.jsp</font>
	</h2>
	<% 
	    //定义一个变量
   	   int num=3;
   %>
</body>
</html>

Example 3: info.txt

内容随便写

Example 4: Included files (normal)

<%@page language="java" contentType="text/html" pageEncoding="GBK"%>
<%@page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
	 <h1>静态包含操作</h1>
   <%@include file="info.html" %>
   <%@include file="info.jsp" %>
   <%@include file="info.txt" %>
</body>
</html>

Example 5: Included files (abnormal)

<%@page language="java" contentType="text/html" pageEncoding="GBK"%>
<%@page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
	 <h1>静态包含操作</h1>
   <%@include file="info.html" %>
   <%@include file="info.jsp" %>
   <%@include file="info.txt" %>
   <%
      int num =3;//异常
   %>
</body>
</html>

In static inclusion, the contents of the included 3 files are actually imported into include.jsp, and then compiled together, and finally a whole content is displayed, that is, first include, and then all Compilation processing of content code .

Dynamic inclusion

Example 6:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html >
<html>
<head>
<title>Insert title here</title>
</head>
<body>
	<!-- 动态包含 -->
	<jsp:include page="info.html"></jsp:include>
	<jsp:include page="info.jsp"></jsp:include>
	<jsp:include page="info2.jsp"></jsp:include>
	<jsp:include page="info.txt"></jsp:include>
	<%
	   int num =4;//正常运行
	%>
</body>
</html>

Through example 5 and example 6, you can compare:
use the <jsp:include> instruction to complete the operation of dynamic inclusion. Unlike the previous static inclusion, the dynamic inclusion statement can automatically distinguish whether the included page is static or dynamic. If If it is a static page, the content will be included in the same way as the static inclusion. If the included page is dynamic, it can be processed dynamically first, and then the processed result will be included .

Dynamically include parameter passing

The following codes are used in combination.
Example 7: Include pages

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<html>
<head>
<title>包含页面</title>
</head>
<body>
	<h1>动态包含操作并接受参数</h1>
	<%
		String username = "zhangsans";
	%>
	<jsp:include page="receive.jsp">
		<jsp:param value="<%=username%>" name="name" />
		<jsp:param value="23" name="age" />
	</jsp:include>
</body>
</html>

Example 8: The included page

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<html>
<head>
<title>被包含页面</title>
</head>
<body>
	<h1>
		参数一:<%=request.getParameter("name")%></h1>
	<h1>
		参数二:<%=request.getParameter("age")%></h1>

</body>
</html>

Using the second grammatical form of dynamic inclusion, parameters can be passed to the included page, and the included page can use the request.getParameter() method to receive parameters.

Guess you like

Origin blog.csdn.net/qwy715229258163/article/details/113976591