文章目录
写在前面
用eclipse创建一个jsp项目后,eclipse会复制tomcat文件到工作空间下,这样导致原先下的tomcat和eclipse使用的tomcat是互不干扰的两个服务器,修改原先下的tomcat配置文件不会影响eclipse项目中的tomcat。
jsp的页面元素
- 脚本 Scriptlet
i. <% java代码,定义局部变量 %>
ii. <%! 定义全局变量,方法%>
iii.<%=输出表达式%> —>等价于 <% out.print(内容); %>
<% out.print("helloworld"); %>
<%! public void init() {} %>
<%="helloworld" %>
- html、css、js
- 指令
i.page指令:<%@page…%> 属性:language(jsp使用的脚本语言)、import(导入的包)、pageEncoding(jsp自身的编码 jsp–>java)、contentType(浏览器解析的编码)
<%@page import = "java.util.*"%>
<%@page contentType="text/html;charset=utf-8"%>
<%@page language="java" %>
<%@page pageEncoding="utf-8" %>
ii.include指令:<%@include…%>
iii.taglib指令:<%@taglib…%>
- 注释
i.html注释:可以查看源码看到
ii.java注释 : 不会在网页源代码中显示
iii.jsp注释:<%-- 注释内容 --%> 不会在网页源代码中显示
jsp 9大内置对象
jsp自带的,不需要new的对象
out
向客户端输出内容
out.print("helloworld");
pageContext
request
请求对象;存储了“客户端向服务端发送的请求信息”
常见方法
- String getParameter(String name)
根据name取值,key–value - String[] getParameterValues(String name)
根据name取一组值 - void setCharacterEncoding(“utf-8”)
设置请求编码,默认根据tomcat的编码(7以前是iso-8859-1,8之后为utf-8) - getRequestDispatcher(“b.jsp”).forward(request,response)
请求转发,用于页面跳转 - SerletContext getServeContext()
获取项目的SerletContext()对象
response
响应对象,
常用方法:
- void addCookie(Cookie cookie);
服务端向客户端增加一个cookie对象 - void sendRedirect(String location) throws IOException;
重定向,页面跳转的一种方式 - void setContentType(String type)
设置服务端相应编码(设置服务端Content编码类型)
转发和重定向的区别:
转发 | 重定向 |
---|---|
request | response |
地址栏保留转发时的地址 | 地址栏变为重定向后的地址 |
数据不丢失 | 数据丢失 |
请求一次 | 请求两次 |
在服务端跳转 | 在客户端跳转 |
转发
重定向
cookie+ session
- session : 在服务端,内置对象
- cookie :在客户端,不是内置对象,需要new
cookie
有服务端产生,发送给客户端保存,作本地缓存,将一些数据存放在本地,可以提高访问服务器效率,但是安全性较差。
里面有键值对:name–value
建议cookie中不保存中文,否则要编码和解码
方法:
- public Cookie(String name,String value);
- String getName()
获取name - String getValue()
获取value - void setMaxAge(int expiry)
设置最大有效期(单位:秒)
服务端发送cookie
1.response.addCookie(Cookie cookie)
2.页面跳转(转发,重定向)
客户端获取cookie
request.getCookies()【只能一次性把cookie全拿到】
session
会话:打开一个网站到关闭网页称为一次会话。
机制:
- 服务器会给每一个连接的客户端创建一个session。
- 当客户端第一次访问服务器时,客户端创建一个session。
- 这个session中有sessionId,服务器会将这个sessionId保存到name为JSESSIONID的cookie中
- 将这个cookie发送给客户端,用以区分不同的客户端。
- 当客户端再次访问时,可以直接寻找对应的session.
- session在同一个用户请求时共享
方法:
- String getId() : 获取sessionId
- boolean isNew() : 判断是否是 新用户(第一次访问)
- void invalidate() : 使session失效(退出登录、注销)
- void setAttribute(name ,value) : 存储信息
- Object getAttribute(name) : 获得信息
- void setMaxInactiveInterval(秒) : 设置最大有效 非活动时间
- int getMaxInactiveInterval() : 查看最大有效 非活动时间
- removeAttribute() : 删除某一信息
区别:
cookie | session |
---|---|
保存在客户端 | 保存在服务端 |
较不安全 | 较安全 |
保存String–String | 保存String – Object |
application
全局对象,可以获得全部信息。
- String getContextPath():获取当前路径的虚拟路径
- String getRealPath(“虚拟路径”):获取绝对路径(虚拟路径相对的绝对路径)
config
配置对象,(服务器配置信息)
page
当前JSP页面对象,相当于java中的this.
exception
异常对象
四种范围对象
- pageContext(一般会说page对象,不是九大内置对象的page对象):当前页面有效
- request:同一次请求有效(请求转发有效,重定向无效)
- session:同一次会话有效(关闭/切换浏览器无效)
- application:全局有效(整个项目有效,服务器重启无效)
共有的方法:
- Object getAttribute(String name):根据属性名,获取属性值
- void setAttribute(String name , Object obj):设置属性值(新增,修改)