JSP-内置对象
前言:
学习JSP内置对象,最重要还是理解,会用,能看懂相关源码。
Eclipse对html提示不是很友好,以前写.net是在VS上写的,对比起来。。。哈哈哈可以先在HBuilder上写好,如有相关推荐和指教,可以给我留言哦。
目标
- 了解JSP内置对象的分类及组成;
- 掌握输入/输出对象方法 request、response 和 out;
- 掌握作用域对象方法 pageContext、request 、session、application;
- 了解Servlet相关的对象 page config;
- 了解错误对象:exception
一、什么是JSP内置对象
1、JSP 内置对象是 Web 容器创建的一组对象;
2、JSP 内置对象是可以直接在JSP页面使用的对象 ,无需使用“new”获取实例;
3、JSP 内置对象的名称是 JSP 的保留字。
案例代码如下:
<%
request.setCharacterEncoding("GBK");
String titleName = request.getParameter("titlename");
%>
代码中request关键字可以在JSP页面直接使用。
二、JSP内置对象分类
JSP内置对象可分为四大类,输入/输出对象、作用域通讯和控制对象、Servlet相关对象和错误处理对象,它们都是用于JSP页面的表达式和代码段中,如下图:
三、JSP内置对象使用方法
3.1 out对象
-
out对象表示输出流
-
对象类型:javax.servlet.jsp.JspWriter
-
使用 write()、print() 和 println() 方法
案例代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>jsp内置对象(out对象的使用)</title>
</head>
<body>
<%
out.println("hello");
out.print("aaaa");
out.println("it");
%>
</body>
</html>
这里代码在html显示的时候是怎样的呢。
是不是觉得是这样的:
hello
aaaait
但其实是这样的:
helloaaaait
因为此时的HTML是这样的:
<!DOCTYPE html>
<html>
<head>
<title>jsp内置对象(out对象的使用)</title>
</head>
<body>
hello
aaaait
</body>
</html>
所以展示给我们的结果就是:helloaaaait
3.2 Request对象
- 表示客户端对网页的请求
- 实现 javax.servlet.http.HttpServletRequest接口
- 使用 HTTP 协议处理客户端的请求
Request常用方法:
方法名称 | 说明 |
---|---|
String getParameter(String name) | 根据页面表单组件名称获取页面提交数据 |
String[ ] getParameterValues (String name) | 获取一个页面表单组件对应多个值时的用户的请求数据 |
void setCharacterEncoding (String charset) | 指定每个请求的编码, 在调用request.getParameter()之前进行设定,可以解决中文乱码问题 |
request.getRequestDispatcher(String path) | 返回一个javax.servlet.RequestDispatcher对象,该对象的forward方法用于转发请求 |
案例代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSP内置对象-Request对象使用</title>
</head>
<body>
<h1>JSP内置对象-Request对象使用</h1>
<%
// 获得请求的参数
String msg = request.getParameter("message"); // 请求地址xx.jsp?message=hehe
out.print("message=" + msg + "<br/>");
// 获得请求的方法
String method = request.getMethod(); // 请求方法 doPost doGet
out.print("method:" + method);
out.print("<br />");
// 获得请求ContextUrl
String contextPath = request.getContextPath();
out.print("contextPath:" + contextPath);
out.print("<br />");
%>
</body>
</html>
使用Request对象完成用户注册,案例代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户注册(Request对象的使用)</title>
</head>
<body>
<h1>用户注册</h1>
<form action="doReg.jsp" method="post">
用户名:<input type="text" name="uname" value="hehe" /><br/>
密码:<input type="password" name="pwd" /><br/>
性别:<input type="radio" name="sex" value="1">男<input type="radio" name="sex" value="0">女<br>
兴趣爱好:<input type="checkbox" name="intrest" value="code">写代码
<input type="checkbox" name="intrest" value="sleep">睡觉
<input type="checkbox" name="intrest" value="waking">跑步
<br/>
你所在城市:<select name="city">
<option value="wuhan">武汉</option>
<option value="shanghai">上海</option>
<option value="guangzhou">广州</option>
</select>
<br />
自我介绍:
<textarea name="introduaction" rows="10" cols="20" >xxxxx</textarea>
<br />
<input type="submit" value="提交" />
</form>
</body>
</html>
处理用户注册页面doReg.jsp页面,案例代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
// 设置请求的编码(只能在post请求中有效)
request.setCharacterEncoding("utf-8");
String uname = request.getParameter("uname");
String pwd = request.getParameter("pwd");
String sex = request.getParameter("sex");
String[] intrest = request.getParameterValues("intrest");
String city = request.getParameter("city");
String introduaction = request.getParameter("introduaction");
%>
用户名:<%=uname%><br>
密码:<%=pwd%><br>
性别:<%=sex%><br>
您选择爱好:
<%
for(int i = 0;i < intrest.length;i++){
out.print(intrest[i] + " ");
}
%>
<br>
选择的城市:
<%=city%>
<br>
简介:
<%=introduaction%>
</body>
</html>
3.3 response 对象
- 处理 JSP 生成的响应
- 将响应发送给客户端
- 实现javax.servlet.http.HttpServletResponse 接口
- 使用 HTTP 协议将响应发送给客户端
response 对象常用方法
- void setContentType (String name)
设置作为响应生成的内容的类型和字符编码 - void sendRedirect (String name)
发送一个响应给浏览器,指示其应请求另一个URL
用户登录案例:
案例描述:
1、用户进入登录页面(login.jsp),如果输入的用户名和密码正确,页面跳转到欢迎页面(welcome.jsp),否则跳转到错误提示页面(error.jsp)。
案例代码如下:
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>response的使用</title>
</head>
<body>
<form action="dologin.jsp" method="post">
用户名:<input type="text" name="uname" /><br>
密码:<input type="password" name="pwd" /><br>
<input type="submit" value="登录" />
</form>
</body>
</html>
处理登录页面dologin.jsp页面代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
// 获得用户提交的用户和密码信息
String uname = request.getParameter("uname");
String password = request.getParameter("pwd");
// 如果用户名=admin 密码=123456
if("admin".equals(uname) && "123456".equals(password)){
// 登录成功,页面跳转到success.jsp
response.sendRedirect("success.jsp"); // URL重定向(理解:使用代码的方式重新发送一个请求,一个客户端的行为)
// response.sendRedirect("http://www.qq.com"); // 重定向 可以定向到应用之外的地址,如:http://www.qq.com
//request.getRequestDispatcher("success.jsp").forward(request, response); // 请求转发 还是那个请求,仅仅转发一次,但服务器的行为,所有客户端的浏览器地址不会发生变化
//request.getRequestDispatcher("http://www.qq.com").forward(request, response); // 请求转发仅仅只能在应用内部进行转发
}else{
response.sendRedirect("error.jsp");
}
%>
</body>
</html>
3.4 作用域对象组(pageContext对象、Request对象、Session对象、application对象)
内置对象的作用域包括pageContext、request、session 和 application,他们都有setAttribute(key,value)和getAttribute(key)的方法,分别表示在对应作用域中保存内容和获取内容。
PageContext对象
- 使用户可以访问页面作用域中定义的所有隐式对象
- 它的作用范围是在同一页面
- javax.servlet.jsp.PageContext 类的实例
Session对象
-
Web 服务器为单个用户发送的多个请求创建会话
-
存储有关用户会话的所有信息
-
javax.servlet.http.HttpSession 接口的实例
session 对象最常用的方法有:
-
void setAttribute(String name,Object value)
以键/值的方式,将一个对象的值存放到session 中
-
void getAttribute(String name)
根据名称去获取session中存放对象的值
application对象
- 表示 JSP 页面所属的应用程序
- 应用程序的 JSP 页面组合起来形成一个应用程序
- javax.servlet.ServletContext接口实例
application对象最常用的方法有:
- void setAttribute(String name,Object value)
以键/值的方式,将一个对象的值存放到application中 - void getAttribute(String name)
根据名称去获取application中存放对象的值
案例代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>作用域对象的使用(pageContext request session application)</title>
</head>
<body>
<body>
<%
pageContext.setAttribute("pagekey", "pageContent");
request.setAttribute("requestkey", "requestcontent");
session.setAttribute("sessionkey", "sessionContent");
application.setAttribute("applicationkey", "applicationContent");
%>
<%
String pageContent =(String) pageContext.getAttribute("pagekey");
out.print("pageContext:" + pageContent);
%>
<%
String requestContent = (String) request.getAttribute("requestkey");
out.print("request:" + requestContent);
%>
<%
String sessionContent = (String) session.getAttribute("sessionkey");
out.print("session:" + sessionContent);
%>
<%
String applicationContent = (String) application.getAttribute("applicationkey");
out.print("application:" + applicationContent);
%>
<a href="next.jsp">下一个页面</a>
<%
request.getRequestDispatcher("next.jsp").forward(request, response);
%>
</body>
</html>
next.jsp页面代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
获得在pageContext.jsp页面的内容
<%
String pageContent =(String) pageContext.getAttribute("pagekey");
out.print("pageContext:" + pageContent);
%>
<br>
<%
String requestContent = (String) request.getAttribute("requestkey");
out.print("request:" + requestContent);
%>
<br>
<%
String sessionContent = (String) session.getAttribute("sessionkey");
out.print("session:" + sessionContent);
%>
<br>
<%
String applicationContent = (String) application.getAttribute("applicationkey");
out.print("application:" + applicationContent);
%>
</body>
</html>
为了更好理解pageContext对象、request对象、session对象、appliction对象的作用范围,显示完成一个计数器案例,代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>作用域对象(request session application)</title>
</head>
<body>
<body>
<%
if(pageContext.getAttribute("pageCount") == null){
pageContext.setAttribute("pageCount", 0);
}
if(session.getAttribute("sessionCount") == null){
session.setAttribute("sessionCount", 0);
}
if(application.getAttribute("applicationCount") == null){
application.setAttribute("applicationCount", 0);
}
// 计数
int pageCount = (Integer)pageContext.getAttribute("pageCount");
pageContext.setAttribute("pageCount", pageCount + 1);
// session
int sessionCount = (Integer) session.getAttribute("sessionCount");
session.setAttribute("sessionCount", sessionCount + 1);
// application
int applicationCount = (Integer) application.getAttribute("applicationCount");
application.setAttribute("applicationCount", applicationCount + 1);
%>
pageContext:<%=pageContext.getAttribute("pageCount")%><br>
session:<%=session.getAttribute("sessionCount")%><br>
application:<%=application.getAttribute("applicationCount")%>
</body>
</body>
</html>
3.5 Servlet相关内置对象(page对象、config对象)
-
JSP 引擎为每个 JSP 生成一个 Servlet
-
Servlet对象提供了访问 Servlet 信息的方法和变量
Servlet 对象包括
- page对象
- config 对象
3.6 exception对象
- exception对象用于处理 JSP 页面中的错误
- exception 对象用于访问执行 JSP 的过程中引发的异常
- exception 对象是 java.lang.Throwable 类的实例
案例代码如下:
makerror.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page errorPage="exception.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>exception对象使用</title>
</head>
<body>
<%
int a = 10;
a = a / 0;
%>
</body>
</html>
exception.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page isErrorPage="true" %>
<!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>Insert title here</title>
</head>
<body>
<%
String message = exception.getMessage();
out.print(message);
%>
</body>
</html>