一、JSP标签概念
JSP标签也称之为Jsp Action元素,它用于在JSP页面中提供业务逻辑功能,避免直接写Java代码。
二、常用JSP标签
常用标签:
- <jsp:include>
- <jsp:forward>
- <jsp:param>
1.<jsp:include>标签
作用:用于把另外一个资源的内容插入当前JSP页面的内容中,这种JSP在执行时的引入方式称为动态引入。
语法:<jsp:include page="url" flush="true" />,其中page属性指定被引入资源的路径,flush是否刷新当前JSP页面已输出的内容。
引入资源:
<jsp:include>标签和include指令的区别
head.jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<!-- Mirrored from www.zi-han.net/theme/hplus/login_v2.html by HTTrack Website Copier/3.x [XR&CO'2014], Wed, 20 Jan 2016 14:19:49 GMT -->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>test</title>
</head>
<body>
<%
int i=1000;
%>
<!-- 用jsp标签引入资源内容 -->
<p>head页面中i的值为<%=i %></p>
</body>
</html>
include指令是静态引入
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<!-- Mirrored from www.zi-han.net/theme/hplus/login_v2.html by HTTrack Website Copier/3.x [XR&CO'2014], Wed, 20 Jan 2016 14:19:49 GMT -->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>test</title>
</head>
<body>
<%
int i=100;
%>
<!-- 用jsp标签引入资源内容 -->
<p>test页面中i的值为<%=i %></p>
<%@include file="/head.jsp" %>
</body>
</html>
以上结果,是因为include指令是先引入在处理,当head.jsp引入后,head.jsp和test.jsp中同时存在相同的变量i,所以有冲突,导致编译异常。
<jsp:include>动态引入
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<!-- Mirrored from www.zi-han.net/theme/hplus/login_v2.html by HTTrack Website Copier/3.x [XR&CO'2014], Wed, 20 Jan 2016 14:19:49 GMT -->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>test</title>
</head>
<body>
<%
int i=100;
%>
<!-- 用jsp标签引入资源内容 -->
<p>test页面中i的值为<%=i %></p>
<jsp:include page="/head.jsp"></jsp:include>
</body>
</html>
<jsp:include>动态引入,是先处理两个JSP页面,然后再引入内容。
2.<jsp:forward>标签
作用:用于把请求转发给另外一个资源。
语法:<jsp:forward page="url"/> page属性用于指定请求转发资源路径
示例
test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"test1.jsp
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<!-- Mirrored from www.zi-han.net/theme/hplus/login_v2.html by HTTrack Website Copier/3.x [XR&CO'2014], Wed, 20 Jan 2016 14:19:49 GMT -->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>test</title>
</head>
<body>
<!-- 用jsp标签转发 -->
<jsp:forward page="/test1.jsp"></jsp:forward>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html><html lang="en"><!-- Mirrored from www.zi-han.net/theme/hplus/login_v2.html by HTTrack Website Copier/3.x [XR&CO'2014], Wed, 20 Jan 2016 14:19:49 GMT --><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"> <title>test</title></head><body> <p>转发后的页面</p></body></html>访问结果:
3.<jsp:param>标签
作用:当使用<jsp:include>和<jsp:forward>标签引入或将请求转发给其它资源时,可以使用<jsp:param>标签向这个资源传递参数。
语法:
- <jsp:include page="url"><jsp:param name="" value="" /></jsp:include>
- <jsp:forward page="url"><jsp:param name="" value="" /></jsp:forward>
其中,name属性指定参数名,value属性指定参数值
示例1:
test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<!-- Mirrored from www.zi-han.net/theme/hplus/login_v2.html by HTTrack Website Copier/3.x [XR&CO'2014], Wed, 20 Jan 2016 14:19:49 GMT -->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>test</title>
</head>
<body>
<!-- 用jsp标签转发 -->
<jsp:forward page="/test1.jsp">
<jsp:param value="hello" name="param1"/>
<jsp:param value="world" name="param2"/>
</jsp:forward>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<!-- Mirrored from www.zi-han.net/theme/hplus/login_v2.html by HTTrack Website Copier/3.x [XR&CO'2014], Wed, 20 Jan 2016 14:19:49 GMT -->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>test</title>
</head>
<body>
<p>接收到的参数</p>
<p><%=request.getParameter("param1") %></p>
<p><%=request.getParameter("param2") %></p>
</body>
</html>
示例2
test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<!-- Mirrored from www.zi-han.net/theme/hplus/login_v2.html by HTTrack Website Copier/3.x [XR&CO'2014], Wed, 20 Jan 2016 14:19:49 GMT -->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>test</title>
</head>
<body>
<jsp:include page="/head.jsp">
<jsp:param value="hello" name="param1"/>
<jsp:param value="world" name="param2"/>
</jsp:include>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<!-- Mirrored from www.zi-han.net/theme/hplus/login_v2.html by HTTrack Website Copier/3.x [XR&CO'2014], Wed, 20 Jan 2016 14:19:49 GMT -->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>test</title>
</head>
<body>
<p>接收到的参数</p>
<p><%=request.getParameter("param1") %></p>
<p><%=request.getParameter("param2") %></p>
</body>
</html>
访问结果: