jsp九大内置对象:
1. request 请求对象
2. response 响应对象
3. pageContest 页面上下文对象
4. session 会话对象
5. application 应用程序对象
6. out 输出对象
7. config 配置对象
8. page 页面对象
9. exception 例外对象
内置对象,又叫隐含对象,不需要预先声明就可以在脚本代码和表达式中随意使用
1、request对象:
该对象代表了客户端的请求信息,作用域为一次请求
常用方法:
1.request.setCharacterEncoding("utf-8") //设置字符编码
2.object getAttribute(String name) //返回指定属性的属性值
3.String getCharacterEncoding() //返回字符编码方式
4.String getParameter(String name) //返回name指定参数的参数值
5.void setAttribute(String key,Object obj) //设置属性的属性值
requesrt对象演示(完成一个简单的用户注册信息界面,将注册信息发送到欢迎界面上)
注册页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta charset="UTF-8">
<title>学员注册</title>
<meta name="author" content="赵玥茹">
<style>
.txtBox{
padding: 3px;
width: 300px;
font-size: 16px;
}
</style>
</head>
<body>
<div align="center">请输入注册信息
<form name="form1" method="post" action="reginfo.jsp">
<table border="0" align="center">
<tr>
<td>用户姓名:</td>
<td><input type="text" name="name" value="赵玥茹" class="txtBox" /></td>
</tr>
<tr>
<td>博客地址:</td>
<td><input type="text" name="blog" value="https://iuiuhi.cn" class="txtBox"/></td>
</tr>
<tr>
<td>备注信息:</td>
<td><input type="text" name="remark" value="您好,欢迎访问赵玥茹的博客!" class="txtBox"/></td>
</tr>
<tr>
<td>兴趣爱好:</td>
<td>
<input type="checkbox" name="interests" value="足球" />足球
<input type="checkbox" name="interests" value="篮球" />篮球
<input type="checkbox" name="interests" value="羽毛球" />羽毛球
<input type="checkbox" name="interests" value="乒乓球" />乒乓球
</td>
</tr>
<!-- 以下是提交、取消按钮 -->
<tr>
<td>
<input type="submit" value="提交" />
</td>
<td>
<input type="reset" value="取消" />
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
注册提交页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
request.setCharacterEncoding("UTF-8"); //设置请求编码
String name = request.getParameter("name"); //获取用户名
String blog = request.getParameter("blog"); //获取博客地址
String remark = request.getParameter("remark"); //获取备注信息
//获取兴趣爱好
String[] interests = request.getParameterValues("interests");
String interStr = "";
if(interests != null)
{
for(String item : interests)
{
interStr += item + ";";
}
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册信息</title>
<meta name="author" content="pan_junbiao的博客">
</head>
<body>
用户姓名:<%= name %><br/>
博客地址:<%= blog %><br/>
备注信息:<%= remark %><br/>
兴趣爱好:<%= interStr %>
</body>
</html>
效果图如下:
2.out对象:
它能把信息发送给客户端的浏览器,out对象有两个常用的方法:print();和println();
out对象也可以对缓冲区做相关操作:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>赵玥茹out</title>
</head>
<body>
<%
out.print("helloworld");
// out.clearBuffer(); // 将缓冲区的数据清空
out.flush(); // 先显示数据在清空缓存
out.println("缓冲区空间:" + out.getBufferSize());
out.print("剩余空间:" + out.getRemaining());
out.print("AutoFlush状态:" + out.isAutoFlush());
out.close();
%>
</body>
</html>
效果图如下:
常用方法:
1.void clear() //清除缓冲区的内容
2.void clearBuffer() //清除缓冲区的当前内容
3.void flush() //清空流
4.int getBufferSize() //返回缓冲区以字节数的大小,如不设缓冲区则为0
5.int getRemaining() //返回缓冲区还剩余多少可用
6.boolean isAutoFlush() //返回缓冲区满时,是自动清空还是抛出异常
7.void close() //关闭输出流
3.page对象:page 对象代表JSP本身,只有在JSP页面内才是合法的。 page隐含对象本质上包含当前 Servlet接口引用的变量,类似于Java编程中的 this 指针。
代码如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>201819101012赵玥茹page</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>
效果图如下:
4、application对象
application对象用于保存所有应用中的共有数据。它在服务器启动时自动创建,在服务器停止时销毁。
代码如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>application</title>
</head>
<body>
<%
Object obj=application.getAttribute("counter");
if (obj==null){
application.setAttribute("counter",new Integer(1));
out.println("该页面被访问了1次<br/>");
}else {
int countValue=new Integer(obj.toString());
countValue++;
out.println("该页面被访问了"+countValue+"次<br/>");
application.setAttribute("counter",countValue);//java会自动装箱
}
%>
</body>
</html>
效果图如下
5. exception对象
是一个例外对象,当一个页面在运行过程中发生了例外,就产生这个对象
代码如下
<%@ page contentType="texthtml;charset=UTF-8" language="java" errorPage="error.jsp" %>
<html>
<head>
<title>exception</title>
</head>
<body>
<%
int a=8;
int b=0;
int c=a/b;
%>
</body>
<html>
<%@ page contentType="texthtml;charset=UTF-8" language="java"
pageEncoding="UTF-8" isErrorPage="true"%>
<html>
<head>
<title>异常处理页面</title>
</head>
<body>
<hr>
异常类型:<%=exception.getClass()%><br/><br/>
异常信息:<%=exception.getMessage()%>
</hr>
</body>
<html>
效果如下
6、 config对象
config对象是在一个Servlet初始化时,JSP引擎向它传递信息用的,此信息包括Servlet初始化时所要用到的参数(通过属性名和属性值构成)以及服务器的有关信息(通过传递一个ServletContext对象)。 代码如下:
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>
jspconfigdemo
</servlet-name>
<jsp-file>/index.jsp</jsp-file>
<init-param>
<param-name>url</param-name>
<param-value>http://www.baidu.com</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>
jspconfigdemo
</servlet-name>
<url-pattern>/index.jsp</url-pattern>
</servlet-mapping>
</web-app>
----------
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>演示config对象</title>
</head>
<body>
<%
String url = config.getInitParameter("url");
String str = config.toString();
out.print("page对象的initParameter方法:"+url+"</br>");
out.print("page对象的toString方法:"+str);
%>
</body>
</html>
效果图如下:
7、response对象
response 代表的是对客户端的响应,主要是将JSP容器处理过的对象传回到客户端。response对象也具有作用域,它只在JSP页面内有效。
服务器收到客户端请求后做出的响应。它是HttpServletResponse类的实例
代码如下:
<%@ page contentType="text/html;charset=UTF-8" language="java"
pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charst=utf-8">
<title>response</title>
</head>
<body>
<center>
跳转页面到百度主页
<%//response.sendRedirect("https://www.baidu.com");%>
</center>
</body>
</html>
效果如下:
8、session对象
session 对象是由服务器自动创建的与用户请求相关的对象。服务器为每个用户都生成一个session对象,用于保存该用户的信息,跟踪用户的操作状态。session对象内部使用Map类来保存数据,因此保存数据的格式为 “Key/value”。 session对象的value可以使复杂的对象类型,而不仅仅局限于字符串类型。
是客户端与服务器的一次会话,从客户连到服务器的一个WebApplication开始,直到客户端与服务器断开连接为止。它是HttpSession类的实例。
session1代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
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 'index.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="css/style.css">
</head>
<body>
<form id="form1" name="form1" method="post" action="session2.jsp">
<div align="center">
<table width="40%" border="0">
<tr>
<td width="36%"><div align="center">您的名字是:</div></td>
<td width="64%">
<label>
<div align="center">
<input type="text" name="name" />
</div>
</label>
</td>
</tr>
<tr>
<td colspan="2">
<label>
<div align="center">
<input type="submit" name="Submit" value="提交" />
</div>
</label>
</td>
</tr>
</table>
</div>
</form>
</body>
<html>
session2代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
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 'session.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="css/style.css">
</head>
<body>
<%
String name = request.getParameter("name"); //获取用户填写的用户名
session.setAttribute("name",name); //将用户名保存在session对象中
%>
<div align="center">
<form id="form1" name="form1" method="post" action="sessionShow.jsp">
<table width="28%" border="0">
<tr>
<td>您的名字是:</td>
<td><%=name%></td>
</tr>
<tr>
<td>您最喜欢去的地方是:</td>
<td><label>
<input type="text" name="address" />
</label></td>
</tr>
<tr>
<td colspan="2"><label>
<div align="center">
<input type="submit" name="Submit" value="提交" />
</div>
</label></td>
</tr>
</table>
</form>
<p> </p>
</div>
</body>
<html>
sessionShow代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
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 'result.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="css/style.css">
</head>
<body>
<div align="center">
<%
Cookie[] ck=request.getCookies();
for(Cookie coo:ck){
String ID=coo.getValue();
System.out.println("ID:"+ID);
}
String sessionID=request.getSession(true).getId();
String name = (String)session.getAttribute("name"); //获取保存在session范围内的对象
String solution = request.getParameter("address"); //获取用户输入的最想去的地方
%>
<form id="form1" name="form1" method="post" action="">
<table width="28%" border="0">
<tr>
<td colspan="2"><div align="center"><strong>显示答案</strong></div> </td>
</tr>
<tr>
<td width="49%"><div align="left">您的名字是:</div></td>
<td width="51%"><label>
<div align="left"><%=name%></div>
<!-- 将用户输入的用户名在页面中显示 -->
</label></td>
</tr>
<tr>
<td><label>
<div align="left">您最喜欢去的地方是:</div>
</label></td>
<td><div align="left"><%=solution%></div></td>
<!-- 将用户输入的最想去的地方在页面中显示 -->
</tr>
<td><label>
<div align="left">sessionID:</div>
</label></td>
<td><div align="left"><%=sessionID%></div></td>
</tr>
</table>
</form>
<p> </p>
</div>
</body>
<html>
效果图如下: