1、page对象
page对象就是指向当前JSP页面本身,有点像SE中的this指针,它时java.lang.Object类的实例。常用方法与Object类中的常用方法完全一致。
范例:使用page的toString方法
<%@ page import="java.util.Enumeration" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<meta charset="UTF-8">
</html>
<body>
<h1>page内置对象</h1>
page对象的toString():<%=page.toString()%>
</body>
2、pageContext对象
- pageContext对象
– pageContext对象提供了对JSP页面内所有的对象及名字空间的访问。
– pageContext对象可以访问到本页面所在的session,application的某一属性值。 - pageContext常用方法参见API: javax.servlet.jsp.PageContext 位于 jsp-api包中 。
范例:使用pageContext的几个常用方法
<%@ page import="java.util.Enumeration" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<meta charset="UTF-8">
</html>
<body>
<h1>page内置对象</h1>
<%
session.setAttribute("userName","zhangxiao");
%>
用户名为:<%=pageContext.getSession().getAttribute("userName")%>
<hr>
<%
// 跳转到新的页面
// pageContext.forward("login.jsp");
pageContext.include("include.jsp");
%>
</body>
include.jsp内容:
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
String str = sdf.format(date);
out.println(str+"<br>");
%>
exception对象
exception对象是一个异常对象,当一个页面在运行过程中发生了异常,就产生这个对象。如果一个JSP页面要应用此对象,就必须把isErrorPage置为true,否则无法编译。exception对象实际上是java.lang.Throwable的对象。
常用方法如下:
- String getMessage()返回描述异常的信息。
- String toString()返回关于异常的简短描述信息
- void printStackTrace()显示异常及其堆栈
- Throwable FillInStackTrace()重写异常的执行堆栈
首先创建错误页面,指定errorPage。
<%@ page import="java.util.Enumeration" %>
<%@ page contentType="text/html;charset=UTF-8" language="java"
errorPage="exception.jsp" %>
<html>
<meta charset="UTF-8">
</html>
<body>
<h1>测试exception对象</h1>
<%
// 抛出异常
System.out.println(10/0);
%>
</body>
然后在指定的错误页面将 isErrorPage 置为 true
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>
<html>
<meta charset="UTF-8">
</html>
<body>
<h1>exception内置对象</h1>
异常消息为:<%=exception.getMessage()%><br>
异常消息的字符串表示为<%=exception.toString()%><br>
</body>
3、JSP指令与动作元素
3.1 include
include指令语法:
<%@ include file = "URL"%>
先创建一个要包含的页面date.jsp
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
String str = sdf.format(date);
out.println(str);
%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<meta charset="UTF-8">
</html>
<body>
<h1>include指令</h1>
<hr>
<%@include file="date.jsp"%>
</body>
include动作语法:
<jsp:include page="URL" flush="true|false"/>
其中page为要包含的页面,flush表示被包含的页面是否要从缓冲区读取
范例:使用include动作/font>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<meta charset="UTF-8">
</html>
<body>
<h1>include动作</h1>
<hr>
<jsp:include page="date.jsp" flush="false"/>
</body>
3.2 forward动作
语法:
<jsp:forward page="URL"/>
等同于:
request.getRequestDispatcher("/url").forward(request,response);
创建一个用户登录页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>login</title>
</head>
<body>
<h1>用户登录</h1>
<hr>
<form action="/forward_action.jsp" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="userName"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="3"><input type="submit" value="登录"></td>
</tr>
</table>
</form>
</body>
</html>
forward_action.jsp如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>login</title>
</head>
<body>
<h1>用户登录</h1>
<hr>
<jsp:forward page="user.jsp"/>
</body>
</html>
user.jsp如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
</head>
<body>
<h1>用户信息</h1>
<hr>
<%
String userName = "";
String password = "";
if (request.getParameter("userName")!=null) {
userName = request.getParameter("userName");
}
if (request.getParameter("password")!=null) {
password = request.getParameter("password");
}
%>
用户名:<%=userName%><br>
密码:<%=password%><br>
</body>
</html>
3.3 param动作
语法:
<jsp:param name="参数名" value="参数值">
常常与jsp:forward一起使用,作为其子标签;修改上述forward_action.jsp,添加param动作如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>param动作</title>
</head>
<body>
<hr>
<% request.setCharacterEncoding("UTF-8"); %>
<jsp:forward page="user.jsp">
<jsp:param name="email" value="zhangxiao@bitekeji.com"/>
</jsp:forward>
</body>
</html>
修改user.jsp来获取param动作中新增的参数,如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
</head>
<body>
<h1>用户信息</h1>
<hr>
<%
request.setCharacterEncoding("utf-8");
String userName = "";
String password = "";
String eamil = "";
if (request.getParameter("userName")!=null) {
userName = request.getParameter("userName");
}
if (request.getParameter("password")!=null) {
password = request.getParameter("password");
}
if (request.getParameter("eamil")!=null) {
eamil = request.getParameter("eamil");
}
%>
用户名:<%=userName%><br>
密码:<%=password%><br>
电子邮箱:<%=eamil%><br>
</body>
</html>