JSP九大内置对象
1, out :输出对象,向客户端输出内容。
2, request: 请求对象,存储“客户端向服务端发送的请求信息”。
request对象的常见方法:
1> String getParameter(String name): 根据请求的字段名key, 返回字段值value。
2> String[ ] getParameterValues(String name): 根据请求的字段名key, 返回多个字段值value(checkbox)。
3> setCharacterEncoding(“编码格式utf-8”):设置请求编码。
4> getRequestDispatcher(“b. jsp”).forward(request, response) : 请求转发方式跳转页面 (A->B)
5> ServletContext getServerContext() : 获取项目的ServletContext对象
示意图:
示例:
注册
register.jsp , show.jsp
1)register.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>
<form action="show.jsp" method="post">
用户名:<input type="text" name="uname"/><br/>
密码:<input type="password" name="upwd"/><br/>
年龄:<input type="text" name="uage"/><br/>
爱好:<br/>
<input type="checkbox" name="uhobbies" value="足球"/>足球,
<input type="checkbox" name="uhobbies" value="篮球"/>篮球,
<input type="checkbox" name="uhobbies" value="乒乓球"/>乒乓球<br/>
<input type="submit" value="注册">
</form>
</body>
</html>
2)show.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>
<%
//设置编码
request.setCharacterEncoding("utf-8");
String name = request.getParameter("uname");
int age = Integer.parseInt(request.getParameter("uage"));
String pwd = request.getParameter("upwd");
String[] hobbies = request.getParameterValues("uhobbies");
%>
注册成功,信息如下; <br/>
姓名:<%= name %><br/>
年龄:<%= age %><br/>
密码:<%= pwd%><br/>
爱好:<br/>
<%
if(hobbies != null){
for(String hobby : hobbies){
out.print(hobby+" ");
}
}
%>
</body>
</html>
解释:register.jsp向服务器发出请求,转换成为以下格式:
http://localhost:8080/MyJspProject/show.jsp?uname=ewe&uage=12&uhobbies=%E8%B6%B3%E7%90%83
http://localhost:8080/MyJspProject----- 连接, 即URL
show.jsp-----文件
uname=ewe-----参数名1=参数值1
uage=12------参数名2=参数值2
uhobbies=%E8%B6%B3%E7%90%83-----参数名3=参数值3
…
3) get提交方式:method=“get” 和 地址栏,超链接 请求方式 默认都属于get提交方式
…
4) get与post请求方式的区别:
a. get 方式 在地址栏显示 请求信息 (但是地址栏能够容纳的信息有限,报错), post 不会显示。
b. 文件上传操作,必须是post。 注:推荐使用post
…
5) 统一请求的编码 request
1, get方式请求 如果出现乱码,解决:
a. 统一每一个变量的 编码
new String( );
b. 更改配置文件server.xml, 一次性的更改tomcat默认get提交方式的编码(UTF-8), URIEncoding=“UTF-8”.
2, post方式请求 如果出现乱码,解决:
a. 设置编码格式:setCharacterEncoding(“编码格式utf-8”).
3, response: 响应对象
提供的方法:
1> void addCookie(Cookie cookie): 服务端向客服端增加cookie对象
2> void sendRedirect(String location) throws IOException: 页面跳转的一种方式
3> void setContetType(String type): 设置服务端响应的编码(设置服务端的contentType类型)
示意图:
1)请求转发和重定向的区别:
- 请求转发后地址栏没有改变,而重定向改变。
- 请求转发保留第一次请求时的数据,而重定向不保留(即数据丢失)。
- 请求转发的请求次数为1, 而重定向为2。
区别如图所示:
请求转发的方式
重定向的方式:
转发:张三(客户端)-> [ 服务窗口A(服务端)-> 服务窗口B ]
重定向: 张三(客户端)-> 服务窗口A (服务端)-> 去找 B
张三(客户端)-> 服务窗口B (服务端)-> 结束
2)Cookie(客户端,不是内置对象):Cookie是由服务端生成的, 再发送给客户端保存。
相当于 本地缓存的作用: 客户端(hello. mp4, zs/abc)->服务端(hello.mp4, zs/abc)
作用:提高服务端的效率,但是安全性较差。
形式:Cookie: name=value
包:javax.servlet.http.Cookie
常见方法有:
public Cookie(String name, String value); 创建Cookie对象
String getName(); 获取name
String getValue(); 获取value
void setMaxAge(int expiry); 最大有效期(秒)
服务端准备Cookie:
response.addCookie(Cookie cookie)
页面跳转(转发,重定向)
客户端获取Cookie:
request.getCookies();
4, session(服务端)会话对象
a, session存储在服务端。
b, session是在同一个用户请求时共享。
c, 实现机制:第一次客户请求时产生一个SESSIONID,并复制给cookie的JSESSIONID,然后发给客户端。最终通过cookie的JSESSIONID对应session的SESSIONID。
session方法:
String getId(); 获取sessionId
boolean isNew(); 判断是否是新用户(第一次访问)
void invalidate(); 使session失效(退出登录,注销)
void setMaxInactiveInterval(秒): 设置最大有效非活动时间
int getMaxInactiveInterval(): 获取最大有效非活动时间
5, application 全局对象
需要掌握的方法有:
String getContextPath() :虚拟路径
String getRealPath(): 绝对路径 ( 与虚拟路径相对应的绝对路径 )
例如:
虚拟路径为:/MyJspProject
则绝对路径为:C:\Program Files\Tomcat\apache-tomcat-8.5.69\wtpwebapps\MyJspProject\MyjspProject
以下的对象了解即可
6, config 配置对象(服务器配置信息)
7, out 输出对象
8, page 当前JSP页面对象(相当于java中的this)
9, exception 异常对象
10, 九大内置对象
其中四种范围对象(小->大)
pageContext JSP页面容器(page对象)
request 请求对象
session 会话对象
appliation 全局对象
以上4个对象共有的方法:
Object getAttribute(String name) :根据属性名,或者属性值
void setAttribute(String name, Object obj) :设置属性值(新增,修改)
void removeAttribute(String name): 根据属性名,删除对象
a. pageContext 当前页面有效(页面跳转后无效)
b. request 同一次请求有效;其他请求无效(请求转发后有效;重定向后无效)
c. session 同一次会话有效(无论怎么跳转,都有效;关闭/切换浏览器后无效)
d. application 全局变量;整个项目运行期间都有效(关闭服务或其他项目无效)
以上对象,对象的范围越大,造成的性能损耗越大