2020.04.20 登录验证码案例

Posted aojie

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2020.04.20 登录验证码案例相关的知识,希望对你有一定的参考价值。

package com.aojie.servlet;

import com.aojie.dao.UserDao;
import com.aojie.domain.User;
import org.apache.commons.beanutils.BeanUtils;

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 javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
* @author aojie
* @fuction
* @create 2020-04-23 20:28
*/
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String checkcode=request.getParameter("checkcode");
Map<String, String[]> parameterMap = request.getParameterMap();
User user=new User();
try {
BeanUtils.populate(user,parameterMap);
System.out.println(user);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
HttpSession session = request.getSession();
String checkcode2 = (String)session.getAttribute("checkcode");
session.removeAttribute("checkcode");
if (checkcode.equalsIgnoreCase(checkcode2)){
UserDao userDao = new UserDao();
User login = userDao.login(user);
if (login!=null){
HttpSession session1 = request.getSession();
session.setAttribute("user",login.getUsername());
//重定向
response.sendRedirect(request.getContextPath()+"/success.jsp");
}else{
request.setAttribute("loginmsg","用户名或密码错误");
request.getRequestDispatcher("/login.jsp").forward(request,response);
}
}else {
request.setAttribute("checkcodemsg","验证码不正确");
request.getRequestDispatcher("/login.jsp").forward(request,response);
}
}

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

以上是关于2020.04.20 登录验证码案例的主要内容,如果未能解决你的问题,请参考以下文章

登录案例version1 基本登录+验证码

antd-Vue实现Login登录页面布局-附带验证码验证功能-案例

antd-Vue实现Login登录页面布局-附带验证码验证功能-案例

Django(十六)基于模板的登录案例:登录装饰器csrf攻击方式及防护ajax的Post 的csrf开启写法生成验证码加验证码登录反向解析+传参

Session & Cookie登录案例 January 27,2020

登录(带验证码图片)小案例