JSP中有三大指令:page,include,taglib,之前已经说过了page的用法。这里介绍下include。
使用语法如下:
<%@ include file="URL"%>
比如有一个页面要包含另一个date.jsp页面,date.jsp提供一个时间输出:
<%@ page language="java" import="java.util.*,java.io.*" contentType="text/html; charset=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">
</head>
<body>
<h1>include</h1>
<%@ include file="date.jsp"%>
</body>
</html>
包含的date.jsp如下:
<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
import="java.util.*"
pageEncoding="utf-8"%>
<%
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:MM:ss");
Date d = new Date();
String s = sdf.format(d);
out.println(s);
%>
运行结果:
查看生成的源码,可以发现,两个页面变成了一个文件:
查看源码发现,可以更好的理解
使用include动作标签也可以完成上述的操作,添加标签如下:
<jsp:include page="date.jsp" flush="true" />
可以达到上面同样的效果。
观察发现,此时访问jsp生成了四个文件:
观察源码可以更好的理解:
一张图很好的说明了他们的区别(来源:慕课网):
forward动作是使用jsp:forwad标签实现:
<jsp:forward page="URL" />
可以达到与request.getRequestDispatcher("/url").forward(request,response)同样的效果。
常常与forward标签搭配使用,传递一些参数值:
<jsp:forward page="userForward.jsp">
<jsp:param value="test@qq.com" name="email"/>
<jsp:param value="666666" name="password"/>
</jsp:forward>
例如,登陆界面loginForward.jsp登录用户名密码,经过处理界面doLoginForward.jsp处理后,修改密码并新添加email参数后,转发给显示页面userForward.jsp。
loginForward.jsp代码如下
<%@ page language="java" contentType="text/html; charset=utf-8"
import="java.net.*"
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>loginForward</h1>
<hr>
<form name="loginForm" action="doLoginForward.jsp" method="post">
<table>
<tr>
<td>username</td>
<td><input type="text" name="username" ></input></td>
</tr>
<tr>
<td>password</td>
<td><input type="password" name="password" ></input></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="submit"/></td>
</tr>
</table>
</form>
</body>
</html>
doLoginForward.jsp代码如下:
<%@ page language="java" contentType="text/html; charset=utf-8"
import="java.net.*"
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>loginForward</h1>
<hr>
<jsp:forward page="userForward.jsp">
<jsp:param value="test@qq.com" name="email"/>
<jsp:param value="666666" name="password"/>
</jsp:forward>
</body>
</html>
userForward.jsp代码如下:
<%@ page language="java" contentType="text/html; charset=utf-8"
import="java.net.*"
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>userForward</h1>
<hr>
<%
request.setCharacterEncoding("utf-8");
String username = "";
String password = "";
String email = "";
if(request.getParameter("username")!=null){
username = request.getParameter("username");
}
if(request.getParameter("password")!=null){
password = request.getParameter("password");
}
if(request.getParameter("email")!=null){
email = request.getParameter("email");
}
%>
用户名:<%=username %>
密码:<%=password %>
邮箱:<%=email %>
</body>
</html>