1.会话跟踪技术概述
http协议是无状态的,所以服务器不知道每次请求是不是一个客户端
会话跟踪技术:解决一个客户端多次请求的数据共享问题
2.Cookie基本使用
发送Cookie
Cookie cookie=new Cookie("username","zs");
response.addCookie(cookie);
获取Cookie
注意,我们之前写的("username","zs")
这个意思是,建立一个cookie,它的名称name是username,值value是zs
Cookie[] cookies = request.getCookies();
for(Cookie cookie:cookies){
String name=cookie.getName();
if("username".equals(name)){
String value=cookie.getValue();
System.out.println(name+":"+value);
}
}
3.Cookie原理
4.Cookie细节
注意setMaxAge是关闭浏览器后,重新开,才能看出来效果。而且setMaxAge()括号里面是秒
存中文:不能直接存储中文,否则会报错。所以用URL先编码再解码
在发送cookie的时候进行URL编码
String name="张三";
name=URLEncoder.encode(name,"utf-8");
Cookie cookie=new Cookie("username",name);
在接收的时候进行URL解码
String value=cookie.getValue();
value=URLDecoder.decode(value);
System.out.println(name+":"+value);
5.Session基本使用
通过session来共享数据 ,主要就是三个点:获取session对象,通过session对象存取数据,通过session对象获取数据
获取session:
//1. 获取Session对象
HttpSession session = request.getSession();
System.out.println(session);
存储数据:
//2. 存储数据
session.setAttribute("username","zs");
获取数据:
//2. 获取数据
Object username = session.getAttribute("username");
System.out.println(username);
6.session的原理
一次会话的多次请求之间,都是同一个session
7.Session使用细节
session的销毁:
session.invalidate();
小结
cookie和session都是用来解决会话内多次数据共享的
用户没有登录;要长期 用cookie
案例---登陆注册
用户登录
UserService那里的return 不要忘记把null改回来!!!!!!!
基本就是先写Usermapper接口,然后用UserService重写接口里面的方法。接着写login.jsp登录界面,然后写跳转到的loginServlet,用request可以读取jsp里面获得的数据,然后再调用service里面的方法
比较难写的就是web里面的servlet
以下代码封装了session,用response。重定向,不能进行数据共享,所以要用session封装,这样重定向后,才能在服务器里面读取到信息
HttpSession session = request.getSession();
session.setAttribute("user",login);
String contextPath = request.getContextPath();
response.sendRedirect(contextPath+"/selectAllServlet");
以下代码是request进行转发,这个是可以进行数据共享的,所以就不用用session进行封装传输数据了
request.setAttribute("login_msg","用户名或密码错误");
request.getRequestDispatcher("/login.jsp").forward(request,response);
记住用户
获取jsp的信息:getPramter()里面填的是name,获取的是value值
存储cookie的步骤:1.判断是否为null 2.勾选了复选框
String remember = request.getParameter("remember");
if(login!=null){
//登录成功,跳转到查询所有对象的,没有数据共享,所以直接重定向就好了
//动态获得
if("1".equals(remember)){
Cookie c_username = new Cookie("username", username);
Cookie c_password = new Cookie("password", password);
c_username.setMaxAge(60*60*24);
c_password.setMaxAge(60*60*24);
response.addCookie(c_username);
response.addCookie(c_password);
}
System.out.println("()()()()");
HttpSession session = request.getSession();
session.setAttribute("user",login);
String contextPath = request.getContextPath();
response.sendRedirect(contextPath+"/selectAllServlet");
}
获取cookie后,会存在浏览器里面。下次可以获取cookie中的数据, 把它存在jsp中(jsp可以调用cookie中的数据)
用户注册
一样的步骤:先在mapper里面写好接口,再在userservice里面写好register方法,然后写register.jsp,再跳转到registerservlet
在web的Servlet里面,读取jsp的传来的值,然后调用service里面的方法,写好对应的业务
验证码
验证码的展示
导入现成的一个包,写以下main函数即可。这样依赖随机生成4位验证码,并且写道s://a.jpg里面
public static void main(String[] args) throws IOException {
OutputStream fos=new FileOutputStream("d://a.jpg");
String s = CheckCodeUtil.outputVerifyImage(100, 50, fos, 4);
System.out.println(s);
}
每次生成的不一样,是随机的。这只是生成验证码图片,我们还要把它放在页面上
在register.jsp里面放一个验证码标签,把img路径写在checkServlet里面,然后把刚刚上面的d://a.jpg改成字节输出流,这样以后就能写到电脑屏幕上面了
<tr>
<td>验证码</td>
<td class="inputs">
<input name="checkCode" type="text" id="checkCode">
<img src="/brand-demo//checkCodeServlet">
<a href="#" id="changeImg">看不清?</a>
</td>
</tr>
ServletOutputStream fos= response.getOutputStream();
String s = CheckCodeUtil.outputVerifyImage(100, 50, fos, 4);
接着处理点击换一张的业务,用script写onclick方法,每次点击后,给图片重新获取歌路径,这样重新跑一次,就换了一个新的图片,而且要让每次的图片不一样,所以在后面加上时间(其实这一块的逻辑还是没怎么懂。。。。。)
<script>
document.getElementById("changeImg").onclick = function(){
document.getElementById("checkCode").src="/brand-demo//checkCodeServlet"+new Date().getMilliseconds();
}
</script>
校验验证码:
生成的验证码存入session中。获取用户输入的验证码。然后对比两者即可
HttpSession session = request.getSession();
session.setAttribute("c",s);
String ch = request.getParameter("checkCode");
HttpSession session = request.getSession();
String c = (String) session.getAttribute("c");
突然意识到,jsp里面标签的name是给web使用的,id是给jsp使用的吗?????
一般输入正确或者错误,都是先输出啥,然后跳转到哪
注意!号啊,我哭死
if(!c.toString().equalsIgnoreCase(ch)){
request.setAttribute("register_msg","验证码错误");
request.getRequestDispatcher("/register.jsp").forward(request,response);
}