JSP中有三大指令:page,include,taglib,以前已经说过了page的用法。这里介绍下include。html
使用语法以下:java
<%@ include file="URL"%>
好比有一个页面要包含另外一个date.jsp页面,date.jsp提供一个时间输出: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以下:ide
<%@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); %>
运行结果:post
查看生成的源码,能够发现,两个页面变成了一个文件:ui
查看源码发现,能够更好的理解url
使用include动做标签也能够完成上述的操做,添加标签以下:spa
<jsp:include page="date.jsp" flush="true" />
能够达到上面一样的效果。.net
观察发现,此时访问jsp生成了四个文件:code
观察源码能够更好的理解:
一张图很好的说明了他们的区别(来源:慕课网):
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>