JSP内置对象是Web容器创建的一组对象.
九大内置对象: 1.out :页面输出;
2.request:得到用户的请求信息;
3.response::服务器向客户端的回应信息 ;
4.session:用来保存用户的信息;
5.application:表示所有用户的共享信息;
6.config:服务器配置,可以取得初始化参数;
7.page:表示从该页面中表示出一个Servlet实例;
8.pageContext : JSP的页面容器 ;
9. exception :表示JSP页面所发生的异常,在错误中才起作用.
out对象:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Demo_out</title>
</head>
<body>
<%<span style="white-space:pre"> </span>//获取缓冲区大小
<span style="white-space:pre"> </span>int buffer = out.getBufferSize();
<span style="white-space:pre"> </span>//获取未被占用的缓冲区大小
<span style="white-space:pre"> </span>int available = out.getRemaining();
<span style="white-space:pre"> </span>//正在使用的缓冲区大小
<span style="white-space:pre"> </span>int use = buffer - available;
%>
<h3>缓存大小:<%=buffer %></h3>
<h3>剩余缓存大小:<%=available %></h3>
<h3>使用中的缓存大小:<%=use %></h3>
</body>
</html>
request对象:
demo1_request.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="demo1_request.jsp" method="post">
请输入内容:
<input type="text" name="content_input">
<input type="submit" value="提交">
</form>
</body>
</html>
demo1_request.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>demo1_request</title>
</head>
<body>
<% //获得客户端传送给服务器端的参数值,
String content = request.getParameter("content_input");
%>
<h3><%=content %></h3>
</body>
</html>
response对象:(属于客户端跳转)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>demo2_response</title>
</head>
<body>
<%<span style="white-space:pre"> </span>//setHead方法将根据HTTP文件头的名字来设定它的值
response.setHeader("refresh", "3;url=hello.jsp");
%>
</body>
</html>
hello.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-
8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>hello</title>
</head>
<body>
<h1>Hello!</h1>
</body>
</html>
session对象:
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>login</title>
</head>
<body>
<form action="" method="post">
<p>用户名:<input type="text" name="uname_input"></p>
<p>密 码:<input type="password" name="upwd_input"></p>
<input type="submit" value="登录">
<input type="reset" value="重置">
</form>
<%
String username = request.getParameter("uname_input");
String password = request.getParameter("upwd_input");
%>
<%
if( !(null==username || "".equals(username) || null==password || "".equals(password)) ){
if( "admin".equals(username) && "123".equals(password)){
response.setHeader("refresh", "3;url=welcome.jsp");
<span style="white-space:pre"> </span>//该方法将username指定的对象添加到session对象中
session.setAttribute("userid", username);
%>
<h3>登录成功,3秒后跳转到欢迎页面!</h3>
<h3>如果没有跳转,请点击<a href="welcome.jsp">这里</a></h3>
<%
}else{
%>
<h3>登录失败,用户名或者密码错误!</h3>
<%
}
}
%>
</body>
</html>
logout.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>logout</title>
</head>
<body>
<%<span style="white-space:pre"> </span>//注销,是session不可用
session.invalidate();
response.setHeader("refresh", "3;url=login.jsp");
%>
<h3>注销成功,3秒后返回登录页面!</h3>
<h3>如果没有跳转,请点击<a href="login.jsp">这里</a>!</h3>
</body>
</html>
welcome.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>welcome</title>
</head>
<body>
<%
if( null != session.getAttribute("userid")){
%>
<h3>欢迎<%=session.getAttribute("userid") %>登录本系统,<a href="logout.jsp">注销</a></h3>
<%
}else{
%>
<h3>您尚未登录系统,请先进行系统的<a href="login.jsp">登录</a>!</h3>
<%
}
%>
</body>
</html>
四大作用域: (作用域:一个内置对象,可以在多少个页面中 保存并继续使用)
1.page :只在一个页面中保存属性,跳转之后无效;
2. request :只在一次请求中保存,服务器跳转后依然有效;
3. session :在一次会话范围中,无论何种跳转都可以使用,但是新开浏览器无法使用;
4.application :在整个服务器上保存,所有用户都可以使用.
以上4个内置对象都支持的属性操作方法:
1. public void setAttribute(String name, Object obj)
设置属性的名称及内容
2. public void getAttribute(String name)
根据属性名称取得内容
3. public void removeAttibute(String name)
删除指定的属性
page作用域:只在一个页面保存属性,跳转之后无效.
request作用域:在服务器跳转后,所有设置的内容依然会被保留下来。
session作用域:使用session设置属性后,不管是客户端跳转还是服务器跳转,只要属性设置了就都可以取 得.可以在任何一个与 设置页面相关的页面中取得该属性.
application :在整个服务器上保存,所有用户都可以使用.