1.1 会话管理
1.1.1 概念介绍
会话是指一个终端用户与交互系统进行通讯的过程,这种多次Http连接维护用户与同一个用户发出的不同请求之间关联的情况,我们称之为一个会话(session)
1.1.2 Cookie基本使用
- 存储在客户端的一小段数据,以key-value的形式存在
添加Cookie
//创建Cookie并写入到客户端Cookie ck = new Cookie("hobby","girl");//设置 有效期 ck.setMaxAge(300);//单位秒 //响应的时候,通过响应头写回到客户端resp.addCookie(ck);resp.getWriter().println("<h1>ok</h1>");
有效期为负数:不存储,关闭浏览器就无效了 正数:指定时间后过期 零:删除(立即删除)
读取Cookie
//读取Cookie Cookie[] cks = req.getCookies();if (cks!=null){for (Cookie ck :cks){System.out.println(ck.getName()+":"+ck.getValue());}}
每次客户端请求会主动携带Cookie
删除Cookie
//删除的原理: 重复发送一个同名的cooke客户端去,同时设置这个cooke过期时间为0Cookie cookie = new Cookie("hobby","");cookie.setMaxAge(0);resp.addCookie(cookie);resp.getWriter().println("delete ok");
1.2 域
1.2.1 request域
所谓的域就是一块存储空间,在Javaweb中本质是类似于一个Map集合
1.2.2 域的管理
```java
- setAttribute(String name, Objcet value) 存数据
- Object getAttribute(String name) 取数据
- removeAttribute( String name ) 移除
```
经验:通常这两种跳转的方式的选择:如果跳转前后不需要传递数据,可以重重定向,如果需要可以使用转发
1.2.3 HttpSession域
Servlet中会话管理(跟踪)使用的是HttpSession,其底层是依靠Cookie来实现的,利用Cookie来传递凭证。Session域,服务器为浏览器开辟的独立空间,每个浏览器独立的空间,用于保护用户的一些信息
1.2.4 Session生命周期
- 关闭浏览器Session失效
- 不关闭浏览器默认30分钟
- 调用API 注销
1.2.5 配置时间
<session-config><session-timeout>30</session-timeout></session-config>
1.2.6 管理HttpSession域的方法
登录成功后把用户信息存数据存到Session中
if(name.equals("admin")&&pwd.equals("123456")) {//登录成功,跳转到指定页面//存入Seesionreq.getSession().setAttribute("curr_name",name);resp.sendRedirect("/books.do");}else{//登录失败resp.sendRedirect("/page/error.html");}
其他页从Session取数据,如果有则证明登录过,没有则跳转到登录页面
//判断当前用户是否登录 if( req.getSession().getAttribute("curr_name")!=null ){...... 其他逻辑...... }else{resp.sendRedirect("/login.html");//跳转到登录页面}
1.2.4 全局作用域
所谓的域即是空间
- 用户保存数据空间
- Servlet三大作用域:
- Request域
- Session域
- Application全局作用域
- 是一个单例模式
- 使用方法
//获得全局作用域ServletContext context = req.getServletContext();//添加void setAttribute(String key,Object value);//获得Object getAttribute(String key);//删除void removeAttribute(String key)
- 其他API
//转发getRequestDispatcher("转发的url").forward(req,resp);//获得网站(上下文根路径)getContextPath() ;//获得相对路径的本决定路径。【用于上传文件】context.getRealPath("/相对路径") 获得相对路径的本决定路径。
1.3 图片验证
1.3.1 步骤
- 引入jar包
2.编写生成图片验证的Servleteasy-captcha-1.6.2-RELEASE.jar
package com.qfedu.servlet;import com.wf.captcha.SpecCaptcha;import com.wf.captcha.base.Captcha;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;//这是一个负责响应一个图片验证码的Servelt//你请求它,它就响应你一个图片验证码@WebServlet("/pic") public class CaptchaServlet extends HttpServlet {@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//落地实现,生成图片验证码SpecCaptcha captcha = new SpecCaptcha(120,40,4);//创建一个验证码//保存这个验证码到Session空间 谁的Session?当前请求的客户端的Sessionreq.getSession().setAttribute("code",captcha.text());captcha.out( resp.getOutputStream() ); //把这个图片响应个浏览器。}}
1.3.2 网上获取图片验证码
<form action="/login.do" method="post"><input type="text" name="un" placeholder="输入账号"/><input type="password" name="pw" placeholder="输入密码"/><input type="text" name="code" placeholder="验证码"><img src="/pic" onclick="javascript:this.src='/pic?t='+Math.random()" ><input type="submit" value="登录"></form>
1.3.3 登录处理Servlet是否正确
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding("utf-8");resp.setCharacterEncoding("utf-8");resp.setContentType("text/html;charset=utf-8");String name =req.getParameter("un");String pwd= req.getParameter("pw");//用户输入的 验证码String code= req.getParameter("code");//正确的验证码? 从Session中取String relCode = req.getSession().getAttribute("code").toString();if( relCode.equalsIgnoreCase(code) ){System.out.println("正确"); //其他逻辑比如验证账号密码 }else{System.out.println("错误");}}
