1、页面具有三个input框:用户名、密码、验证码;
2、点击验证码图片可以为用户更换一张验证码;
3、首先判断验证码是否输入正确,若错误,网页提示“验证码输入错误”;
4、再判断用户名和密码是否正确,若错误,网页提示“用户名或密码错误”
5、只有当三者全部输入正确时,跳转到成功登录界面,显示“欢迎您,用户名”
IDE:idea
数据库:mysql
连接池:druid
JDBCTemplate模板
1、导入相关工具包
2、编写login.jsp页面
<%--
Created by IntelliJ IDEA.
User: lenovo
Date: 2020/2/22
Time: 19:47
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
登录
<%=request.getAttribute("cc_error") == null ?"": request.getAttribute("cc_error")%>
<%=request.getAttribute("user_error") == null ? "":request.getAttribute("user_error")%>
2、使用JDBCTemplate实现操作数据库功能
参考之前文章:在idea下使用JDBCTemplate实现登录功能
(1)配置数据库连接池文件
(2)创建数据表
(3)创建User类,与数据表数据一一对应
(4)创建JDBCUtils工具包
(5)在dao包下创建UserDao类,编写logn方法,操作数据库
3、制作验证码图片并输出
参考之前文章:java实现验证码功能
由于之后要判断用户的验证码是否输入正确,在这里使用session存储生成的验证码。
4、编写loginServlet页面。
(1)获取请求参数的map集合,使用BeanUtils.populate(user,map)方法,将user进行封装
(2)获取session中存储的验证码,与用户输入进行判断。
(3)验证码正确,调用userDao中的login方法,判断用户名密码是否正确。
(4)若全部正确,页面转发到successServlet页面。
(5)三个输入有一向错误,到登录界面,展现错误提示。
package com.li.LoginTest;
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;
@WebServlet( "/checkCodeServlet")
public class checkCodeServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int hight = 50;
int width = 100;
//1、创建一个对象
BufferedImage bufferedImage = new BufferedImage(width, hight, BufferedImage.TYPE_INT_RGB);
//2 美化图片
//2.1 填充背景色
Graphics graphics = bufferedImage.getGraphics();//画笔对象
graphics.setColor(Color.PINK);//设置画笔颜色
graphics.fillRect(0,0,width,hight);
//2.2 画边框
graphics.setColor(Color.blue);
graphics.drawRect(0,0,width-1,hight-1);
//2.3 写验证码,验证码使使用随机数显示
String str = "QWERTYUIOPASDFGHJKLMNBVCXZqwertyuioplkjhgfdsazxcvbnm1234567890";
Random ran = new Random();
//2.4new一个StringBuilder,存储验证码信息
StringBuilder stringBuilder = new StringBuilder();
for(int i=1;i<=4;i++){
int num = ran.nextInt(str.length());
char c = str.charAt(num);
stringBuilder.append(c);
graphics.drawString(c+"",width/5*i,hight/2);
}
//2.5、将验证码存入session中,以便之后的验证
String checkCode = stringBuilder.toString();
request.getSession().setAttribute("checkCode",checkCode);
//2.6 随机画一些线
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(hight);
int y2 = ran.nextInt(hight);
graphics.drawLine(x1,y1,x2,y2);
}
//3 输出
ImageIO.write(bufferedImage,"jpg",response.getOutputStream());
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
package com.li.LoginTest;
import com.li.dao.UserDao;
import com.li.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.beans.JavaBean;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.Set;
@WebServlet("/loginServlet")
public class loginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
User user = new User();
Map<String, String[]> parameterMap = request.getParameterMap();
try {
BeanUtils.populate(user,parameterMap);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
UserDao userDao = new UserDao();
User loginUser = userDao.login(user);
//1、获取请求参数的值
// String username = request.getParameter("username");
// String password = request.getParameter("password");
String checkCode = request.getParameter("checkCode");
System.out.println(checkCode);
//2、验证码是否正确
//2.1 使用session获取checkCode的值
HttpSession session = request.getSession();
String checkCode1 = (String) session.getAttribute("checkCode");
session.removeAttribute("checkCode");
System.out.println(checkCode1);
//2.2 判断
if(checkCode1 != null && checkCode1.equalsIgnoreCase(checkCode)){
//判断用户名与密码
if(loginUser!=null){
//用户名与密码正确
//使用request域存储用户名
request.setAttribute("username",loginUser.getUsername());
//转发
request.getRequestDispatcher("/successServlet").forward(request,response);
}else{
//用户名与密码不正确
request.setAttribute("user_error","用户名或密码输入错误");
request.getRequestDispatcher("/login.jsp").forward(request,response);
System.out.println("用户名或密码输入错误");
}
}else{
//验证码不正确,
request.setAttribute("cc_error","验证码输入错误");
request.getRequestDispatcher("/login.jsp").forward(request,response);
System.out.println("验证码输入错误");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
package com.li.LoginTest;
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.io.IOException;
@WebServlet( "/successServlet")
public class SuccessServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取request域的用户名
String username = (String) request.getAttribute("username");
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("欢迎您,"+username);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}