一、include指令与动作
在JSP中在一个页面里包含其他的页面有两种方法:include指令和include动作。
1、include指令的语法为:<%@ include file="URL" %>,其中file是指要包含的页面。
2、include动作的语法为:<jsp:include page="URL" flush="false|true"/>,其中page为要包含的页面,flush指被包含的页面是否从缓冲区读取,默认为false。
include指令与动作的区别可以从下图中看出(图片摘自慕课网):
其中最重要的区别是第三点和第四点,第三点是指当使用include指令时包含的其实是被包含文件的源码,而include动作包含的是页面的输出。下面将根据一个实例来比较两种方式的区别。
首先创建一个被包含页面date.jsp,这个页面只输出一个被格式化的当前日期。
<%@ page language="java" import="java.text.*, java.util.*" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
out.println(sdf.format(date));
%>
我们先来看include指令,写一个页面includeCmd.jsp,使用include指令将date.jsp包含进来。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Include指令</title>
</head>
<body>
<h1>Include指令</h1>
<hr/>
<%@ include file="date.jsp" %>
</body>
</html>
发布并运行工程,访问includeCmd.jsp页面,页面显示如下:
这时再打开Tomcat目录下/work/Catalina/localhost/jsp-web/org/apache/jsp/jsp/cmdAndAction,会发现只生成了一个Servlet名为includeCmd_jsp。
打开这个文件,查看源码,会发现这个Servlet直接把date.jsp中的源码包含进来了。
...
out.write("\t<h1>Include指令</h1>\n");
out.write("\t<hr/>\n");
out.write("\t");
out.write('\n');
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
out.println(sdf.format(date));
out.write("\n");
out.write("</body>\n");
out.write("</html>");
...
再来看include动作,写一个includeAction.jsp,使用include动作将date.jsp包含进来。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Include动作</title>
</head>
<body>
<h1>Include动作</h1>
<hr/>
<jsp:include page="date.jsp" flush="false"/>
</body>
</html>
发布并运行程序,访问includeAction.jsp显示的结果与includeCmd.jsp相同,这时查看Tomcat的work目录
会发现生成了两个Servlet,分别为date_jsp和includeAction_jsp,查看includeAction_jsp的源码,会发现,这个Servlet并没有将date.jsp的源码引入,只是包含了输出。
...
out.write("\t<h1>Include动作</h1>\n");
out.write("\t<hr/>\n");
out.write("\t");
org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, "date.jsp", out, false);
out.write("\n");
out.write("</body>\n");
out.write("</html>");
...
由此可以发现include指令与include动作的区别。
二、forward动作与param动作
forward动作是指页面跳转至某一页面,而param动作常常和forward动作一起使用,作为其子标签,为页面跳转添加一些参数。
1、forward动作的语法:<jsp:forward page="URL"/>,其中page是指跳转的页面名,forward动作类似于服务器内部转发方法:request.getRequestDispatcher("/url").forward(request, response)
2、param动作的语法:<jsp:param value="参数值" name="参数名"/>,使用param不仅可以新增一个参数,也可以修改原有的参数值。
下面举个例子来说明这两个动作的使用,先写一个登录页面login.jsp,其中包含用户名和密码,用户点击登录将请求交给dologin.jsp处理。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录</title>
</head>
<body>
<h1>登录</h1>
<hr/>
<form action="dologin.jsp" method="post">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="userName" value=""/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" value=""/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登录"/></td>
</tr>
</table>
</form>
</body>
</html>
在dologin.jsp中将请求转发到showUser.jsp页面,其中使用param动作添加一个参数email并为其赋值,并且修改password的值为888888。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>forward动作和param动作</title>
</head>
<body>
<jsp:forward page="showUser.jsp">
<jsp:param value="admin@test.com" name="email"/>
<jsp:param value="888888" name="password"/>
</jsp:forward>
</body>
</html>
在showUser.jsp展示接收到的用户信息。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>展示用户</title>
</head>
<body>
<h1>登录成功</h1>
<hr/>
<%
request.setCharacterEncoding("UTF-8");
String userName = "";
String password = "";
String email = "";
if(null != request.getParameter("userName")) {
userName = request.getParameter("userName");
}
if(null != request.getParameter("password")) {
password = request.getParameter("password");
}
if(null != request.getParameter("email")) {
email = request.getParameter("email");
}
%>
用户名:<%=userName %><br/>
密码:<%=password %><br/>
电子邮箱:<%=email %><br/>
</body>
</html>
发布并运行程序,进入login.jsp,在页面输入用户名为张三,密码123456,点击登录。
页面跳转,显示用户信息,说明forward动作生效,但是可以发现密码显示为888888,同时也显示了在登录页面没有的email的内容,说明param动作生效。