http
无状态的协议,每次请求都是独立的我们不能存储第一次访问的数据
Cookie
实现访问请求的数据保存
将服务器中的一小段,存入浏览器中放在浏览器中的cookie中,是存入浏览器中。
优点:减少服务器的压力
缺点:不安全,存储的数据单一,只能为字符串,可以通过路由器获得所有的cookie
1. 添加cookie
2. 获得cookie
3. 解决中文乱码问题
4. 修改cookie的两种方式(cookie为键值对,key值不能重复,或者cookie.setvalues())
5. 生命周期
(1)默认为添加到浏览器和关闭浏览器
(2)设置setMaxAge(>0)
package cn.jiedada.controller; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLDecoder; import java.net.URLEncoder; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("*.cookie") public class CookieServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获得uri String uri = req.getRequestURI(); //输出类型 resp.setContentType("text/html;charset=utf-8"); //判断请求 uri=uri.substring(uri.indexOf("/")+1, uri.indexOf(".")); if(uri.equals("add")){ doAdd(req,resp); }if(uri.equals("find")){ doFind(req,resp); } } private void doFind(HttpServletRequest req, HttpServletResponse resp) throws IOException { Cookie[] cookies = req.getCookies(); //获得cookie for (Cookie cookie : cookies) { String name = cookie.getName(); String value = cookie.getValue(); //解码方式 value=URLDecoder.decode(value, "UTF-8"); resp.getWriter().print("<h1>"+name+"--"+value+"</h2>"); } } private void doAdd(HttpServletRequest req, HttpServletResponse resp) throws IOException { //解决中文乱码问题 Cookie c1 = new Cookie("name", URLEncoder.encode("杰帅", "UTF-8")); Cookie c2 = new Cookie("age", "20"); //创建cookie并且发送到页面 resp.addCookie(c1); resp.addCookie(c2); resp.getWriter().print("<h1>登陆成功</h1>"); } }
作用可以通过cookie来实现再次浏览该网页,然后登陆,判断里面的cookie值,如果有就直接登陆
Session
Session一次会话的,的创建方式
1通过servlet中的req.getSession()获得
2. 创建一个jsp
session的使用
Session.setAttribute()
Session.getAttribute()
Session.removeAttribute()
session的注销
Session.inv
package cn.jiedada.controller; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLDecoder; import java.net.URLEncoder; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @WebServlet("*.session") public class SessionServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String uri = req.getRequestURI(); resp.setContentType("text/html;charset=utf-8"); uri=uri.substring(uri.indexOf("/")+1, uri.indexOf(".")); if(uri.equals("add")){ doAdd(req,resp); } } private void doAdd(HttpServletRequest req, HttpServletResponse resp) throws IOException { HttpSession session = req.getSession(); session.setAttribute("name", "tom"); //session.setMaxInactiveInterval(30); resp.sendRedirect("session.jsp"); } }
防止重复提交
把随机数生成的值绑定到前端,然后在Java代码中删除这次的绑定参数,这样就可以在第二次提交的时候随机数通不过
//通过绑定之获得随机数中的随机值,判断是否和我们输入的相同 String incode = req.getSession().getAttribute("RANDOMCODE_IN_SESSION").toString(); //清除值,让他为null防止重复提交 req.getSession().removeAttribute("RANDOMCODE_IN_SESSION"); if(code.equals(incode)){
验证码
有多种方式这里使用的是uuid的方式,还有雪花函数,等等。。。。
package cn.jiedada.controller; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Random; import java.util.UUID; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/randomCode") public class RandomCode1Servlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //生成随机数 String randomCode = UUID.randomUUID().toString().substring(0, 5); //把随机数放进Session中 req.getSession().setAttribute("RANDOMCODE_IN_SESSION", randomCode); //创建图片对象 int width = 100; int height = 30; int imageType = BufferedImage.TYPE_INT_RGB; BufferedImage image = new BufferedImage(width, height, imageType); //画板 Graphics g = image.getGraphics(); g.setColor(Color.WHITE); //绘制一个实心的矩形 g.fillRect(1, 1, width - 2, height - 2); //把随机数画进图片中 g.setColor(Color.BLUE);//设置随机数的颜色 Font font = new Font("宋体", Font.BOLD + Font.ITALIC, 25); g.setFont(font);//设置随机数的字体和大小 g.drawString(randomCode, 10, 23); //干扰线 g.setColor(Color.GRAY); Random r = new Random(); for (int i = 0; i < 100; i++) { g.fillRect(r.nextInt(width), r.nextInt(height), 2, 2); } //关闭 g.dispose(); //把图片对象以流的方式保存出去 ImageIO.write(image, "jpg", resp.getOutputStream()); } }
原文地址:https://www.cnblogs.com/xiaoruirui/p/11437864.html
时间: 2024-10-07 18:39:55