page对象
page对象就是指向当前jsp页面本身,有点像javase中的this指针,它是Java.lang.Object类的实例。常用方法和Object类的常用方法一致:
class getClass() //返回当时被转译的servlet类。
int hashCode() //返回当时被转译的servlet类的哈希值。
Boolean equals(Object obj) //判断当前对象是否与指定对象相等
String toString() //把当时被转译的servlet类转换成字符串
void notify() //唤醒一个等待的线程
void notifyAll() //唤醒所有等待的线程
void wait(int timeout) //使一个线程处于等待直到timeout结束或被唤醒
void wait() //使一个线程处于等待直到被唤醒
pageContext对象
- pageContext对象提供了对JSP页面中所有对象及名字空间的访问操作
- pageContext对象可以访问本页面的session,可以访问本页面的application对象的任意属性值
- pageContext对象是某个页面中所有功能的集大成者
常用方法:
JspWriter getOut() //返回当前客户端相应被使用的JspWriter流(out)
HttpSession getSession() //返回当前页面中HttpSession对象(session)
Object getPage() //返回当前页面的Object对象(page)
ServletRequest getRequest() //返回当前面的ServletRequest对象(request)
servletResponse getResponse() //返回当前页的ServletResponse对象(response)
void setAttribute(Strign name, Object attribute) //设置属性及属性值
Object getAttribute(String name ,int scope) //在指定范围内取属性值
int getAttributeScope(String name) //返回某属性的作用范围
void forward(String relativeUrlPath) //使当前页面跳转到另外一个页面(服务器转发)
void include(String relativeUrlPath) //在当前位置包含另一个文件
config对象
config对象实现了javax.servlet.ServletConfig接口,它一般是在页面初始化时传递参数用的
常用方法:
ServletContext getServletContext() //返回含有服务器相关信息的ServletContext对象
String getInitParameter(String name) //返回初始化参数的值
Enumeration getInitParameterNames() //返回Servlet初始化所需所有参数的枚举
exception对象
exception对象是异常对象,当一个页面有一个异常抛出时,就需要做出处理,这个时候需要用到我们所熟悉的exception对象,exception实际上是java.lang.Throwable类的一个实例。如果一个页面需要用到exception对象,那么该页面的page属性中需要设置isErrorPage=true,exception对象才可以调用。
/*异常出现的页面,注意指明errorPage*/
<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8" errorPage="exception_solve.jsp"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>产生异常</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<%
System.out.println(10/0); //抛出算术异常
%>
</body>
</html>
//处理异常的页面,注意isErrorPage置为true
<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8" isErrorPage="true"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>处理异常</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
异常信息:<%=exception.getMessage() %> <br>
异常的字符串描述:<%=exception.toString() %> <br>
</body>
</html>