文章目录
会话技术
购物车案例引出会话对象
Cookie技术
在客户端保存的数据,数据产生是在服务器
在浏览器中Cookie是以文本的形式保存数据
服务器向浏览器发送Cookie
- Cookie对象,直接new Cookie(String key,String value)
- 将Cookie数据保存到客户端 response对象方法 addCookie()
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/**
* 创建Cookie对象,构造方法传递键值对
* 响应对象response,将Cookie响应回浏览器
*/
Cookie cookie = new Cookie("heima","java");
response.addCookie(cookie);
}
获取浏览器携带的Cookie
浏览器在访问服务器的时候携带Cookie数据,服务器端使用request对象获取Cookie
Cookie数据,会放在请求头
- request对象方法 Cookie[] getCookies() 获取多个Cookie,返回的是Cookie对象数组
- Cookie对象方法 getName()获取Cookie的键, getValue()获取Cookie中的值
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/**
* 获取客户端浏览器携带的Cookie数据
* request对象方法 getCookies()
*/
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies){
//遍历数组,取出的是数组中的每个Cookie对象
//取出Cookie中的键
String key = cookie.getName();
//取出Cookie中的值
String value = cookie.getValue();
System.out.println(key+"==="+value);
}
}
Cookie中使用中文问题(扩展)
Cookie数据,值可以使用中文,建议不要使用中文
在Cookie使用中文,Tomcat低版本不支持,从Tomcat8开始支持中文
你好-> %AE%3A%CD
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = "哈哈";
name = URLEncoder.encode(name,"utf-8");
System.out.println(name);
Cookie cookie = new Cookie("haha",name);
response.addCookie(cookie);
Cookie[] cookies = request.getCookies();
for (Cookie cookie2: cookies){
String value = URLDecoder.decode( cookie2.getValue(),"utf-8");
System.out.println(cookie2.getName()+"=="+value);
}
}
Cookie的携带路径
浏览器访问服务器,每次都会携带Cookie进行访问吗
Cookie数据放在请求头,每次浏览器请求服务器,都会携带吗
- 结论: 浏览器携带Cookie,在Cookie产生的路径下
- Cookie的产生路径: /abc下产生的
- 浏览器访问/abc下的任意资源,携带Cookie
- 访问不是/abc下的资源,不会携带Cookie
- 需求: 只要访问WEB应用下的任意资源都携带Cookie
- Cookie对象方法 setPath()设置携带路径
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie cookie = new Cookie("heima","java");
//设置Cookie的携带路径,WEB应用名称,不要写死的
cookie.setPath( request.getContextPath());
response.addCookie(cookie);
}
Cookie的生存时间
浏览器中Cookie是有生存时间,默认是当前会话.浏览器关闭,会话结束
设置生存时间,Cookie对象方法 setMaxAge(int 秒)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie cookie = new Cookie("heima","java");
cookie.setPath(request.getContextPath());
//设置生存时间
cookie.setMaxAge(60);
response.addCookie(cookie);
}
记录上一次的访问时间
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
//获取客户端携带的Cookie
Cookie[] cookies = request.getCookies();
//获取当前时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
String time = sdf.format(new Date());
//判断数组
if (cookies==null){
//没有数据,第一次访问
response.getWriter().write("欢迎第一次访问");
//当前时间,存储到Cookie中
Cookie cookie = new Cookie("time",time);
cookie.setMaxAge(60*10);
cookie.setPath(request.getContextPath());
response.addCookie(cookie);
}else {
//有数据,获取cookie中存储时间
for(Cookie cookie :cookies){
String key = cookie.getName();
String value = cookie.getValue();
response.getWriter().write("上次访问时间是:"+value);
Cookie cookie2 = new Cookie("time",time);
cookie2.setMaxAge(60*10);
cookie2.setPath(request.getContextPath());
response.addCookie(cookie2);
}
}
}
Session域对象
HttpSession接口,session对象是接口的实现类,实现类对象tomcat引擎创建
方法 request.getSession()获取session对象
作用域 : 一次会话有效,浏览器不关闭
- 域对象存储数据: setAttribute(String key,String value)
- 取出域对象数据: Object getAttribute(String key)
- 移除域对象数据: removeAttribute(String key)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/**
* 获取session对象
* 域中存储和取出数据
*/
HttpSession session = request.getSession();
session.setAttribute("heima","java");
Object value = session.getAttribute("heima");
System.out.println("session1="+value);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Object value = request.getSession().getAttribute("heima");
System.out.println("session2="+value);
}
持久化Session对象
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.setAttribute("testSession","testSession");
//session方法,getId() 获取session对象的唯一编码,返回String
String id = session.getId();
System.out.println(id);
//id值,存储到Cookie中
Cookie cookie = new Cookie("JSESSIONID",id);
cookie.setMaxAge(60*10);
cookie.setPath(request.getContextPath());
response.addCookie(cookie);
}
session生命周期
session域对象生命时候生,什么时候销毁
- session对象什么时候创建
- request.getSession()方法,Cookie中id和服务器的id匹配不上就创建
- session对什么时候销毁
- 默认30分钟销毁, tomcat全局配置文件web.xml
- 调用方法session.invalidate();
- 关闭 (非正常关闭) 服务器
如何清空Cookie
- 覆盖的方式情况
- 保证同名键
- 保证相同的携带路径
- setMaxAge(0)
验证码案例
验证码本质是图片,图片里面是随机生成的字符串
字符串存储在Session域对象
客户端浏览器填写验证码(String),提交服务器Servlet
取出验证码,取出session域中的验证码, 比较
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
//获取页面填写验证码
String code = request.getParameter("code");
//session域对象取出存储验证码
String code_session = (String) request.getSession().getAttribute("code_session");
if(code_session.equalsIgnoreCase(code)){
response.getWriter().write("验证码正确");
}else {
response.getWriter().write("验证码错误");
}
}
<form method="post" action="/WEB05/check">
<input type="text" name="code">
<img src="/WEB05/code" onclick="fnChange()" id="code">
<input type="submit">
</form>
<script type="text/javascript">
function fnChange() {
/*
* 页面打开的时候,请求服务器资源/WEB03/code
* 点击图片js函数中,发了请求/WEB03/code
*
* 请求的资源没有变化,服务器端程序也没有变化
* 浏览器拿缓存吧
*
* 每次请求不一样就行,添加参数
*/
//点击图片,修改src的属性值
var code = document.getElementById("code");
var date = new Date().getTime();
code.src="/WEB05/code?a="+date;
}
</script>
JSP技术
JSP称为Java的动态服务器端网页技术,(Java Server Page).
Java程序直接嵌入到了HTML中,页面称为jsp页面
Java嵌入HTML方式 <% %> <%= %> <%! %>
JSP页面的执行原理
jsp页面,执行的时候被转成.java文件,编译为.class
转换后目录:
C:\Users\shisong\.IntelliJIdea2017.3\system\tomcat\Tomcat_8_5_32_heima331\work\Catalina\localhost\WEB05\org\apache\jsp\jsp
C:\apache-tomcat-8.5.32\work\Catalina\localhost\WEB05\org\apache\jsp\jsp
JSP本质就是Servlet
JSP中的注释
- JSP中存在,翻译后的.java存在,浏览器中存在
-
<% // /* Java自己注释%> JSP中存在,翻译后的.java存在,浏览器没有
-
<%-- JSP特有注释 --%> 只在JSP源代码中出现
JSP九大内置对象
- request
- response
- ServletContext对象,在JSP写对象,只能写 application
- ServletConfig对象,在JSP写对象,只能写config
- session
- out
- page
- pageContext 最小域对象,作用范围是当前页面
- exception异常