include指令
语法:
<%@ include file="URL" %>
代码示例:
新建date.jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.text.*"%>
<%@page import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
Date d =new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
String s = sdf.format(d);
out.println(s);
%>
新建Include_Command.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>Insert title here</title>
</head>
<body>
<h1>Include指令</h1>
<hr>
<%@ include file="date.jsp" %>
</body>
</html>
Include动作
语法:
<jsp:include page="URL" flush="true|false" />
代码示例:
新建Include_Action.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>Insert title here</title>
</head>
<body>
<h1>Include动作</h1>
<hr>
<jsp:include page="date.jsp" flush="false"></jsp:include>
</body>
</html>
include指令与include动作之间的区别
forward动作
语法:
<jsp:forward page="URL" />
等同于:
request.getRequestDispatcher("/url").forward(request,response);
代码示例:
login.jsp文件
<%@ page language="java" import="java.util.*,java.net.*" 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>Insert title here</title>
</head>
<body>
<h1>用户登录</h1>
<hr>
<%
request.setCharacterEncoding("utf-8");
String username = "";
String password = "";
Cookie[] cookies = request.getCookies();
if (cookies != null && cookies.length > 0) {
for (Cookie c : cookies) {
if (c.getName().equals("username")) {
username= URLDecoder.decode(c.getValue(), "utf-8");
}
if(c.getName().equals("password")){
password = URLDecoder.decode(c.getValue(), "utf-8");
}
}
}
%>
<form name="loginForm" action="dologin_param.jsp" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username" value="<%=username %>" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" value="<%=password %>" />
</tr>
<tr>
<td colspan="2"><input type="checkbox" name="isUseCookie" checked="checked" />十天内记住我的登录状态</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="登录" /><input type="reset" value="取消" />
</tr>
<tr>
<td colspan="2" align="center">
</tr>
</table>
</form>
</body>
</html>
forward_Action.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>Insert title here</title>
</head>
<body>
<h1>Forward动作</h1>
<hr>
<!-- 相当于服务器内部跳转与request.getRequestDispatcher("users.jsp").forward(request, response);等价 -->
<%-- <jsp:forward page="users.jsp"></jsp:forward> --%>
<%
request.getRequestDispatcher("users.jsp").forward(request, response);
%>
</body>
</html>
users.jsp文件
<%@ page language="java" import="java.util.*,java.net.*"
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>Insert title here</title>
</head>
<body>
<h1>用户信息</h1>
<hr>
<%
request.setCharacterEncoding("utf-8");
String username="";
String password="";
if(request.getParameter("username") !=null){
username = request.getParameter("username");
}
if(request.getParameter("password") !=null){
password = request.getParameter("password");
}
%>
<br>
<br>
<br>
<br> 用户名:<%=username%>
<br> 密码:<%=password%>
<br>
</body>
</html>
param动作
语法:
<jsp:param name="参数名" value="参数值">
常常与<jsp:forward>
一起使用,作为其子标签
dologin_param.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>Insert title here</title>
</head>
<body>
<jsp:forward page="users.jsp">
<!-- 相当于给users.jsp传递了一个新的参数email -->
<jsp:param value="admin@123.net" name="email"/>
</jsp:forward>
</body>
</html>