JSP九大内置对象

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.运行
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值