JavaWeb简单实现记住密码以及自动填充功能

使用技术 jQuery druid MySQL Jackson AJAX JSON

话不多说直接上代码
智汇代理申请https://www.kaifx.cn/broker/t...

–JSP页面

<%--
  Created by IntelliJ IDEA.
  User: QiLin
  Date: 2020/7/28
  Time: 13:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


    
    
    
    管理员登录

    
    
    
    
    
    
    
    


管理员登录


记住密码  
记住密码  

Servlet—登录

package web;

import service.UserService;
import service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.net.URL;
import java.net.URLEncoder;

/**
 * @author: QiLin
 * @date: 2020/8/1 14:32
 * @version: 1.0
 */
@WebServlet( "/Login")
public class Login extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String user = request.getParameter("user");
        String pwd = request.getParameter("password");
        String code = request.getParameter("verifycode");
        String jzmm = request.getParameter("cookie");
        String check_card = (String) request.getSession().getAttribute("check_card");
        HttpSession session = request.getSession();
        if (code.equalsIgnoreCase(check_card)) {
            UserService userService = new UserServiceImpl();
            int login = userService.login(user, pwd);
            if (login == 1) {
                if ( jzmm !=null && jzmm.equals("true") ) {
                    String username = URLEncoder.encode(user,"UTF-8");
                    String userpwd = URLEncoder.encode(user+"pwd","UTF-8");
                    Cookie name = new Cookie(username,username);
                    Cookie pwdpwd = new Cookie(userpwd,pwd);
                    Cookie jzmmm = new Cookie("jzmm",jzmm);
                    name.setMaxAge(1*60*60);
                    pwdpwd.setMaxAge(1*60*60);
                    jzmmm.setMaxAge(1*60*60);
                    session.setAttribute("username", user);
                    session.setAttribute("password", pwd);
                    response.addCookie(name);
                    response.addCookie(pwdpwd);
                    response.addCookie(jzmmm);
                    request.setAttribute("user",user);
                    request.getRequestDispatcher("index.jsp").forward(request,response);
                } else {
                    request.setAttribute("user",user);
                    request.getRequestDispatcher("index.jsp").forward(request,response);
                }
            } else {
                session.removeAttribute("msg1");
                session.setAttribute("msg2","用户名或密码错误");
                response.sendRedirect("login.jsp");
            }
        } else {
            session.removeAttribute("msg2");
            session.setAttribute("msg1","验证码错误");
            response.sendRedirect("login.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

Servlet—获取密码

package web;

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * @author: QiLin
 * @date: 2020/8/2 14:09
 * @version: 1.0
 */
@WebServlet( "/GetCookie")
public class GetCookie extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String user = request.getParameter("uname");
        String password = "";
        ObjectMapper mapper = new ObjectMapper();
        // 获取cookie
        Cookie[] cookies = request.getCookies();
        // 如果为空,则停留在该页面
         if(cookies !=null || cookies.length >0) {
            String username = URLEncoder.encode(user,"UTF-8");
            String userpwd = URLEncoder.encode(user+"pwd","UTF-8");
            for (int i = 0; i < cookies.length; i++) {
                if (cookies[i].getName().equals(username)) {
                    user = URLDecoder.decode(cookies[i].getValue(),"UTF-8");
                }
                if (cookies[i].getName().equals(userpwd)) {
                    password = cookies[i].getValue();
                }
            }
            request.setAttribute("pwd",password);
            String pwd = mapper.writeValueAsString(request.getAttribute("pwd"));
            response.getWriter().write(pwd);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

Servlet—验证码

package web;

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;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

/**
 * @author: QiLin
 * @date: 2020/8/1 13:52
 * @version: 1.0
 */
@WebServlet( "/Check_card")
public class Check_card extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置宽高
        int width = 100;
        int height = 50;
        //创建对象,在内存验证码图
        BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
        //美化图片
        Graphics graphics = image.getGraphics();
        //画边框
        graphics.setColor(Color.magenta);
        graphics.drawRect(0,0,width-1,height-1);
        //填充背景颜色
        graphics.setColor(Color.cyan);//画笔颜色
        graphics.drawRect(0,0,width,height);
        //定义随机抽取池
        String str = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuioplkjhgfdsazxcvbnm0123456789";
        //生成随机角标
        Random ran = new Random();
        //接收验证码
        StringBuffer sb = new StringBuffer();
        //写入验证码
        for (int i = 1; i <= 4; i++) {
            int index = ran.nextInt(str.length());
            //获取随机字符
            char c = str.charAt(index);
            sb.append(c);
//            //生成随机x轴
//            int x = ran.nextInt(width-10) % (width-10-10+1)+10;
//            //生成随机y轴
//            int y = ran.nextInt(height-5) % (width-5-5+1)+5;
            graphics.setFont(new Font("Tahoma", Font.BOLD, 18));
            graphics.drawString(c+"",width/5*i,height/2);
        }
        //存入session
        String session_check = sb.toString();
        request.getSession().setAttribute("check_card",session_check);
        //画干扰线
        graphics.setColor(Color.green);
        for (int i = 0; i < 10; i++) {
            int x1 = ran.nextInt(width);
            int x2 = ran.nextInt(width);
            int y1 = ran.nextInt(height);
            int y2 = ran.nextInt(height);
            graphics.drawLine(x1,y1,x2,y2);
        }
        //将图片画到页面
        ImageIO.write(image,"jpg",response.getOutputStream());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

你可能感兴趣的:(java,web)