JSP内置对象
1:内置对象简介
JSP内置对象是WEB容器创建的一组对象,不是用new 关键就可以使用的
2:四种作用域范围
3:out
4:request/response
5:seession
6:applicaton
7:其他
JSP内置对象是WEB容器中创建的一组对象,可以直接使用不需要new,如截图中的out 对象
五大常用对象: out、request、response、session、application<br>
其它四个不常用对象:page、pageContext、exception、config
<%@ page language="java" import="java.util.*" contentType="text/html; charset=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 'MyJsp.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>out内置对象</h1><%//clear()清除缓冲区out.println("<h2>静夜思</h2>");out.println("床前明月光<br>");out.println("床前明月光<br>");out.flush();//没任何区别输出,缓冲区变大,调用clear()的话,会打印前几句,false//clearBuffer() 不会抛出异常out.println("床前明月光<br>");out.println("床前明月光<br>");%>缓冲区大小:<%=out.getBufferSize() %>byte<br>缓冲区剩余大小:<%=out.getRemaining() %>byte<br>是否清空缓冲区:<%=out.isAutoFlush() %><br></body></html>
<%@ page language="java" import="java.util.*"
contentType="text/html; charset=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 'login.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>用户登录</h1>
<hr>
<from action="dologin.jsp" name="loginForm" method="get"><%--post --%>
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登陆"></td>
</tr>
</table>
</from>
</body>
</html>
<body>
<h1>登陆成功</h1>
<hr>
</body>
<%@ page language="java" import="java.util.*"
contentType="text/html; charset=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 'req.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>用户注册</h1>
<hr>
<form name="regForm" action="request.jsp" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>爱好:</td>
<td><input type="checkbox" name="favorite" value="read">读书
<input type="checkbox" name="favorite" value="music">音乐 <input
type="checkbox" name="favorite" value="movie">电影 <input
type="checkbox" name="favorite" value="internet">上网</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登陆"></td>
</tr>
</table>
</form>
<br>
<a href="request.jsp?username=lisi"> 测试url传参</a> <!-- 中文“李四”会出问题,通过tomcat-conf-server-Connector标签+URLEncoding="utf-8"-重启tomcat -->
</body>
</html>
<body>
<h1>requst内置对象</h1>
<%
request.setCharacterEncoding("utf-8");//可以提交汉字
request.setAttribute("password", "123456");
%>
用户名:<%=request.getParameter("username")%><br> 爱好:<%
if (request.getParameterValues("favorite") != null) {
String[] favorites = request.getParameterValues("favorite");
for (int i = 0; i < favorites.length; i++) {
out.println(favorites[i] + " ");
}
}
%><br> 密码:<%=request.getAttribute("password")%><br>
请求体的MIME类型:<%=request.getContentType()%><br> 协议类型及版本号:<%=request.getProtocol()%><br>
服务器主机名:<%=request.getServerName()%><br> 服务器端口号:<%=request.getServerPort()%><br>
请求文件的长度(字节):<%=request.getContentLength()%><br> 请求客户端的ip地址:<%=request.getRemoteAddr()%><br>
请求真实路径 :<%=request.getRealPath("request.jsp")%><br> 请求上下文路径:<%=request.getContextPath()%><br>
</body>
response对象
-
<%@page import="java.io.PrintWriter"%>
<%@ page language="java" import="java.util.*"
contentType="text/html; charset=UTF-8"%>
<%
response.setContentType("text/html; charset=UTF-8");//设置的响应的MIMI类型
out.println("<h1>reponse内置对象</h1>");
out.println("<br>tttt");
PrintWriter out = response.getWriter();//获取输出流对象
outer.println("大家好,我是response输出流");//输出 提前与内置out,用.fluse(清缓存)可以将out提前
response.sendRedirect("reg.jsp");//请求重定向(直接跳转)
%>
-请求重定向
客户端行为-----response.sendRedirect(),两次请求,地址栏地址改变
-请求转发
服务器行为-----request.getRequestDispatcher().forward(req,resp);地址不改变
<body>
<h1>用户注册</h1>
<hr>
<form name="regForm" action="response.jsp" method="post">
- <-- action="response.jsp"-->
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>爱好:</td>
<td><input type="checkbox" name="favorite" value="read">读书
<input type="checkbox" name="favorite" value="music">音乐 <input
type="checkbox" name="favorite" value="movie">电影 <input
type="checkbox" name="favorite" value="internet">上网</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登陆"></td>
</tr>
</table>
</form>
<br>
<a href="request.jsp?username=lisi"> 测试url传参</a>
<!-- 中文“李四”会出问题,通过tomcat-conf-server-Connector标签+URLEncoding="utf-8"-重启tomcat -->
</body>
<%@page import="java.io.*,java.util.*"%>
<%@ page language="java" import="java.util.*"
contentType="text/html; charset=UTF-8"%>
<%
response.setContentType("text/html; charset=UTF-8");//设置的响应的MIMI类型
out.println("<h1>reponse内置对象</h1>");
out.println("<br>tttt");
PrintWriter outer = response.getWriter();//获取输出流对象
outer.println("大家好,我是response输出流");//输出 提前与内置out,用.fluse(清缓存)可以将out提前
//response.sendRedirect("reg.jsp");//请求重定向(直接跳转)
//请求重定向 response.sendRedirect("request.jsp");
//请求转发
request.getRequestDispatcher("request.jsp").forward(request, response);
%>
session
客户端和服务器一次对话----用户浏览网站花费的时间
在服务器内存中保存着不同用户session
内置对象
<body>
<h1>session内置对象</h1>
<hr>
<%
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date d=new Date(session.getCreationTime());/* 时间化 */
session.setAttribute("username", "admin");
%>
Session创建时间:<%=sdf.format(d) %><br><!-- 直接session.getCreationTime()会出现一长串代码 -->
Session的ID编号:<%=session.getId() %><br>
从Session获取用户名:<%=session.getAttribute("username") %><br>
</body>
<body>
<h1>session内置对象1</h1>
<hr>
<%
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date d=new Date(session.getCreationTime());/* 时间化 */
session.setAttribute("username", "admin");
session.setAttribute("password", "123456");
session.setAttribute("age", "18");
//设置当前session最大生成期限单位是秒
session.setMaxInactiveInterval(10);//10s
%>
Session创建时间:<%=sdf.format(d) %><br><!-- 直接session.getCreationTime()会出现一长串代码 -->
Session的ID编号:<%=session.getId() %><br>
从Session获取用户名:<%=session.getAttribute("username") %><br>
<a href="session2.jsp" target="_blank">用超链接跳转到Session2</a>
</body>
<body>
<h1>session内置对象2</h1>
<hr>
<%
// SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
// Date d=new Date(session.getCreationTime());/* 时间化 */
// session.setAttribute("username", "admin");
%>
Session的ID编号:<%=session.getId()%><br> 从Session获取用户名:<%=session.getAttribute("username")%><br>
session中保存的全部属性:<%
String[] names = session.getValueNames();
for (int i = 0; i < names.length; i++) {
out.println(names[i] + " ");
}
%>
</body>
session生命周期
创建-活动-销毁
session对象
Tomcat默认超时30s
.设置Session超时两种方式
1.session.setMaxInactiveInterval(时间)//单位是秒
2:在web.xml配置
<session-confi> <session-timeout>10</session-timeout></session-confi>//单位分
application
实现用户间数据共享,可存放全局变量
开始与服务器启动,终止服务器关闭
public void setAttribute(String,Object)使用指定名称将对象绑定到对话
public Object getAttribute(String)返回uzai次对话中指定名称绑定在一起的对象,没有返回null
Enumeration getAttributeNames()返回可用属性名枚举
String getServerInfo()返回JSP(SERVET)引擎名及版本号
<%@ page language="java" import="java.util.*" contentType="text/html; charset=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 'application.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>application内置对象</h1>
<hr>
<%
application.setAttribute("city", "北京");
application.setAttribute("postcode", "10000");
application.setAttribute("email", "lisi@126.com");
%>
所在城市:<%=application.getAttribute("city") %><br>
application所有属性:<%
Enumeration attibute=application.getAttributeNames();
while(attibute.hasMoreElements()){
out.println(attibute.nextElement()+" ");
}
%><br>
JSp(SERVET)引擎名及版本号:<%=application.getServerInfo() %>
</body>
</html>
Page
指向当前JSP页面本身,java.lang.Object
<body>
<h1>page内置对象</h1>
<hr>
当前page页面对象的字符串描述:<%=page.toString() %>
</body>
PageContext
提供对JSP页面内所有对象及名字空间的访问
可以访问本页所在session---也可以application的某一属性
<body>
<h1>pageContext内置对象</h1>
<hr>
用户名是:<%=pageContext.getSession().getAttribute("username") %><br>
<%//跳转到注册页面
//pageContext.forward("req.jsp");
//包含include.jsp内容
pageContext.include("include.jsp");
%>
</body>
<%@page import="javax.enterprise.inject.New"%>
<%@page import="java.text.*"%>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=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>");
%>
Config
在一个Servet初始化时,JSP引擎向它传递信息,
Exception
是一个异常对象,
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" errorPage="exception.jsp"%>
<!-- errorPage="exception.jsp"将异常交给 exception.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 'exception.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>测试异常页面</h1>
<%
System.out.println(100/0);//抛出算数异常
%>
</body>
</html>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" 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 'excepton.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>exception内置对象</h1>
异常消息:<%=exception.getMessage() %><br>
异常字符串描述:<%=exception.toString() %>
</body>
</html>
用户登录例子:用户名admin,登陆成功内部转发login_success.jspbong提示成功,失败则请求重定向login_failure.jsp
indexs.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=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 '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="styles.css">
-->
</head>
<body>
<div id="container">
<div></div>
<div id="box">
<form action="doologin.jsp" method="post">
<p class="main">
<label>用户名:</label><input name="username" value="" />
<label>密码:</label><input type="possword" name="password" value="" />
</p>
<p cclass="space">
<input type="submit" value="登录" class="login" style="cursor:poin">
</p>
</form>
</div>
</div>
</body>
</html>
doologin.jsp
<%@ page language="java" import="java.util.*"
contentType="text/html; charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
String username = "";
String password = "";
request.setCharacterEncoding("utf-8");//防止中文乱码
username = request.getParameter("username");
password = request.getParameter("password");
if ("admin".equals(username) && "admin".equals(password)) {
session.setAttribute("loginUser", username);
request.getRequestDispatcher("login_success.jsp").forward(
request, response);
} else {
response.sendRedirect("login_failure.jsp");
}
%>
login_success.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=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 '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="styles.css">
-->
</head>
<body>
<div id="container">
<div></div>
<div id="box">
<%
String loginUser="";
if(session.getAttribute("loginUser")!=null){
loginUser=session.getAttribute("loginUser").toString();
}
%>
欢迎你<font color="red"><%=loginUser %></font>,登陆成功
</div>
</div>
</body>
</html>
login_failure.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=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 '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="styles.css">
-->
</head>
<body>
<div id="container">
<div></div>
<div id="box">
登陆失败;
<a href="index.jsp">返回</a>
</div>
</div>
</body>
</html>