05- Cookie
又开始重温jsp的用法,好久以前了解了都忘了好多真实一不碰就会忘那种,今天又又双整理一遍笔记淦。
01- 用途
- 服务器,使用,Cookie 来跟踪客户端状态;
- 保存购物车,(他是一个用户向服务器发送的多个请求信息)不能用request保存
- 显示上交登录名(也是一个用户多个请求);
02- setCookie & getCookie
<!--=============================add.jsp ==============================-->
<%
response.addCookie(new Cookie("cookie01","cookie_value01"));
response.addCookie(new Cookie("cookie02","cookie_value02"));
%>
<!--=============================get.jsp ==============================-->
<%
Cookie[] res = request.getCookies();
if(res!=null){
for(Cookie ans:res){
out.print(ans.getName()+","+ans.getValue()+"\r\n");
}
}
%>
03- Cookie 的生命?
Cookie 中的 maxAge
利用Google 查看当前 localhost 的cookie 键值;
(截图工具不是很好有些模糊…我裂开来,)
04- 路径path
包含就带,( 访问路径是否包含cookie 路径 )如图:
05- Cookie 的 域(domain)
跨域?
cookie. setDomain( “. baidu.com” ); e.g.
06- HttpSession
-
httpSession 底层 依赖 Cookie,就是url 重写
-
是 servlet 三大域对象之一,也包括,(set,get,remove)-Attribute();
-
一个会话内,一个session;会话范围:首次访问到该用户关闭浏览器;
07- 练习
用户登录
<!-- 登录成功的界面 -->
<body>
<%
String username = (String)session.getAttribute("username");
if(username==null){
request.setAttribute("msg","you are not here,please");
request.getRequestDispatcher("/for_Jsp/login/login_session.jsp").forward(request,response);
return;
}
%>
<h1>successfully login-ing</h1>
<span style="color: red;font-size: 3rem;"><%=username%></span><span style="font-size: 1.5rem;">,登录成功!</span>
</body>
<!-- 登录入口 -->
<body>
<form action="<c:url value="/login2Servlet"/>" method="post">
<label for="name">用户名:</label><input type="text" name="username" placeholder="input your name" id="name" />
<br>
<label for="pwd">password:</label><input type="password" name="password" placeholder="input your name" id="pwd" />
<br>
<button type="submit">submit</button>
</form>
are you register? <br /><span style="color: red;">${ msg }</span>
</body>
// servlet:
// post method: for the form submited and get the data(username);
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//
String username = request.getParameter("username");
String password = request.getParameter("password");
if("Jason".equalsIgnoreCase(username)){
HttpSession session = request.getSession();
session.setAttribute("username",username);//
response.sendRedirect("/01_demo/for_Jsp/login/successfulylogin01.jsp");
}else{
request.setAttribute("msg","the user is not here");
request.getRequestDispatcher("/for_Jsp/login/login_session.jsp").forward(request,response);
}
测试:直接访问成功界面:
(这里直接截图的我的markdown部分hh)
也可以换成Cookie 来存储用户数据,并设置用户数据的保存时间(maxAge());并在登录input 的value 值中添加cookie判断;方便下次登录时可以直接显示登录名
08- HttpSession 原理
-
request. getSession():
- 获取 sessionId 中的 JSESSIONID:
- sessionId 不存在,创建session对象;
- 存在,找不到,创建
- 存在,找到了,不创建
- 获取 sessionId 中的 JSESSIONID:
-
其他方法:
-
String getId():获取 sessionId;
-
int getMaxInactiveInterval():获取,session的最大不活动时间;默认30min;
-
void invalidate():让session 失效;
-
Boolean isNew():查看 session 是否 为新;
09- session 最大不活动时间
首先了解:uuid:
package UUID_Utils;
import org.junit.Test;
import java.util.UUID;
public class uuidUtils {
private static String Get_uuid(){
return UUID.randomUUID().toString().replace("-","").toUpperCase();
}
@Test
public void func(){
System.out.println(uuidUtils.Get_uuid());
}
}
在web. xml中配置:
<session-config>
<session-timeout>xxx(min)</session-timeout>
</session-config>
10- url 重写;sessionId
JSESSIONID ?把Cookie,变为请求参数?
response.encodeURL("url");// 如果没有id,则url后面会添加,有,则直接输出且不添加;
11- 图形验证码
动态生成图片:
package yanzhengma;
import org.junit.Test;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
public class yanzhengma01 {
@Test
public void func(){
BufferedImage bi = new BufferedImage(100,50,BufferedImage.TYPE_3BYTE_BGR);//
Graphics2D pen = (Graphics2D)bi.getGraphics();// get the pen to drawing;
pen.setColor(Color.WHITE);//
pen.fillRect(0,0,100,50);// filled the rectangle;
pen.setColor(Color.RED);
pen.drawString("hello",10,40);//
try {
ImageIO.write(bi,"png",new FileOutputStream("D:\\web前端开发\\JavaWeb_demo\\01_demo_\\src\\yanzhengma\\hello.png"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
最后运行测试一哈: