JSP 9大内置对象
-
request: 请求对象;存储“客户端向服务器端发送的请求信息”。
- 常见方法:
- String getParameter(String name) : 根据字段名key (input标签的name属性) ,返回字段value (input标签的value属性值)
- String[] getParameterValues(String name) : 根据字段名key ,返回多个字段值value (checkbox)
- void setCharacterEncoding(" 编码格式utf-8"):设置请求编码(tomcat7以前默认iso-8859-1,tomcat8以后默认utf-8)
- getRequestDispatcher(“b.jsp”).forward(request,response):请求转发的方式跳转页面 a->b
- getServerContext():获取项目的ServerContext对象
-
response: 响应对象
- 常见方法:
- void addCookie(Cookie cookie) : 服务端向客户端增加一个Cookie对象
- void sendRedirect(String location): throws IOException 重定向
- void setContentType(String type):设置服务端响应的编码(设置服务端的content)
-
session(服务端)
-
这里引入Cookie做对比
-
Cookie(客户端,不是内置对象):是由服务端产生,再发送给客户端保存。相当于本地缓存的作用。提高访问服务端的效率,但是安全性较差。
-
Cookie name — value
-
public Cookie(String key ,String value)
-
String getName()
-
String getValue()
-
void SetMaxAge(int expiry) 最大有效期(秒)
-
服务端准备Cookie
response.addCookie(Cookie cookie)
页面跳转(转发,重定向)
客户端获取Cookie:request.getCookies()
-
-
session: 会话
-
客户端第一次请求服务端时,服务端会产生一个session对象(用于保存客户信息),并且每个session对象,都会有一个唯一的sessionID(用于区分其他session),服务端会产生一个cookie,并且cookie的name=JSESSIONID,value=服务端sessionID的值,然后服务端会在响应客户端时把cookie发给客户端,实现一一对应。
-
客户端第二次请求服务端时,会先匹配Cookie中的JSESSIONID和session中的sessionID
-
String getId():获取sessionId
-
boolean isNew():判断是否是第一次访问
-
void invalidate():使session失效(退出登录,注销)
-
void setAttribute()
-
Object getAttribute()
-
void setMaxInactiveInterval(秒):设置最大有效非活动时间
-
void setMaxInactiveInterval():获取最大有效非活动时间
-
-
-
application 全局对象
- String getContextPath() 虚拟路径
- String getRealPath() 绝对路径
-
config 配置对象
-
page 当前jsp页面对象(相当于java中的this)
-
pageContext JSP页面容器
-
exception 异常对象
-
out: 输出对象,向客户端输出内容
四种范围对象
-
pageContext JSP页面容器 当前页面有效,跳转后无效
-
request 请求对象 同义词请求有效,其他请求无效(转发后有效,重定向无效)
-
session 会话对象 同一次会话有效(无论怎么跳转,关闭/切换浏览器无效)
-
application 全局对象 整个项目运行期间都有效
Object getAttribute(String name):根据属性名,获得属性值
void setAttribute(String name,Object obj) :设置属性值(新增,修改)
void removeAttribute(String name) :根据属性名删除对象
EL表达式
<%@ page import="com.edu.entity.Student" %><%--
Created by IntelliJ IDEA.
User: 14327
Date: 2020/12/1
Time: 14:03
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
Student student = (Student)request.getAttribute("student");
out.print(student.getSno()+"<br/>");
out.print(student.getSname()+"<br/>");
out.print(student.getAddress().getHomeAddress()+"<br/>");
out.print(student.getAddress().getSchoolAddress()+"<br/>");
%>
----------------EL--------------<br>
${requestScope.student} <br>
${requestScope.student.sno} <br>
${requestScope.student.sname} <br>
${requestScope.student.address.homeAddress} <br>
${requestScope.student.address.schoolAddress} <br>
-------------[""]或者['']运算符 <br>
${requestScope.student.address.homeAddress} <br>
${requestScope.student.address["schoolAddress"]} <br>
----------获取map属性------------ <br>
${requestScope.map.cn}
${requestScope.map["us"]} <br>
----------运算----------- <br>
${3>2}、${3 gt 2}、${3>2 || 3<2} <br>
-----------Empty运算符--不存在或者为null为true,存在为false-- <br>
${empty requestScope.hello}
${empty requestScope.student}<br>
---------隐式对象,略----------
</body>
</html>
JSTL
- 引入jar包
<!--jstl-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
- jsp页面引入lib设置前缀为c
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
核心标签库:
-
通用标签库
- c:set 1. 作用域(4个范围对象)中给某个变量赋值
<% request.setAttribute("name","zhangsan"); %> <%--等价于--%> <c:set var="name" value="zhangsan" scope="request"/>
2. 在某个作用域中给对象的属性赋值 不能指定scope
====给对象属性赋值=== <br> ${requestScope.student.sname} <br> <c:set target="${requestScope.student}" property="sname" value="zxs" /> ${requestScope.student.sname} <br> ====给map对象赋值=== <br> ${requestScope.map.cn} <br> <c:set target="${requestScope.map}" property="cn" value="hello" /> ${requestScope.map.cn} <br>
- c:out 输出
=====c:out====== <br> ${requestScope.student} <br> <c:out value="${requestScope.student}"/> <br> 显示不存在数据显示默认值<c:out value="${requestScope.stu}" default="22-xx"/> 设置为false可以编译html代码 <br> <c:out value='<a href="https://www.baidu.com">百度</a>' escapeXml="true"/> <br> <c:out value='<a href="https://www.baidu.com">百度</a>' escapeXml="false"/> <br>
- c:remove 删除属性
<c:remove var="a" scope="request"/>
-
条件标签库
- c:if
<c:if test="${10>2}" > 真 </c:if> <br> <%--var 判断结果放的位置 可以加scope="request"表示放到request域--%> <c:if test="${10>2}" var="result"> 真 ${result} </c:if>
-
c:chose 类似于switch
-
注意test后面不能有空格
-
test="${10>2}" true
-
test="${10>2} " 非true
<c:set var="role" value="学生" scope="request"></c:set>
<c:choose>
<c:when test="${requestScope.role == '老师'}"> 老师代码</c:when>
<c:when test="${requestScope.role == '学生'}"> 学生代码</c:when>
<c:otherwise>管理员等其他</c:otherwise>
</c:choose>
-
迭代标签库
- c:forEach
<c:forEach begin="0" end="5" step="1" varStatus="status"> ${status.index} test </c:forEach> <br> <c:forEach var="name" items="${requestScope.names}"> ${name} </c:forEach>