1.什么是JSP内置对象
JSP内置对象是 Web 容器创建的一组对象
2.常用的JSP内置对象
(1)out:
(2)request:请求(request对象主要用于处理客户端请求)
(3)response:响应
(4)session:会话
(5)application:
3.request对象常用方法
方法名称 说明String getParameter(String name)根据表单组件名称获取提交数据String[ ] getParameterValues(String name)获取表单组件对应多个值时的请求数据 void setCharacterEncoding(String charset)指定每个请求的编码RequestDispatcher getRequestDispatcher(String path)返回一个RequestDispatcher对象,该对象的forward( )方法用于转发请求
4.学员注册页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<style>
.container {
width: 800px;
margin: 20px auto;
}
</style>
<body>
<div class="container">
<form action="reginfo.jsp" method="post">
<table style="text-align:left;">
<tr>
<td>请输入注册信息</td>
</tr>
<tr>
<td><label>用户名</label> <input name="username"></td>
</tr>
<tr>
<td><label>密码: </label> <input type="password"
name="pwd"></td>
</tr>
<tr>
<td><label>信息来源:</label><br>
<input type="checkbox" name="channel" value="报刊">报刊
<input type="checkbox" name="channel" value="网络">网络
<input type="checkbox" name="channel" value="朋友推荐">朋友推荐
<input type="checkbox" name="channel" value="电视">电视</td>
</tr>
<tr>
<td><input type="submit" value="提交"/>
<input type="button" value="取消"/>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*,java.text.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<%
//请求中文编码设置成utf-8 设置服务器端编码
request.setCharacterEncoding("utf-8");
//响应页面中文编码设置成UTF-8,文件类型是HTML 设置浏览器端解码
response.setContentType("text/html;charset=utf-8");
//获取表单提交的元素
//获取用户名
String username = request.getParameter("username");
//获取密码
String pwd = request.getParameter("pwd");
//获取复选框的内容
String[] channel = request.getParameterValues("channel");
out.println("用户名:"+username);
out.println("密码:"+pwd);
out.println("信息来源:");
for(String str:channel){
out.println(str+"\t");
}
%>
</body>
</html>```
5.response对象
response对象用于响应客户请求并向客户端输出信息
![image.png](https://ucc.alicdn.com/pic/developer-ecology/b39f20f66dac4d09af9635c43505d28a.png)
6.页面重定向:request.sendRedirect();
void sendRedirect(String location)
客户端将重新发送请求到指定的URL
示例:实现登录验证,并在验证成功后跳转至欢迎页面
<%
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("userName");
String pwd = request.getParameter("pwd");
if ("sa".equals(name) && "sa".equals(pwd))
response.sendRedirect("welcome.jsp");
%>
7.请求的转发(request.getRequestDispatcher)
(1)转发的作用
在服务器端,将请求发送给服务器上的其他资源,以共同完成一次请求的处理
(2)转发的实现
RequestDispatcher对象的forward()方法
示例:(在多个页面交互过程中
请求中的数据可以共享
)
<%
RequestDispatcher rd = request.getRequestDispatcher("welcome.jsp");
rd.forward(request, response);
%>
8.小结(转发可带参数对象,重定向不可以)
![image.png](https://ucc.alicdn.com/pic/developer-ecology/248ed3e41c1b4b689540f2dbd04ae025.png)
9.session对象常用方法:
方法名称说明String getId()获取sessionidvoid setMaxInactiveInterval(int interval)设定session的非活动时间int getMaxInactiveInterval()获取session的有效非活动时间(以秒为单位)void invalidate()设置session对象失效void setAttribute(String key, Object value)以key/value的形式保存对象值Object getAttribute(String key)通过key获取对象值 void removeAttribute(String key)从session中删除指定名称(key)所对应的对象
10.session与窗口的关系
每个session对象都与一个浏览器窗口对应 ,重新开启一个浏览器窗口,可以重新创建一个session对象(不同版本浏览器可能有所差别)
通过超链接打开的新窗口,新窗口的session与其父窗口的session相同
![image.png](https://ucc.alicdn.com/pic/developer-ecology/1d42740eef6948988a31267199b635c0.png)
![image.png](https://ucc.alicdn.com/pic/developer-ecology/10a2c88b28824994a87a8d1eb801065f.png)
![image.png](https://ucc.alicdn.com/pic/developer-ecology/94c57e2bab044bd3835a469f815428c2.png)
11.案例:
![image.png](https://ucc.alicdn.com/pic/developer-ecology/4ae6403fd56d40408bfcb2686d30e690.png)
![image.png](https://ucc.alicdn.com/pic/developer-ecology/1a98eb3f4d4843b1a083ea5fece9158f.png)
![image.png](https://ucc.alicdn.com/pic/developer-ecology/5437e115aa074ef09fb08b2133a7681d.png)
![image.png](https://ucc.alicdn.com/pic/developer-ecology/34f7cc400cc34e1ab251401619eb8734.png)
![image.png](https://ucc.alicdn.com/pic/developer-ecology/d3bfd96e92ec41f0b0a0f99086a38a9a.png)
注意下图会出现一个小bug,如图所示:(需要进行操作之后才会将null部分更换),如果改为上图的三元二次表达式就不会出现此问题了
![image.png](https://ucc.alicdn.com/pic/developer-ecology/71ddc08237fc4e2cb87c28cb8717e4c5.png)
![image.png](https://ucc.alicdn.com/pic/developer-ecology/65b7ab850814449a8876c5e866248c89.png)
![image.png](https://ucc.alicdn.com/pic/developer-ecology/57f8e9ef5c4f497d8466ca095da70e3d.png)
![image.png](https://ucc.alicdn.com/pic/developer-ecology/5e415972875d4df08255f2bf1365e3d4.png)
![image.png](https://ucc.alicdn.com/pic/developer-ecology/ee970483b7614f93b3dcc5d97f54043b.png)
![image.png](https://ucc.alicdn.com/pic/developer-ecology/6c90d22eb72a4d0db02d5f365ca5ef1b.png)
![image.png](https://ucc.alicdn.com/pic/developer-ecology/3b4f3ae4aa8f47ffa46ada022ed57fa5.png)