jsp内置对象之page,pageContext,config,exception
page:
代码:
当前page页面对象的字符串描述:<%=page.toString() %>
结果:
page内置对象
当前page页面对象的字符串描述:org.apache.jsp.page_jsp@1dc66fd
pageContext:
代码:
pageContext.jsp:
<%@ page language="java" import="java.util.*,java.text.*" pageEncoding="UTF-8"%>
<%
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>My JSP 'out.jsp' starting page</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>pageContext内置对象</h1>
<hr>
用户名是:<%=pageContext.getSession().getAttribute("username") %>
<%
//跳转到注册页面
//pageContext.forward("reg.jsp");
pageContext.include("include.jsp");
%>
</body>
</html>
include.jsp:
<%@ page language="java" import="java.util.*,java.text.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
String s=sdf.format(date);
out.println(s+"<br>");
%>
结果:
pageContext内置对象
用户名是:admin 2016年09月23日
config:
exception:
程序:
exception.jsp:
<%@ page language="java" import="java.util.*,java.text.*" pageEncoding="UTF-8" errorPage="exception_text.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>My JSP 'out.jsp' starting page</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<!--
如需使用异常处理
在第一行添加:errorPage="exception_text.jsp"
-->
<body>
<h1>测试异常的界面</h1>
<hr>
<%
System.out.println(100/0);//抛出运行异常,算术异常
%>
</body>
</html>
exception_text.jsp:
<%@ page language="java" import="java.util.*,java.text.*" pageEncoding="UTF-8" errorPage="exception.jsp" 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>My JSP 'out.jsp' starting page</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<!--
如需使用异常处理
在第一行添加:isErrorPage="true"
-->
<body>
<h1>exception内置对象</h1>
<hr>
异常消息:<%=exception.getMessage() %><br>
异常的字符串描述:<%=exception.toString() %>
</body>
</html>
结果:
exception内置对象
异常消息:/ by zero
异常的字符串描述:java.lang.ArithmeticException: / by zero