JSP九大内置对象
## (out,request,response,session,pageContext,application,config,page,exception )
requst
1.代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>201819101007李晓佳</title>
</head>
<body>
客户提交信息的方式:<%=request.getMethod()%>
<br>使用的协议:<%=request.getProtocol()%>
<br>获取发出请求字符串的客户端地址:<%=request.getRequestURI()%>
<br>获取发出请求字符串的客户端地址:<%=request.getRequestURL()%>
<br>获取提交数据的客户端IP地址:<%=request.getRemoteAddr()%>
<br>获取服务器端口号:<%=request.getServerPort()%>
<br>获取服务器的名称:<%=request.getServerName()%>
<br>获取服务器的主机名:<%=request.getRemoteHost()%>
<br>获取服务器端口号:<%=request.getServerPort()%>
</body>
</html>
2.运行
out
1.代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>201819101007李晓佳</title>
</head>
<body>
<pre>
<%
out.println("<b>使用println()方法向客户端浏览器输出文字:</b>");
out.println("您好!");
out.println("欢迎访问***博客");
%>
</pre>
</body>
</html>
2.运行
session
1.代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>201819101007李晓佳</title>
</head>
<body>
<form action="do_login.jsp" method="post">
userName:<input type="text" name="userName"><br/>
password:<input type="password" name="password"><br/>
<input type="submit" value="submit">
<input type="reset" value="reset">
</body>
</html>
2.运行
response
1.代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>201819101007李晓佳</title>
</head>
<body>
<%!int i=0; %>
<%
//每秒刷新一次
response.setHeader("refresh","1");
%>
<%=i++ %>
</body>
</html>
2.运行
PageContext
1.代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>201819101007李晓佳</title>
</head>
<body>
<h4>向pageContext域存取东西</h4>
<%
//下面两句等价
pageContext.setAttribute("name", "page");
pageContext.setAttribute("name", "page", pageContext.PAGE_SCOPE);
//下面两句等价
request.setAttribute("name", "request");
pageContext.setAttribute("name", "request", pageContext.REQUEST_SCOPE);
//类似的
pageContext.setAttribute("name", "session", pageContext.SESSION_SCOPE);
//ServletContext是application的真是对象
pageContext.setAttribute("name", "pageContext", pageContext.APPLICATION_SCOPE);
%>
<%=pageContext.getAttribute("name",pageContext.PAGE_SCOPE) %>
<%=pageContext.getAttribute("name",pageContext.REQUEST_SCOPE) %>
<%=pageContext.getAttribute("name",pageContext.SESSION_SCOPE) %>
<%=pageContext.getAttribute("name",pageContext.APPLICATION_SCOPE) %>
</body>
</html>
2.运行
page
1.代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>201819101007李晓佳</title>
</head>
<body>
<%! Object object; //声明一个Object型的变量 %>
<ul>
<li>getClass()方法的返回值:<%=page.getClass()%></li>
<li>hashCode()方法的返回值:<%=page.hashCode()%></li>
<li>toString()方法的返回值:<%=page.toString()%></li>
<li>与Object对象比较的返回值:<%=page.equals(object)%></li>
<li>与this对象比较的返回值:<%=page.equals(this)%></li>
</ul>
</body>
</html>
2.运行
application
1.代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>201819101007李晓佳</title>
</head>
<body>
if (application.getAttribute("count") == null)
{
application.setAttribute("count", 1);
}
else
{
int count = (Integer) application.getAttribute("count");
count++;
application.setAttribute("count", count);
}
out.print("访问过本网站的人次为:" + application.getAttribute("count"));
</body>
</html>
2.运行
exception
1.代码(一)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>201819101007李晓佳</title>
</head>
<body>
if (application.getAttribute("count") == null)
{
application.setAttribute("count", 1);
}
else
{
int count = (Integer) application.getAttribute("count");
count++;
application.setAttribute("count", count);
}
out.print("访问过本网站的人次为:" + application.getAttribute("count"));
</body>
</html>
代码(二)error
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>错误提示页面</title>
<meta name="author" content="pan_junbiao的博客">
</head>
<body>
错误提示为:<%= exception.getMessage() %>
</body>
</html>
2.运行